Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
how can i compare more than two variables in loops? to find out the largest an minimum number here is my code which takes 10 inputs from the user .. Now how can i find out maximum(largest) and minimum (smallest) between them?????
 
#include<iostream>
using namespace std;
 
int main()
 
{
 int max=0,min=0;
 int num;
 int counter =0;
 cout<<"Enter 10 numbers"<<endl;
 while(counter<10)
 {
 
     cin>>num;
     counter++;
 }
     return 0;
 }
Posted 17-Dec-12 21:45pm
Edited 17-Dec-12 22:37pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can compare as many as you want. You need to reword this, with code, so it makes sense. Typically, you create a variable for the biggest and one for the smallest, and every one, you replace the current value if the new one is a better match.
  Permalink  
Comments
Malik Yousaf at 18-Dec-12 4:19am
   
here i wrote a code which takes 10 inputs from the user now how can i find maximum and minimum number between them???
Christian Graus at 18-Dec-12 14:21pm
   
I told you how and someone else ( foolishly in my view ) wrote the code for you. If you could not write that code, you need to talk to your teacher, you're struggling.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

#include<iostream>
#include<climits>
using namespace std;
 
int main()
{
 int max=INT_MIN,min=INT_MAX;
 int num;
 int counter =0;
 cout<<"Enter 10 numbers"<<endl;
 while(counter<10)
 {
 
     cin>>num;
     if ( max < num) max = num;
     if ( min > num) min = num;
     counter++;
 }
 cout << "min " << min << ", max " << max << endl;
 return 0;
 }
  Permalink  
v2
Comments
Malik Yousaf at 18-Dec-12 7:16am
   
Thank you very much.
can you tell me other way without using climits??
CPallini at 18-Dec-12 8:15am
   
Of course there are many ways. But why? Why don't you want climits header?
Philippe Mori at 18-Dec-12 20:22pm
   
If counter is 0, you can initialize both min and max with num.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 250
1 OriginalGriff 210
2 Manfred R. Bihy 190
3 CHill60 180
4 _Amy 155
0 OriginalGriff 7,445
1 Sergey Alexandrovich Kryukov 6,177
2 Maciej Los 3,774
3 Peter Leow 3,478
4 CHill60 2,702


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100