Problem Statement: The purpose of this programming project is to write a small C
ID: 670884 • Letter: P
Question
Problem Statement:
The purpose of this programming project is to write a small C++ program to perform some mathematical operations (i.e., sum, mean, min, and max) on a fixed set of five numbers (these may be integers or floating-point numbers) entered by the user. The menu and the resulting operations shall be repeatedly displayed until the user enters the option to exit the program. Your program should then print out a menu to support the following actions:
1. Sum of 5 numbers
2. Mean of 5 numbers
3. Minimum of 5 numbers
4. Maximum of 5 numbers
5. Exit
Your program may assume that the user inputs an integer in response to this menu, although the integer may be out-of-range. If the user enters an invalid menu option (i.e., not an integer between 1 and 5, inclusively), your program should display an error message and re-print the menu. If the user input is between 1 and 4, inclusively, then your program will prompt the user to enter five numbers (these may be integers or floating-point numbers). Then, based on the option selected, your program will determine (1) the sum of the 5 numbers, (2) the mean of the 5 numbers, (3) the minimum of the 5 numbers, or (4) the maximum of the 5 numbers, respectively, for the options 1 through 4, inclusively, and print out the result. After printing the result for these options, your program will reprint the menu and accept new input, thus repeating the process. If the user input is 5, you will print out a friendly message and end the program.
Some additional requirements:
• You shall define (and use) the number of numbers that you read in from the user as a constant.
• You shall read each of the five numbers input by the user individually in a loop (i.e., loop a number of times, prompting for and reading in the number only once inside the loop).
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
int option;
do{
cout<<"1. Sum of 5 numbers 2. Mean of 5 numbers 3. Minimum of 5 numbers 4. Maximum of 5 numbers 5. Exit"<<endl;
cin>>option;
if(option==1){
float a[5];float sum=0;
for(int i=0;i<5;i++){
cout<<"enter "<<i<<" element: ";
cin>>a[i];
sum+=a[i];
}
cout<<"sum of 5 numbers: "<<sum<<endl;
}else if(option==2){
float a[5];float avg=0;
for(int i=0;i<5;i++){
cout<<"enter "<<i<<" element: ";
cin>>a[i];
avg+=a[i];
}
avg=avg/5;
cout<<"mean of 5 numbers: "<<avg<<endl;
}else if(option==3){
float a[5];float min=0;
for(int i=0;i<5;i++){
cout<<"enter "<<i<<" element: ";
cin>>a[i];
}
min=a[0];
for(int i=0;i<5;i++){
if(min>a[i]){
min=a[i];
}
}
cout<<"minimum of 5 numbers: "<<min<<endl;
}else if(option==4){
float a[5];float max=0;
for(int i=0;i<5;i++){
cout<<"enter "<<i<<" element: ";
cin>>a[i];
}
max=a[0];
for(int i=0;i<5;i++){
if(max<a[i]){
max=a[i];
}
}
cout<<"maximum of 5 numbers: "<<max<<endl;
}else if(option==5){
break;
}else{
cout<<"enter a valid option!!!!"<<endl;
}
}while(option!=5);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.