In: Computer Science
Exercise 9.2 (c++) (max, min, average, and median code)
Write a program that asks users to input up to 20 integers, and then finds the maximum, minimum, average, and median of the numbers that were entered. Use the following information to write your program. The 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
The minimum is the smallest element of the array. To find the smallest element in an array, you need to find the index of the smallest number and access it. 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
used_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, i,
used_size);
// swap two elements
temp = a[i];
a[i] = a[index_of_next_smallest];
a[index_of_next_smallest] = temp;
}
}
Note that you should 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. Call your programex92.cpp.
use c++ and #include <iostream>
C++ Program:
#include<iostream>
using namespace std;
int index_of_smallest(const int a[], int start_index, int
used_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;
}
int index_of_largest(const int a[], int start_index, int
used_size)
{
int max = a[start_index], index_of_max = start_index;
for(int i = start_index+1; i < used_size; i++)
{
if(a[i] > max )
{
max = a[i];
index_of_max = i;
}
}
return index_of_max;
}
double average(int a[], int used_size){
double sum =0;
for(int i=0;i<used_size;i++)
sum += a[i];
return sum/used_size;
}
double median(int sort_a[],int used_size){
if(used_size%2!=0)
return sort_a[used_size/2];
else
return (sort_a[used_size/2] + sort_a[(used_size/2)-1])/2;
}
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, i, used_size);
// swap two elements
temp = a[i];
a[i] = a[index_of_next_smallest];
a[index_of_next_smallest] = temp;
}
}
int main(){
int arr[100];
cout<<"Enter the 20 integers:\n";
for(int i=0;i<20;i++)
cin>>arr[i];
cout<<"The minimum of 20 integers is
"<<arr[index_of_smallest(arr,0,20)]<<"\n";
cout<<"The maximum of 20 integers is
"<<arr[index_of_largest(arr,0,20)]<<"\n";
cout<<"The average of 20 integers is
"<<average(arr,20)<<"\n";
sort_array(arr,20);
cout<<"The median of 20 integers is
"<<median(arr,20)<<"\n";
return 0;
}
Output: