I'm working on an beginning C++ assignment that requires me to find the minimum and maximum values of a variable. Specifically, the program is asking the user to input the age of people going in to a particular business. I'm using a single variable "age", and a while loop to get the various age values. What I need now is a way to get the max/min values entered into the variable age. Can anyone help? I hope that's clear! Thanks!
http://www2.research.att.com/~bs/bs_faq2.html I am not a C++ person. But MSDN has some great tutorials and there's a classing post at http://www2.research.att.com/~bs/bs_faq2.html that may help!
void main( ) { int age[20],i,min,max; for(i=0;i<=20;i++) { cout<<"Enter the age of i th man"; cin>>age[i]; } for(i=0;i<=20;i++) { if(age[i]>max) max=age[i]; if(age[i]<min) min=age[i]; } cout<<"maximum aged man is :"; cout<<max; cout<<"minimum aged man is :"; cout<<min; getch( ); }
There's no need to store the ages in an array if all you want is the min and max: void main() { bool started = false; // or equivalent C++ syntax int min, max, age; while (1) { cin >> age; // read ages in somehow if (!started) // first time through loop, first age is both min and max { min = age; max = age; started = true; // or equivalent C++ syntax } else if (age < min) min = age; else if (age > max) max = age; } }
Join our real-time social learning platform and learn together with your friends!