I want a loop that asks the user for integers, wehn the user types 0, It displays the biggest and smallest integer s/he typed. And then it shows the difference under it. how would I, the easiest way.
Printable View
I want a loop that asks the user for integers, wehn the user types 0, It displays the biggest and smallest integer s/he typed. And then it shows the difference under it. how would I, the easiest way.
Code:int max int *);
int min(int *);
int main(){
int a[100];
fir (int i=0;i<100;i++)a[i]=0;
// loop for reading user input
// blah
printf("smallest %d\n", min(a));
printf("largest %d\n", max(a));
}
// assumes 32 bit integers
int max(int * a){
int tmp = 0xefffffff; // smallest possible integer (negative)
while( *a){
if (*a > tmp) tmp = *a;
a++;
}
return tmp;
}
int min(int * a){
int tmp = 0x7fffffff; // biggest possible integer (postrive)
while( *a){
if (*a < tmp) tmp = *a;
a++;
}
return tmp;
}
Isn't the smallest possible integer 0xe0000000?
BTW, the <algorithm> header contains functions to do that for you.
Code:#include <iostream>
using namespace std;
int main(){
int x;
cin>>x;
in max=x,min=x;
while (x){
max=x>max?x:max;
min=x<min?x:min;
cin>>x;
}
cout<<min<<","<<max<<endl;
return 0;
}