The program will sort 3 numbers in ascending or descending order. The code will be like this.
//sorting 3 number.. from lowest to highest digit or vice versa//for educational purposes only#include <iostream>using namespace std;int main(){int num1, num2, num3;int min, ave, max;char ans;char ans1;while (ans != 'y'){cout<<"Enter 3 numbers:"<<endl;cin>>num1>>num2>>num3;cout<<endl<<endl<<endl<<endl<<endl;if (num1 > num2){if (num1 > num3){if (num2 > num3){max = num1;ave = num2;min = num3;}else{max = num1;ave = num3;min = num2;}}else{max = num3;ave = num1;min = num2;}}else{if (num2 > num3){if (num1 > num3){max = num2;ave = num1;min = num3;}else{max = num2;ave = num3;min = num1;}}else{max = num3;ave = num2;min = num1;}}cout<<"Key in 'a' for ascending order or 'd' for descending order: ";cin >> ans1;cout<<endl<<endl<<endl<<endl<<endl;if (ans1 == 'a'){cout<<"Sorted to: "<<min<<" "<<ave<<" "<<max<<endl<<endl<<endl<<endl<<endl;}else{cout<<"Sorted to: "<<max<<" "<<ave<<" "<<min<<endl<<endl<<endl<<endl<<endl;}cout<<"Type ""y"" to quit. or type any letter to try again: ";cin>>ans;cout<<endl<<endl<<endl;}return 0;}
0 comments:
Post a Comment