Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads one or more sets of numbers from the user. Each set o

ID: 3529347 • Letter: W

Question

Write a program that reads one or more sets of numbers from the user. Each set of numbers consists of an integer N followed by N integer values (N is guaranteed [by you] to be > 0). The values in the array will of type int. Program Flow The program starts the user must enter a value greater than 0. You must ensure this. The user will then enter that many values. Display a menu that has the following options 1) Change the values in the array. 2) Change N and the values in the array 3) Find and display the mean 4) Find and display the median 5) Quit Definitions for purposes of this problem: Mean is defined as the result of dividing the sum of the N numbers by N, the number of numbers. Median is the "middle" number. That is, in an ordered list of numbers, the one in the middle of the list is the median. If N is even, then the mean of the two numbers in the middle is the median. Use the following as the main function: #include #include int main() { int *myArray = NULL; int num, choice; double mean, median; num = readNum(); myArray = fillArray(num); selectionSort(myArray, num); do { choice = menu(); if(choice == 1) { changeArray(myArray, num); selectionSort(myArray, num); }// end choice == 1 else if(choice == 2) { free(myArray); num = readNum(); myArray = fillArray(num); selectionSort(myArray, num); }// end choice == 2 else if(choice == 3) findMean(myArray, num); else if(choice == 4) findMedian(myArray, num); }while(choice != 5); free(myArray); return 0; }// end main

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
int readNum()
{
int t;
while(1)
{
printf("Enter the number: ");
scanf("%d",&t);
if(t>0)
break;
else
printf("Wrong number,enter again ");
}
return t;
}

int* fillArray(int num)
{
int *temp = NULL;
int i,t;
temp = (int*)malloc(sizeof(int)*num);
for(i=0;i<num;i++)
{
printf("Enter #%d: ",(i+1));
scanf("%d",&t);
temp[i] = t;
}
return temp;
}

void selectionSort(int *myArray,int num)
{
int i,j;
int temp;
int t;
for(i=0;i<num;i++)
{
temp = myArray[i];
for(j=1;j<num;j++)
{
if(temp>myArray[j])
{
t = myArray[i];
myArray[i] = myArray[j];
myArray[j] = t;
}
}
}
}

int menu()
{
int ch;
while(1)
{
printf(" 1. Change the values in the array");
printf(" 2. Change N and the values in the array");
printf(" 3. Find and display the mean");
printf(" 4. Find and display the median");
printf(" 5. Quit ");
printf(" Enter your choice: ");
scanf("%d",&ch);
if(ch < 1 && ch >5)
printf("Wrong choice,enter again");
else
break;
}
return ch;
}

void changeArray(int *myArray,int num)
{
int loc,val;
printf(" Enter the location for changing value: ");
scanf("%d",&loc);
printf(" Enter the value for that location: ");
scanf("%d",&val);

if(loc >= 0 && loc<num)
{
myArray[loc] = val;
printf(" Value Changed");
}
else
printf(" Value not Changed");
}

void findMean(int *myArray,int num)
{
int i,sum=0;
float mean;
for(i=0;i<num;i++)
{
sum = sum + myArray[i];
}
mean = (float)sum/num;
printf(" Mean is: %f",mean);
}
void findMedian(int *myArray,int num)
{
int i;
float t;
if(num%2 != 0)
{
i = num/2;
printf("Median is: %d",myArray[i]);
}
else
{
i = num/2;
float t = (myArray[i] + myArray[i-1])/2;
printf("Median is: %f",t);
}
}
int main()
{
int *myArray = NULL;
int num, choice;
double mean, median;
num = readNum();
myArray = fillArray(num);
selectionSort(myArray, num);
do
{
choice = menu();
if(choice == 1)
{
changeArray(myArray, num);
selectionSort(myArray, num);
}
// end choice == 1
else if(choice == 2)
{
free(myArray);
num = readNum();
myArray = fillArray(num);
selectionSort(myArray, num);
}
// end choice == 2
else if(choice == 3)
findMean(myArray, num);
else if(choice == 4)
findMedian(myArray, num);
}
while(choice != 5);
free(myArray);
return 0;
}// end main

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote