64 730
Assignments Done
99,3%
Successfully Done
In September 2018

Answer to Question #18473 in C++ for farouk dehbi

Question #18473
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.
Expert's answer
#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;
}

int*bubbleSortDesc(int*arr,int len)
{
int temp;

for(int i=0;i<len;i++)
{
for(int j=0;j<len-1;j++)
{
if(arr[j]<arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
return arr;
}

void WriteToFile(){
string name;
ofstream outFile; // Step #2 - Declare outstream file.
cout<<"Enter file name: ";
cin>>name;
outFile.open(name); // Step #3 - Open outFile.

if (outFile.fail()) // Check for file creation and return error.
{
cout << "Error opening "numbers.txt" for
output.
";


}else{
for(int i=0;i<3;i++){
outFile << arrayofnumbers[i] <<" ";
}

}
}




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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions