In: Computer Science
IN C++ ONLY... please do both parts of this task ..1 part- Menu driven program First load an array(lists) with the values 3, 4, 84, 5, 2, 47, and 7 in this order by using a function. Then sort the array(list) Use a function to display the menu. Create a program with a menu, with choices 1 for factorial (single value, which everyone will use 20!), 2 for median value , 3 for average, 4 for maximum, 5 for minimum, and 6 to end the program. Use a do loop (not while) Use a switch statement inside the loop to make your choice of 1, 2, etc. You also need to to include a screen shot of your menu on the screen in your submission. To make for a better looking output, store the function (Factorial, Median, Maximum, etc.) in a string array(list) and your answer in a numeric array(list). (two arrays) Then in the exit module (choice 6) print out your answers. Note this involves running choices 1-6 in order. AND 2.part-- Redo this by adding a menu option for standard deviation of a population. You move option 6 down to option 7 and your std dev would be option 6. As an added bonus, send the STD Deviation to a sub/module/function to be printed to the screen. Send to the file in the main module because this is where you have defined all of your file objects, and they would not exist in another module. Basic steps: For the standard deviation you have to create a 2nd array(list). I called mine deviations. This array has to be at least as large as the array for your numbers. Then walk through the first array again and subtract the average(mean) from each value in the first array and put this difference(deviation) in the 2nd array. Some of these values will be positive and some will be negative. Walk through the 2nd array again (deviations) and square them, which will make them all positive. Walk through the 2nd array again and sum the deviations. divide the deviations by the count Take the squareroot of the (deviations/count) and you get the STD Deviation. Remember, you are taking the STD Deviation of a population, so you divide by N, not n-1 where n is the sample size. When I say walk through the array, I mean go through the array from row zero to the last row.
1)
Complete Code:
#include<iostream>
#include<string.h>
using namespace std;
int fact(int num){
if(num<=0) return 1;
else return num*fact(num-1);
}
int median(int arr[], int n){
return arr[n/2];
}
int minimum(int arr[], int n){
int min = arr[0];
for(int i=0; i<n; i++){
if(arr[i]<min)
min = arr[i];
}
return min;
}
int maximum(int arr[], int n){
int max = arr[0];
for(int i=0; i<n; i++){
if(arr[i]>max)
max = arr[i];
}
return max;
}
double average(int arr[], int n){
double avg = 0.0;
for(int i=0; i<n; i++){
avg += arr[i];
}
avg /= n;
return avg;
}
int main(){
int arr[] = {3, 4, 84, 5, 2, 47, 7};
unsigned int ans[5] = {0};
string menu[] = {"Factorial","Median", "Average",
"Maximum", "Minimum"};
int n = 7, choice;
do{
cout<<"\n******* MENU
*****"<<endl;
cout<<"1.
Factorial"<<endl;
cout<<"2.
Median"<<endl;
cout<<"3.
Average"<<endl;
cout<<"4.
Maximum"<<endl;
cout<<"5.
Minimum"<<endl;
cout<<"6.
Quit"<<endl;
cout<<"Enter choice: ";
cin>>choice;
switch(choice){
case 1:
ans[0] = fact(20);
cout<<"Factorial value is:
"<<ans[0]<<endl;
break;
case 2:
ans[1] = median(arr, n);
cout<<"Median value is:
"<<ans[1]<<endl;
break;
case 3:
ans[2] = average(arr, n);
cout<<"Average value is:
"<<ans[2]<<endl;
break;
case 4:
ans[3] = maximum(arr, n);
cout<<"Maximum value is:
"<<ans[3]<<endl;
break;
case 5:
ans[4] = minimum(arr, n);
cout<<"Minimum is:
"<<ans[4]<<endl;
break;
case 6:
cout<<"******* Answers
*******"<<endl;
for(int i=0;i<5;i++)
{
cout<<"
"<<menu[i]<<": "<<ans[i]<<endl;
}
cout<<"Good bye!"<<endl;
exit(0);
break;
default:
cout<<"Invalid choice!"<<endl;
}
}while(choice!=n);
return 0;
}
OUTPUT:
2)
Complete Code:
#include<iostream>
#include<string.h>
using namespace std;
int fact(int num){
if(num<=0) return 1;
else return num*fact(num-1);
}
int median(int arr[], int n){
return arr[n/2];
}
int minimum(int arr[], int n){
int min = arr[0];
for(int i=0; i<n; i++){
if(arr[i]<min)
min = arr[i];
}
return min;
}
int maximum(int arr[], int n){
int max = arr[0];
for(int i=0; i<n; i++){
if(arr[i]>max)
max = arr[i];
}
return max;
}
double average(int arr[], int n){
double avg = 0.0;
for(int i=0; i<n; i++){
avg += arr[i];
}
avg /= n;
return avg;
}
int stdd(int arr[], int n){
int avg = average(arr, n);
int d2[n] = {0};
for(int i=0; i<n; i++){
d2[i] = arr[i]-avg;
}
for(int i=0; i<n; i++){
d2[i] = d2[i]*d2[i];
}
int sum=0;
for(int i=0; i<n; i++){
sum += d2[i];
}
return sum/n;
}
int main(){
int arr[] = {3, 4, 84, 5, 2, 47, 7};
unsigned int ans[6] = {0};
string menu[] = {"Factorial","Median", "Average",
"Maximum", "Minimum", "Standard Deviation"};
int n = 7, choice;
do{
cout<<"\n******* MENU
*****"<<endl;
cout<<"1.
Factorial"<<endl;
cout<<"2.
Median"<<endl;
cout<<"3.
Average"<<endl;
cout<<"4.
Maximum"<<endl;
cout<<"5.
Minimum"<<endl;
cout<<"6. Standard
deviation"<<endl;
cout<<"7.
Quit"<<endl;
cout<<"Enter choice: ";
cin>>choice;
switch(choice){
case 1:
ans[0] = fact(20);
cout<<"Factorial value is:
"<<ans[0]<<endl;
break;
case 2:
ans[1] = median(arr, n);
cout<<"Median value is:
"<<ans[1]<<endl;
break;
case 3:
ans[2] = average(arr, n);
cout<<"Average value is:
"<<ans[2]<<endl;
break;
case 4:
ans[3] = maximum(arr, n);
cout<<"Maximum value is:
"<<ans[3]<<endl;
break;
case 5:
ans[4] = minimum(arr, n);
cout<<"Minimum is:
"<<ans[4]<<endl;
break;
case 6:
ans[5] = stdd(arr, n);
cout<<"Minimum is:
"<<ans[5]<<endl;
break;
case 7:
cout<<"******* Answers
*******"<<endl;
for(int i=0;i<6;i++)
{
cout<<"
"<<menu[i]<<": "<<ans[i]<<endl;
}
cout<<"Good bye!"<<endl;
exit(0);
break;
default:
cout<<"Invalid choice!"<<endl;
}
}while(choice!=n);
return 0;
}
OUTPUT: