Advertisement
avr39ripe

cppDynArrCreateFillPrintDelete

May 6th, 2021
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int* createArr(int size)
  4. {
  5.     return new int[size];
  6. }
  7.  
  8. void deleteArr(int* ptr)
  9. {
  10.     delete[] ptr;
  11. }
  12.  
  13. void printArr(const int* begin, const int* const end)
  14. {
  15.     while (begin != end)
  16.     {
  17.         std::cout << *begin++ << ' ';
  18.     }
  19.     std::cout << '\n';
  20. }
  21.  
  22. void fillArr(int* begin, const int* const end, int start, int stop)
  23. {
  24.     while (begin != end)
  25.     {
  26.         *begin++ = (rand() % (stop - start) + start);
  27.     }
  28. }
  29.  
  30.  
  31. void fillArr(int* begin, const int* const end, int val)
  32. {
  33.     while (begin != end)
  34.     {
  35.         *begin++ = val;
  36.     }
  37. }
  38.  
  39. int main()
  40. {
  41.     int arr1Size{ 15 };
  42.     auto arr1{ createArr(arr1Size) };
  43.     fillArr(arr1, arr1 + arr1Size, 0);
  44.     fillArr(arr1 + (arr1Size / 4), arr1 + arr1Size - (arr1Size / 4), 0, 10);
  45.     printArr(arr1, arr1 + arr1Size);
  46.  
  47.     int arr2Size{ 20 };
  48.     auto arr2{ createArr(arr2Size) };
  49.     fillArr(arr2, arr2 + arr2Size, 0);
  50.     fillArr(arr2+(arr2Size / 2), arr2 + arr2Size, 20, 30);
  51.     printArr(arr2, arr2 + arr2Size);
  52.  
  53.     deleteArr(arr2);
  54.     deleteArr(arr1);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement