hngmin15 Posted October 8, 2004 Share Posted October 8, 2004 hi, my teacher told us to create a program that would ask for 3 numbers and put them in order from least to greatest.. i am having trouble doing the least to greatest part. please help asap. here is where im up to.. #include <iostream.h> int main() { int a, b, c; cout<<"what is the first number:"; cin>>a; cout<<"what is the 2nd number:"; cin>>b; cout<<"what is the 3rd number:"; cin>>c; cout<<"You have entered:"<<a<<b<<c<<endl; if ((a<=c) || (c>=b)) cout<<"the smallest number is:"<<a; cout<<endl; if ((a<=b) || (b<=c)) cout<<"the middle number is:"<<b; cout<<endl; if ((b<=c) || (c<=a)) cout<<"the largest number is:"<<c; cout<<endl; } Link to comment Share on other sites More sharing options...
0 Crunch Posted October 8, 2004 Share Posted October 8, 2004 If input is 1, 2, 3 what is the first number:1 what is the 2nd number:2 what is the 3rd number:3 You have entered:123 the smallest number is:1 the middle number is:2 the largest number is:3 Press any key to continue if input is 3, 2, 1 what is the first number:3 what is the 2nd number:2 what is the 3rd number:1 You have entered:321 the largest number is:1 Press any key to continue Link to comment Share on other sites More sharing options...
0 Crunch Posted October 8, 2004 Share Posted October 8, 2004 you have logic errors no matter if your if() tests become true or not they will always display smallest = a middle = b etc... if (a <= b && a <= c) ?cout << "The smallest number is " << a << endl; else if (b <= a && b <= c) ?cout << "The smallest number is " << a << endl; else if (c <= b && c <= a) ?cout << "The smallest number is " << a << endl; that will tell you the smallest gives you a chance to figure the rest for yourself Link to comment Share on other sites More sharing options...
0 hngmin15 Posted October 8, 2004 Author Share Posted October 8, 2004 hmmm i didnt know you can do that. but thx Link to comment Share on other sites More sharing options...
0 hngmin15 Posted October 8, 2004 Author Share Posted October 8, 2004 you have logic errors no matter if your if() tests become true or notthey will always display smallest = a middle = b etc... if (a <= b && a <= c) ?cout << "The smallest number is " << a << endl; else if (b <= a && b <= c) ?cout << "The smallest number is " << a << endl; else if (c <= b && c <= a) ?cout << "The smallest number is " << a << endl; that will tell you the smallest gives you a chance to figure the rest for yourself um.......your code does not seem to work very well. because if i put in 2,3,1 its different and also 3,2,1 or 2,1,3 Link to comment Share on other sites More sharing options...
0 Mouton Posted October 8, 2004 Share Posted October 8, 2004 (edited) // Sort a, b and c if (a > b) { // a & b are not in the good order; exchange the two numbers int temp = a; a = b; b = temp; } if (b > c) { // b & c are not in the good order; exchange the two numbers int temp = b; b = c; c = temp; } if (a > b) { // a & b are not in the good order; exchange the two numbers int temp = a; a = b; b = temp; } // here, a, b and c are in order... just print them http://en.wikipedia.org/wiki/Bubble_sort Edited October 8, 2004 by Mouton Link to comment Share on other sites More sharing options...
0 Crunch Posted October 8, 2004 Share Posted October 8, 2004 true but wouldnt a bubble sort be a little advanced for hngmin12 at this time Link to comment Share on other sites More sharing options...
0 Crunch Posted October 8, 2004 Share Posted October 8, 2004 um.......your code does not seem to work very well. because if i put in 2,3,1 its different and also 3,2,1 or 2,1,3 yes sorry i forgot the change the cout statements to CODE ? if (a <= b && a <= c) cout << "The smallest number is " << a << endl; else if (b <= a && b <= c) cout << "The smallest number is " << b << endl; else if (c <= b && c <= a) cout << "The smallest number is " << c << endl; Link to comment Share on other sites More sharing options...
0 Crunch Posted October 8, 2004 Share Posted October 8, 2004 this is for you, hngmin12 I think this is the simplest way to complete you can use either by multiple if statements or implementing a temp variable to sort them out int main() { int a, b, c; cout << "what is the first number:"; cin >> a; cout << "what is the second number:"; cin >> b; cout << "what is the third number:"; cin >> c; cout << "You have entered: " << a << " " << b << " " << c << endl; //if statements to determine smallest number if (a <= b && a <= c) cout << "The smallest number is " << a << endl; else if (b <= a && b <= c) cout << "The smallest number is " << b << endl; else if (c <= b && c <= a) cout << "The smallest number is " << c << endl; //if statements to determine middle number if (((a >= b) && (a <= c)) || ((a >= c) && (a <= b))) cout << "The middle number is " << a << endl; if (((b >= a) && (b <= c)) || ((b >= c) && (b <= a))) cout << "The middle number is " << b << endl; if (((c >= a) && (c <= b)) || ((c >= b) && (c <= a))) cout << "The middle number is " << c << endl; //if statements to determine largest number if (a >= b && a >= c) cout << "The largest number is " << a << endl; else if (b >= a && b >= c) cout << "The largest number is " << b << endl; else if (c >= b && c >= a) cout << "The largest number is " << c << endl; return 0; } or Link to comment Share on other sites More sharing options...
0 Mouton Posted October 8, 2004 Share Posted October 8, 2004 true but wouldnt a bubble sort be a little advanced for hngmin12 at this time I suggested 3 if statements, and swapping 2 variables. very basic stuff. Takes about 1 min to understand even if u never saw it. It uses the same way your would sort 3 numbers if u had them on a table before u. U suggest 9 if statements, with multiple && and ||..... Link to comment Share on other sites More sharing options...
Question
hngmin15
hi, my teacher told us to create a program that would ask for 3 numbers and put them in order from least to greatest.. i am having trouble doing the least to greatest part. please help asap. here is where im up to..
#include <iostream.h>
int main()
{
int a, b, c;
cout<<"what is the first number:";
cin>>a;
cout<<"what is the 2nd number:";
cin>>b;
cout<<"what is the 3rd number:";
cin>>c;
cout<<"You have entered:"<<a<<b<<c<<endl;
if ((a<=c) || (c>=b))
cout<<"the smallest number is:"<<a;
cout<<endl;
if ((a<=b) || (b<=c))
cout<<"the middle number is:"<<b;
cout<<endl;
if ((b<=c) || (c<=a))
cout<<"the largest number is:"<<c;
cout<<endl;
}
Link to comment
Share on other sites
9 answers to this question
Recommended Posts