Question
Write a program called ex10_2.cpp that asks users to input up to 20 integers, and then finds the maximum, minimum, average, and median of the
Write a program called ex10_2.cpp that asks users to input up to 20 integers, and then finds the maximum, minimum, average, and median of the numbers that were entered.
Median is the number that appears in the middle of the sorted list of numbers. If the array has an odd number of elements, median is a single number in the middle of the list (array). If the array has an even number of elements, then median is the average of the two numbers in the middle.
Example: Odd number of elements: 1 4 6 3 8, first sort the numbers: 1 3 4 6 8, then find the median, i.e, 4. Even number of elements: 1 4 8 3, first sort the numbers: 1 3 4 8, then find the median as the average of 3 and 4, i.e., 3.5
To find the smallest element in an array, you only need to find the index of the smallest number. You can use the following function to do this. This function is also used in the sort_array function.
int index_of_smallest(const int a[], int start_index, int user_size) { int min = a[start_index], index_of_min = start_index; for(int i = start_index+1; i < used_size; i++) if(a[i] < min ) { min = a[i]; index_of_min = i; } return index_of_min; }
To sort an array that has used_size elements, use the following function:
void sort_array(int a[], int used_size) { int index_of_next_smallest; int temp;
for(int i = 0; i < used_size-1; i++) { index_of_next_smallest = index_of_smallest(a, index, used_size); // swap two elements temp = a[i]; a[i] = a[index_of_next_smallest]; a[index_of_next_smallest] = a[i]; } }
Note that you can write three more functions; 1) one similar to the one that finds the index of smallest number for finding the index of the largest number, 2) one that computes the average and returns it to the main, 3) and the last one that takes the sorted array and will return the median.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started