Advertisement
avr39ripe

cppRefPointerFuncMadness

May 6th, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int& max(int& a, int& b)
  4. {
  5.     return a > b ? a : b;
  6. }
  7.  
  8. int& max(int* arrB, int* arrE)
  9. {
  10.     int* const arr{ arrB };
  11.     int maxIdx{ 0 };
  12.     for (int i{0}; arrB != arrE; ++i, ++arrB)
  13.     {
  14.         if (*(arrB) > *(arr + maxIdx))
  15.         {
  16.             maxIdx = i;
  17.         }
  18.     }
  19.     return *(arr + maxIdx);
  20. }
  21.  
  22. int& testFunc()
  23. {
  24.     int random{ 42};
  25.     return random;
  26. }
  27.  
  28. int* testFunc1()
  29. {
  30.     int random1{ 33 };
  31.     return &random1;
  32. }
  33.  
  34. void calcArr(int* arrB, int* arrE, int& pos, int& neg, int& zero)
  35. {
  36.     while (arrB != arrE)
  37.     {
  38.         if (*arrB > 0) { ++pos; }
  39.         else if (*arrB < 0) { ++neg; }
  40.         else { ++zero; }
  41.         ++arrB;
  42.     }
  43. }
  44.  
  45. void calcArr(int* arrB, int arrSize, int& pos, int& neg, int& zero)
  46. {
  47.     for (int i{0}; i < arrSize; ++i)
  48.     {
  49.         if (arrB[i] > 0) { ++pos; }
  50.         else if (arrB[i] < 0) { ++neg; }
  51.         else { ++zero; }
  52.     }
  53. }
  54.  
  55. void boom()
  56. {
  57.     uint64_t bigArr[10000]{};
  58.     std::cout << "Boom!\n";
  59. }
  60.  
  61. void printArr(const int* begin, const int* const end)
  62. {
  63.     while (begin != end)
  64.     {
  65.         std::cout << *begin++ << ' ';
  66.     }
  67.     std::cout << '\n';
  68. }
  69.  
  70. void fillArr(int* begin, const int* const end, int val)
  71. {
  72.     while (begin != end)
  73.     {
  74.         *begin++ = val;
  75.     }
  76. }
  77.  
  78. int main()
  79. {
  80.     const int srcSize{ 10 };
  81.     int arrSrc[srcSize]{ 1,2,3,4,5,6,7,8,9,10 };
  82.     int testArr[srcSize]{ 1,3,6,2,7,8,9,11,4,7 };
  83.     int calcTest[srcSize]{ 0,1,2,0,-1,-6,3,0,-7,3 };
  84.     const int destSize{ 11 };
  85.     int arrDest[destSize]{};
  86.  
  87.     int p{};
  88.     int n{};
  89.     int z{};
  90.  
  91.     calcArr(calcTest, calcTest + srcSize, p, n, z);
  92.  
  93.     std::cout << "Positive: " << p << "\nNegative: " << n << "\nZero: " << z << '\n';
  94.  
  95.     ////std::cout << testFunc() << '\n';
  96.     //int* ptr{ testFunc1() };
  97.     //std::cout <<  *ptr << '\n';
  98.     ////boom();
  99.     //std::cout << *ptr << '\n';
  100.  
  101.     //int x{ 10 };
  102.     //int y{ 20 };
  103.  
  104.     //std::cout << max(testArr, testArr + 5) << '\n';
  105.     //std::cout << max(testArr+5, testArr + srcSize) << '\n';
  106.     //*(max(&x, &y)) += 10;
  107.     /*max(x, y) += 10;
  108.  
  109.     std::cout << "x = " << x << " y = " << y << '\n';*/
  110.  
  111.     //int* const ptr{ arrSrc };
  112.  
  113.     //*(ptr + 4) = 55;
  114.  
  115.    
  116.    
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement