avr39ripe

cppArrMinMaxByIndexSearch

Jul 12th, 2021
753
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3.  
  4. int main()
  5. {
  6.     const int minRange{ 10 };
  7.     const int maxRange{ 20 };
  8.  
  9.     const int arrSize{ 10 };
  10.     //int arr[arrSize]{ 1,-4,2,7,8,-6,1,-3,0,7 };
  11.     int arr[arrSize]{};
  12.  
  13.     int maxIdx{ 0 };
  14.     int minIdx{ 0 };
  15.  
  16.  
  17.     //for (int i{ 0 }; i < arrSize; ++i) { std::cout << arr[i] << '\t'; } std::cout << '\n';
  18.  
  19.     for (int i{ 0 }; i < arrSize; ++i)
  20.     {
  21.         arr[i] = minRange + rand() % (maxRange - minRange + 1);
  22.     }
  23.  
  24.     for (int i{ 0 }; i < arrSize; ++i) { std::cout << arr[i] << '\t'; } std::cout << '\n';
  25.  
  26.     for (int i{ 0 }; i < arrSize; ++i)
  27.     {
  28.         if (arr[i] > arr[maxIdx]) { maxIdx = i; }
  29.         if (arr[i] < arr[minIdx]) { minIdx = i; }
  30.     }
  31.  
  32.     std::cout << "Max index is: " << maxIdx << " and max element is: " << arr[maxIdx] << '\n';
  33.     std::cout << "Min index is: " << minIdx << " and min element is: " << arr[minIdx] << '\n';
  34.  
  35.     return 0;
  36.  
  37.     int maxVal{ 6 };
  38.     int arrSum{ 0 };
  39.  
  40.     //for (int i{ 0 }; i < arrSize; ++i) { std::cout << arr[i] << ' '; } std::cout << '\n';
  41.  
  42.     for (int i{ 0 }; i < arrSize; ++i)
  43.     {
  44.         if (arr[i] < maxVal)
  45.         {
  46.             arrSum += arr[i];
  47.         }
  48.     }
  49.  
  50.     std::cout << "Sum of all array elements is: " << arrSum << '\n';
  51.  
  52.     //for (int i{ 0 }; i < arrSize; ++i) { std::cout << arr[i] << ' '; } std::cout << '\n';
  53.  
  54.     return 0;
  55. }
RAW Paste Data