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

Create a project that computes BOTH the mean and median of a sequence of integer

ID: 3542410 • Letter: C

Question

Create a project that computes BOTH the mean and median of a sequence of integers received via standard input.

The project MUST contain the following files: medianCalculator.h, medianCalculator.cpp, and medianCalculatorDriver.cpp.

The number of integers input by the user is not known in advance, except that it will not exceed 100. Numbers are input through standard input, either from keyboard or file re-direct. The program should read numbers until a non-digit or end-of-file is encountered or 100 numbers have been read.

Once the input numbers have been read, the program should calculate the mean and median and then report these values to standard output.

The source code should be structured as follows:



  1. Implement separate functions with the following prototypes:

     float Mean   (const int* array, size_t size); // calculates mean of data in array

     float Median (int* array, size_t size);       // calculates median of data in array

     void  Sort   (int* array, size_t size);       // sorts the data in array

  2. I/O is handled by function main(); no other functions should do any I/O

  3. Function main() calls Mean() and Median()

  4. Function Median() calls Sort()

The source code should be organized as follows:

  1. Prototypes for Mean, Median, and Sort should be in file stats.h

2. Implementations for Mean, Median, and Sort should be in file stats.cpp

  3. Function main should be in file main.cpp

The Sort() function should implement the Insertion Sort algorithm.


The data input loop should not be interrupted by prompts for a next datum - this will make file redirect cumbersome. Just ask for the data one time, then read until a non-digit or end of file is encountered.

Explanation / Answer

// save in stats.h file

float Mean (const int* array, size_t size); // calculates mean of data in array
float Median (int* array, size_t size); // calculates median of data in array
void Sort (int* array, size_t size); // sorts the data in array

// SAVE IN stats.cpp file

#include "stats.h"

float Mean (const int* array, size_t size) // calculates mean of data in array
{
float sum =0;
for(int i=0; i<size; i++)
sum = sum + array[i];
return (sum/size);
}
float Median (int* array, size_t size) // calculates median of data in array
{
Sort(array,size);
if(size%2!=0) return array[size/2];
int temp = size/2;
return static_cast<float> (array[temp]+array[temp-1])/2.0;
}
void Sort (int* array, size_t size) // sorts the data in array
{
    int j;
   for (int i = 1; i <size; i++)
    {
        int temp = array[i];
        for (j = i-1; j >= 0 && array[j]>temp; j--)
        array[j+1] = array[j];
        array[j+1] = temp;
    }
}

// save in main.cpp file.

#include<iostream>
#include "stats.h"
using namespace std;
int main()
{
    int array[100];
    int index = 0; // to keep track of no of input elements.
    // start reading input from key board;
    while(true)
    {
        int temp;
        cout <<"Enter integer " << (index+1) << " : ";
        cin >> temp;
        if(cin.fail()) break;
        else
        array[index] = temp;
        index++;
    }
    cout << "Mean of array given by " << Mean(array,index) << endl;
    cout << "Median of array given by " << Median(array,index) << endl;
    system("pause");
    return 0;
}

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