|
-
Jan 20th, 2003, 05:14 PM
#1
Thread Starter
Fanatic Member
checking the largest and smallest #
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.
-
Jan 20th, 2003, 06:35 PM
#2
Frenzied Member
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;
}
-
Jan 21st, 2003, 11:36 AM
#3
Isn't the smallest possible integer 0xe0000000?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 21st, 2003, 11:36 AM
#4
BTW, the <algorithm> header contains functions to do that for you.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 23rd, 2003, 02:39 AM
#5
transcendental analytic
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;
}
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|