0

How to find maximum and minimum value in number array?

21st Jan 2017, 9:41 AM
Aakash Garg
Aakash Garg - avatar
3 Answers
+ 8
Set the first element to max(min), and then search trough all the elements, asking if the next one is higher(lower) than max(min) value. It's very easy, try it
21st Jan 2017, 9:54 AM
Filip
Filip - avatar
+ 6
@dariusPianotised Consider this piece of code memory efficient for(int i=0;i<ARRAYSIZE(a);i++) { cout<< "\nEnter value: "; cin>> a[i]; // puts values in array if(i==0) { minValue=a[0]; maxValue=a[0]; } else if(minValue>a[i]) minValue=a[i]; else if(maxValue<a[i]) maxValue = a[i]; }
21st Jan 2017, 10:56 AM
Megatron
Megatron - avatar
+ 2
#include <iostream> #include<Windows.h> //Include Windows to use ARRAYSIZE using namespace std; int main () { int minValue,maxValue; const int size = 10; // modifying this value will allow for less/more numbers to be entered by the user int a[size]; cout<<"Enter your values:"; for(int i=0;i<ARRAYSIZE(a);i++) { cout<< "\nEnter value: "; cin>> a[i]; // puts values in array } minValue=a[0]; maxValue=a[0]; for(int i=1;i<ARRAYSIZE(a);i++) { if(minValue>a[i]) { minValue=a[i]; } else if(maxValue<a[i]){ maxValue = a[i]; } } cout<<"Maximum number is: "<< maxValue << endl; cout<<"Minimum number is: "<< minValue << endl; return 0; }
21st Jan 2017, 10:03 AM
dariusPianotised
dariusPianotised - avatar