Write a program that asks the user to enter (3) digits, than displays the following menu:
a- Display the maximum
b- Display the minimum
c- Display the numbers sorted in decreasing order
d- Display odd numbers
e- Display even numbers
f- Write the numbers in a file.
The program then asks the user to enter his choice and executes the required task.
If the user enters option ‘f’ ,the program will have to ask the user to enter the name of the file.
Important :
1- Use a switch to handle the different options of the menu.
2- All numbers need to be displayed with 2 decimals precision.
1
Expert's answer
2012-11-13T10:36:20-0500
#include <iostream> #include <fstream> #include <string> using namespace std;
int*bubbleSortDesc(int*arr,int len); int arrayofnumbers[4]; void WriteToFile(); int main() {
int number; int count=0; cout<<"Please enter (3) digits: "; char ch; while(cin>>number){ arrayofnumbers[count]=number; count++; if(count==3){ break; } }
int*ptrAsc=bubbleSortDesc(arrayofnumbers,count); cout<<" a- Display the maximum"; cout<<" b- Display the minimum"; cout<<" c- Display the numbers sorted in decreasing order"; cout<<" d- Display odd numbers"; cout<<" e- Display even numbers"; cout<<" f- Write the numbers in a file"; cin>>ch; int max=arrayofnumbers[0]; int min=arrayofnumbers[0]; switch(ch){ case 'a':
for(int i=0;i<count;i++){ if(arrayofnumbers[i]>max){ max=arrayofnumbers[i]; } } cout<<"Maximum number is "<<max; break; case 'b':
for(int i=0;i<count;i++){ if(arrayofnumbers[i]<=min){ min=arrayofnumbers[i]; } } cout<<"Minimum number is "<<min; break; case 'c': for(int i=0;i<count;i++) cout<<ptrAsc[i]<<" "; break; case 'd': cout<<"Odd number: "; for(int i=0;i<count;i++){ if(arrayofnumbers[i]%2==1){ cout<<arrayofnumbers[i]<<" "; } } break; case 'e': cout<<"Even number: "; for(int i=0;i<count;i++){ if(arrayofnumbers[i]%2==0){ cout<<arrayofnumbers[i]<<" "; } } break; case 'f': WriteToFile(); break; } cin>>number; return 0; }
Comments
Leave a comment