Write a program that reads one or more sets of numbers from the user. Each set o
ID: 441732 • 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 and <= 25). The values in the array will of type int. Program Flow The program starts the user must enter a value greater than 0 and less than 26. 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. this is the main program: #include #include const int MAX = 25; int main() { int myArray[MAX]; int num, choice; double mean, median; num = readNum(); fillArray(myArray, num); selectionSort(myArray, num); do { choice = menu(); if(choice == 1) { fillArray(myArray, num); selectionSort(myArray, num); }// end choice == 1 else if(choice == 2) { num = readNum(); fillArray(myArray, num); selectionSort(myArray, num); }// end choice == 2 else if(choice == 3) mean = findMean(myArray, num); else if(choice == 4) median = findMedian(myArray, num); }while(choice != 5); return 0; }Explanation / Answer
#include <iostream.h>
#include<conio.h>
const int MAX = 25;
int main()
{
int myArray[MAX];
int num, choice; double mean, median;
num = readNum();
fillArray(myArray, num);
selectionSort(myArray, num);
do {
choice = menu();
if(choice == 1)
{
fillArray(myArray, num);
selectionSort(myArray, num); }// end choice == 1
else if(choice == 2)
{
num = readNum();
fillArray(myArray, num);
selectionSort(myArray, num);
}// end choice == 2
else if(choice == 3)
mean = findMean(myArray, num);
else if(choice == 4)
median = findMedian(myArray, num);
}while(choice != 5);
return 0;
}
int readNum()
{
scanf("%d",&num);
if(num>0 && num<25)
return num;
}
void fillArray(nArray[], numb)
{
for(int i=0;i<numb;i++)
{
scanf("%d",&nyArray[i];
}
}
void selectionSort(mArray[], nu)
{
for ( int c = 0 ; c < ( nu - 1 ) ; c++ )
{
position = c;
for ( d = c + 1 ; d < nu ; d++ )
{
if ( mArray[position] > mArray[d] )
position = d;
}
if ( position != c )
{
swap = myArray[c];
mArray[c] = mArray[position];
mArray[position] = swap;
}
}
}
int findMean(kArray[], numk)
{
int Me;
for( int b=0;b<num;b++)
{
Me=Me+kArray[b];
}
return Me/numK;
}
findMedian(int nums[], int total)
{
int temp;
int i,j;
for(i=0;i<total;i++)
for(j=i+1;j<total;j++) {
if(nums[i]>nums[j]) {
temp=nums[j];
nums[j]=nums[i];
nums[i]=temp;
}
}
if(total%2==0) {
return (nums[total/2]+nums[total/2-1])/2;
}else{
return nums[total/2];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.