higueraalfredo Posted November 19, 2005 Share Posted November 19, 2005 Is there a better way to find the minimum and maximum of three numbers? This is what I have for the function right now void FindMaxMin(int x, int y, int z, int& max, int& min) { if(x<=y && x<=z) { min=x; if(y<z) max=z; else max=y; } if (y<=x && y<=z) { min=y; if(x<z) max=z; else max=x; } if (z<=x && z<=y) { min=z; if(x<y) max=y; else max=x; } } Link to comment Share on other sites More sharing options...
0 strotee Posted November 19, 2005 Share Posted November 19, 2005 I used an array. Here's the whole program. #include <iostream> using namespace std; int main() { int temp,small,large; int number[3]; for (int a=0;a<3;a++){ system("cls"); cout << "Enter a number: " << endl; cin >> temp; number[a] = temp; } small = number[0]; large = number[0]; for (int i=0;i<3;i++){ if (number[i] <= small) small = number[i]; if (number[i] >= large) large = number[i]; } cout << small << " is the smallest number." << endl; cout << large << " is the largest number." << endl; system("pause"); return 0; } Link to comment Share on other sites More sharing options...
0 Julius Caro Posted November 19, 2005 Share Posted November 19, 2005 Yup I did something similar a while ago in my C course. Basically, you have a certain amount of numbers, and two variables, max and min for example. let's say the first number is min and the last one is max, or whatever :p You basically make a loop, and compare the current max / min with all the numbers. If there is any number smaller or larger, then set the new max to that number. Or something like that :p Link to comment Share on other sites More sharing options...
0 nikhil500 Posted November 19, 2005 Share Posted November 19, 2005 This one would be better than yours. Have not compiled it, so it may have some syntax errors. void FindMaxMin(int x, int y, int z, int& max, int& min) { if(x<=y && x<=z) { min=x; if(y<z) max=z; else max=y; } else if (y<=z) { min=y; if(x<z) max=z; else max=x; } else { min=z; if(x<y) max=y; else max=x; } } I have a feeling that it can be improved much more. Will post if I come up with something. Link to comment Share on other sites More sharing options...
0 +Xinok Subscriber² Posted November 19, 2005 Subscriber² Share Posted November 19, 2005 My stab at it: #include <iostream> using namespace std; #define GetMin(a,b) ((a) < (b) ? (a) : (b)) #define GetMax(a,b) ((a) > (b) ? (a) : (b)) int main(){ int array[] = {3, 5, 7}; int min, max; min = max = array[0]; for(int i = 1; i < sizeof(array)/sizeof(array[0]); i++){ ?min = GetMin(min, array[i]); ?max = GetMax(max, array[i]); } return 0; } Edited: Updated to use Define macros. Link to comment Share on other sites More sharing options...
0 nikhil500 Posted November 19, 2005 Share Posted November 19, 2005 Another attempt..... void FindMaxMin(int x, int y, int z, int& max, int& min) { if (x<=y) { if (x<=z) { min=x; if (y<=z) max=z; else max=y; } else { min=z; max=y; } } else if (x<=z) { max=z; min=y; } else { max=x; if(y<=z) min=y; else min=z; } } I guess this is the best I can do. The program will perform a maximum of 3 comparisons and 2 assignments. Link to comment Share on other sites More sharing options...
0 CdCViRus Posted November 20, 2005 Share Posted November 20, 2005 #define max(a,b) ((a) > (b) ? (a) : (b)) #define max3(a,b,c) (max((a), max((b),(c)))) same for min then, your function will look like this: void FindMaxMin(int x, int y, int z, int& max, int& min) { max = max3(x, y, z); min = min3(x, y, z); } Link to comment Share on other sites More sharing options...
0 higueraalfredo Posted November 20, 2005 Author Share Posted November 20, 2005 Well it seems like this is done with if statements so I'll stick with mine and I haven't been taught about that #define thing so I don't want to use it. Link to comment Share on other sites More sharing options...
Question
higueraalfredo
Is there a better way to find the minimum and maximum of three numbers?
This is what I have for the function right now
Link to comment
Share on other sites
7 answers to this question
Recommended Posts