Question

In: Computer Science

Exercise 9.2 (c++) (max, min, average, and median code) Write a program that asks users to...

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>

Solutions

Expert Solution

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:


Related Solutions

Write a C program/code that prompts the user for a minimum min and a maximum max....
Write a C program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
Write a program/code that prompts the user for a minimum min and a maximum max. Then...
Write a program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. (WRITTEN IN C) For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
Write a Python program that computes certain values such as sum, product, max, min and average...
Write a Python program that computes certain values such as sum, product, max, min and average of any 5 given numbers along with the following requirements. Define a function that takes 5 numbers, calculates and returns the sum of the numbers. Define a function that takes 5 numbers, calculates and returns the product of the numbers. Define a function that takes 5 numbers, calculates and returns the average of the numbers. Must use the function you defined earlier to find...
c++ Write a program that print stars, Max and Min values. It should use the following...
c++ Write a program that print stars, Max and Min values. It should use the following functions: (2 pts) int getNum ( ) should ask the user for a number and return the number. This function should be called by main once for each number to be entered. Input Validation: Do not accept numbers less than -100. (2 pts) void printStars ( int n ) should print n number of stars. If n is less than 0, display "Invalid" message...
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in...
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in a number greater than or equal to zero and less than or equal to 100. If they do not you should alert them and end the program. Next, determine the letter grade associated with the number. For example, A is any grade between 90 and 100. Report the letter grade to the user.
Write a program in Java that asks users to enter a series of marks (one at...
Write a program in Java that asks users to enter a series of marks (one at a time) until a signal is entered to stop. Have your program ensure that each mark entered is between 0 and 100. When done, output the number of marks entered and the average of all marks. Also output the highest mark and the lowest mark, and the range between the highest and lowest mark.
Write JAVA program that finds 3 students with the best scores. The program asks users for...
Write JAVA program that finds 3 students with the best scores. The program asks users for scores of 5 students. The program prints the first, second, third place students and scores. You can assume that there is no two students with the same score. <EXAMPLE> enter the score of each student score of student 1: 50 score of student 2: 70 score of student 3: 30 score of student 4: 90 score of student 5: 40 1st place is student...
For this C++ program, Write and modify the code to compute and display the class average...
For this C++ program, Write and modify the code to compute and display the class average as well as the standard deviation. Your code changes are as follows: 1. The variable “double grade” should be replaced by a two-dimensional array variable “double grade[NUMSTUDENTS][NUMGRADES].” Also replace the variable “double average” by “double average[NUMSTUDENTS].” This is necessary since you need to save the entered grades specially to compute the standard deviations. 2. After completing the display of the average grade of all...
CODE IN C++ PLEASE Write a program to implement the algorithm that you designed in Exercise...
CODE IN C++ PLEASE Write a program to implement the algorithm that you designed in Exercise 19 of Chapter 1. Your program should allow the user to buy as many items as the user desires. Instructions for Exercise 19 of Chapter 1 have been posted below for your convenience. Exercise 19 Jason typically uses the Internet to buy various items. If the total cost of the items ordered, at one time, is $200 or more, then the shipping and handling...
This is the program that use to find the Mean (Average) and Median in C++. #include...
This is the program that use to find the Mean (Average) and Median in C++. #include <iostream> #include <fstream> #include <iomanip> #include <cstdlib> // used by the exit() functiona using namespace std; int main(int argc, char* argv[]) { // variables to control the disk file ifstream infile; char filename[200]; int recordCount = 0; int recordsToSkip = 0; // variables for fields of each record in the file int AcctNo = 0; char Name[100] = ""; double AcctBal = 0.0; //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT