C++ Online Compiler
Example: How to Find Max and Min in Array C++ Program
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// How to Find Max and Min in Array C++ Program #include <iostream> #include <cmath> using namespace std; int main() { int s=0; cout << "Enter the size of the array::\n"; cin >> s; int arr[s]; cout << "\nEnter the " << s << " elements of the array::\n"; int i=0; while (i<s) { cin >> arr[i]; i++; } double min=INFINITY, max=-INFINITY; int l=s, min_sum=0, max_sum=0, counter=0; // It will get the lowest and highest value from the array while (l--) { if (arr[l] < min) { min=arr[l]; } if (arr[l] > max) { max=arr[l]; } if (arr[l]==arr[s-1]) { counter++; } } // It will calculate the minimum & maximum sum from the array if (counter == s) { l=s-1; while (l--) { min_sum += arr[l]; } max_sum=min_sum; } else { l=s; while (l--) { if (arr[l]!=max) { min_sum += arr[l]; } if (arr[l]!=min) { max_sum += arr[l]; } } } // It will print the final output of the program cout << "MIN SUM:: " << min_sum << endl; cout << "MAX SUM:: " << max_sum << endl; return 0; }
6 45 50 3 78 3 99
Output
Clear
ADVERTISEMENTS