c language only Create a menu-driven program that will accept a list of non-nega
ID: 3915256 • Letter: C
Question
c language only
Create a menu-driven program that will accept a list of non-negative integers from the keyboard, calculate the mean and median values and display those values on the screen. Your menu should have 4 options Add a number to the array (list) Display the mean 1. 2. 3. Display the median 4. Quit Program particulars: Use an integer array to store the integers entered by the user. There must be error checking on the input integer. If it is negative, the program will print an error message and re-prompt. This process will continue until a non-negative integer is entered. There must be error checking on the menu choice entered. If the user enters a choice not on the menu, the program will print an error message, re-display the menu and re-prompt. This process will continue until valid input is entered. Your solution must be modular. The design of your functions is up to you, but the rules of highly cohesive" and "loosely coupled" must be followed.Explanation / Answer
#include<stdio.h>
//method to sort array
void Sort(int a[],int n)
{
int i=0,j=0,k;
while(i<n)
{
j=0;
while(j<n)
{
if(a[i]<a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
j++;
}
i++;
}
}
//method to calculate mean
double Mean(int a[], int n)
{
int sum = 0;
int i;
for (i = 0; i < n; i++)
sum += a[i];
return (double)sum/(double)n;
}
//method to calculate mean
double Median(int a[], int n)
{
//inorder to find median we need to sort array....
//sorting array....
Sort(a,n);
//finding mid element
if (n % 2 != 0)
return (double)a[n/2];
//finding average of mid two elements
return (double)(a[(n-1)/2] + a[n/2])/2.0;
}
// Driver program
int main()
{
int a[10000];
int n=0;
int c;
while(1)
{
printf(" 1. Add a number to the array(list): ");
printf("2. Display the mean ");
printf("3. Display the median ");
printf("4. Quit ");
printf("Enter ur choice: ");
scanf("%d",&c);
if(c==1)
{
while(1)
{
printf("Enter a non negative number:");
scanf("%d",&c);
if(c>=0)break;
else//printing error message
printf("Error...enter a non negative number... ");
}
a[n]=c;//adding to array
n++;
}
else if(c==2)
{
//calling method to display mean
printf(" The Mean is: %lf ",Mean(a,n));
}
else if(c==3)
{
//calling method to display meadian
printf(" The Median is: %lf ",Median(a,n));
}
else if(c==4)
break;
else//printing error message
{
printf(" Error: Enter correct choice ");
}
}
return 0;
}
output:
1. Add a number to the array(list):
2. Display the mean
3. Display the median
4. Quit
Enter ur choice:
1
Enter a non negative number:1
1. Add a number to the array(list):
2. Display the mean
3. Display the median
4. Quit
Enter ur choice:
1
Enter a non negative number:-1
Error...enter a non negative number...
Enter a non negative number:3
1. Add a number to the array(list):
2. Display the mean
3. Display the median
4. Quit
Enter ur choice:
1
Enter a non negative number:4
1. Add a number to the array(list):
2. Display the mean
3. Display the median
4. Quit
Enter ur choice:
1
Enter a non negative number:2
1. Add a number to the array(list):
2. Display the mean
3. Display the median
4. Quit
Enter ur choice:
1
Enter a non negative number:7
1. Add a number to the array(list):
2. Display the mean
3. Display the median
4. Quit
Enter ur choice:
1
Enter a non negative number:5
1. Add a number to the array(list):
2. Display the mean
3. Display the median
4. Quit
Enter ur choice:
1
Enter a non negative number:8
1. Add a number to the array(list):
2. Display the mean
3. Display the median
4. Quit
Enter ur choice:
5
Error: Enter correct choice
1. Add a number to the array(list):
2. Display the mean
3. Display the median
4. Quit
Enter ur choice:
1
Enter a non negative number:6
1. Add a number to the array(list):
2. Display the mean
3. Display the median
4. Quit
Enter ur choice:
2
The Mean is: 4.500000
1. Add a number to the array(list):
2. Display the mean
3. Display the median
4. Quit
Enter ur choice:
3
The Median is: 4.500000
1. Add a number to the array(list):
2. Display the mean
3. Display the median
4. Quit
Enter ur choice:
4
Process exited normally.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.