Program is C I don\'t understand the array portion at least how to be able to ad
ID: 3916803 • Letter: P
Question
Program is C I don't understand the array portion at least how to be able to add elements to an array
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 Display the median 1. 2. 3. 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 followedExplanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
//Code
#include<stdio.h>
//method to display the menu
void showMenu(){
printf(" 1. Add a number to the array ");
printf("2. Display the mean ");
printf("3. Display the median ");
printf("4. Quit ");
printf("Enter your choice: ");
}
//method to get a valid positive integer from user
int getInput(){
int input=-1;
//looping until a positive enter is entered by the user
while(input<0){
printf("Enter the number to be added: ");
scanf("%d",&input);
if(input<0){
printf("Please enter a positive number ");
}
}
return input;
}
//method to find the mean of an array with 'numElements' number of elements
double findMean(int* array, int numElements){
//if array is empty, returning 0
if(numElements==0){
return 0;
}
//finding the sum total
int total=0;
for(int i=0;i<numElements;i++){
total+=array[i];
}
//finding the mean
double mean=(double)total/numElements;
return mean;
}
//method to sort the array (important for finding median)
void sort(int* array, int numElements){
for(int i=0;i<numElements;i++){
for(int j=0;j<numElements-1;j++){
if(array[j]>array[j+1]){
//swapping
int temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
}
//method to find the median of an array
double findMedian(int* array, int numElements){
if(numElements==0){
//empty array
return 0;
}
//in order to find the median, we have to make sure that the array is sorted
sort(array,numElements);
//if number of elements is odd, median is the middle element (n-1)
if(numElements%2!=0){
return array[numElements/2];
}else{
//otherwise, median is the average of two elements at the middle
int a=array[(numElements-1)/2];
int b=array[(numElements)/2];
double median=(a+b)/2.0;
return median;
}
}
int main(){
//defining all required variables
int MAX=20; //maximum number of elements
int array[MAX]; //array
int count=0; //current number of elements
int choice=0,input;
double m;
//looping until user quit
do{
//displaying the menu
showMenu();
scanf("%d",&choice);
//performing actions based on input
switch(choice){
case 1:
if(count==MAX){
printf("Array is full! ");
}else{
input=getInput();
array[count]=input;
count++;
printf("Added! ");
}
break;
case 2:
m=findMean(array,count);
printf("Mean: %.2f",m);
break;
case 3:
m=findMedian(array,count);
printf("Median: %.2f",m);
break;
case 4:
printf("Bye! ");
break;
default: printf("Invalid choice! ");
}
}while(choice!=4);
}
/*OUTPUT*/
1. Add a number to the array
2. Display the mean
3. Display the median
4. Quit
Enter your choice: 1
Enter the number to be added: 12
Added!
1. Add a number to the array
2. Display the mean
3. Display the median
4. Quit
Enter your choice: 1
Enter the number to be added: 66
Added!
1. Add a number to the array
2. Display the mean
3. Display the median
4. Quit
Enter your choice: 1
Enter the number to be added: 32
Added!
1. Add a number to the array
2. Display the mean
3. Display the median
4. Quit
Enter your choice: 2
Mean: 36.67
1. Add a number to the array
2. Display the mean
3. Display the median
4. Quit
Enter your choice: 3
Median: 32.00
1. Add a number to the array
2. Display the mean
3. Display the median
4. Quit
Enter your choice: 1
Enter the number to be added: -55
Please enter a positive number
Enter the number to be added: -3
Please enter a positive number
Enter the number to be added: 6
Added!
1. Add a number to the array
2. Display the mean
3. Display the median
4. Quit
Enter your choice: 2
Mean: 29.00
1. Add a number to the array
2. Display the mean
3. Display the median
4. Quit
Enter your choice: 4
Bye!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.