Answer to Question #19297 in C++ for Fatima

Question #19297
1- Write a program that asks the user to enter 3 numbers than displays the following menu:
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-26T08:58:52-0500

#include <iostream>
#include <fstream>
using namespace std;

void bubbleSort();
int arrayofnumbers[3];
int main()
{
char ch=' ';
for(int i=0;i<3;i++){
printf("Enter number %d: ",i+1);
scanf("%d",&arrayofnumbers[i]);
}
do{
printf("
c - Display the numbers sorted in decreasing order
");
printf("d - Display odd numbers
");
printf("e - Display even numbers
");
printf("f - Write the numbers in a file.
");
printf("q - Exit
");
printf("Select one item: ");
ch=getchar();
ch=getchar();
switch(ch){
case 'c':
bubbleSort();
printf("The numbers sorted in decreasing order: ");
for(int i=2;i>=0;i--){
printf("%d,",arrayofnumbers[i]);
}
break;
case 'd':
printf("Odd numbers: ");
for(int i=0;i<3;i++){
if(arrayofnumbers[i]%2==1){
printf("%d,",arrayofnumbers[i]);
}
}
break;
case 'e':

printf("Even numbers: ");
for(int i=0;i<3;i++){
if(arrayofnumbers[i]%2==0){
printf("%d,",arrayofnumbers[i]);
}
}
break;
case 'f':

ofstream outputFile("numbers.txt");
for(int i=0;i<3;i++){
outputFile <<arrayofnumbers[i]<<",";
}
break;
}
}
while(ch!='q');
int k;
scanf("%d",&k);
return 0;
}



void bubbleSort()
{
int i, j, temp;

for (i = (2); i > 0; i--)
{
for (j = 1; j <= i; j++)
{
if (arrayofnumbers[j-1] > arrayofnumbers[j])
{
temp = arrayofnumbers[j-1];
arrayofnumbers[j-1] = arrayofnumbers[j];
arrayofnumbers[j] = temp;
}
}
}
}

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
APPROVED BY CLIENTS