Answer to Question #56471 in C++ for Nithya

Question #56471
Write a program that asks the user to enter 3 numbers then 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
2015-11-20T03:55:25-0500
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
double number;
vector<double> vect;
for(int i=0; i<3; i++) {
cin>>number;
vect.push_back(number);
}

cout<<"a- Display the maximum "<<endl;
cout<<"b- Display the minimum "<<endl;
cout<<"c- Display the numbers sorted in decreasing order "<<endl;
cout<<"d- Display odd numbers "<<endl;
cout<<"e- Display even numbers "<<endl;
cout<<"f- Write the numbers in a file. \n";
char choise;
cin>>choise;

sort(vect.rbegin(), vect.rend());
cout<<setprecision(2)<<fixed;

switch(choise) {
case 'a':
cout<<*vect.begin()<<endl;
break;
case 'b':
cout<<*(vect.end()-1)<<endl;
break;
case 'c':
cout<<vect[0]<<" "<<vect[1]<<" "<<vect[2]<<endl;
break;
case 'd':
for(int i=0;i<3;i++) {
if((int)vect[i]%2==1) cout<<vect[i]<<" ";
}
cout<<endl;
break;
case 'e':
for(int i=0;i<3;i++) {
if((int)vect[i]%2==0) cout<<vect[i]<<" ";
}
cout<<endl;
break;
case 'f':
char name[80];
cin>>name;
FILE *file=fopen(name,"w");
fprintf(file,"%.2f %.2f %.2f",vect[0], vect[1], vect[2]);
fclose(file);
break;
}

return 0;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS