C++ Assignment (Mean, Median, Mode)

I need help with this assignment.

Given the following input file: (USE THIS FILE)

4 9 7 6 7 6 9 4 5 3 4 8 9 4 9 5 8 4 3 2 3 6 8 8 2 6 1 4 2 3 2 3 1 7 6 7 1 4 8 6
6 3 2 5 4 1 7 2 2 1 1 4 6 8 1 6 4 1 7 3 4 7 5 4 4 2 8 4 3 7 9 8 7 1 1 8 8 8 7 1
8 7 2 2 6 2 7 7 3 3 1 4 9 3 5 1 2 1 4

Determine mean, median, mode, sample variance, sample standard deviation, minimum value, and maximum value.

Use at least six (6) programmer-defined functions; this excludes sort. At least three (3) must be void. Display unsorted array, sorted array, mean, median, minimum value, maximum value, mode (response frequency/histogram), sample variance and sample standard deviation. All output should be displayed in the main program with the exception of mode (response/frequency histogram).

User should be prompted for input file name. Only 40 values of sorted and unsorted arrays should be printed on one line. Please use a constant for size of array. ( const int SIZE=99)
You'd get more responses if you told us specifically what you were having trouble with, instead of just posting assignment text.

Break the assignment into parts that you can understand one at a time.
Given the following input file: (USE THIS FILE)

Do you know how to open files in C++?
Please see: http://www.cplusplus.com/doc/tutorial/files/

use at least 6 functions

You will have to define some functions to use.
Please see: http://www.cplusplus.com/doc/tutorial/functions/

User should be prompted for input file name.

See: http://www.cplusplus.com/doc/tutorial/basic_io/ (input section)

arrays

For working with arrays, see: http://www.cplusplus.com/doc/tutorial/arrays/

For example, here's how I could open a file and loop through every space-delimited number in it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Example program
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    // test.txt might contain something like: 10 20 30 40 50 60
    std::ifstream file("test.txt");
    
    int my_int; // variable to store a number in
    while (file >> my_int) // loop through every number in the file
    {
        std::cout << my_int << ", "; // print each number we find in the file
    }
    std::cout << std::endl;
}


http://www.cplusplus.com/doc/tutorial/
I am having trouble with making a function to find variance and the response frequency.
To calculate sample variance, use the formula here: http://mathworld.wolfram.com/SampleVariance.html

You have to loop through each element in the array of elements, and the on each number, subtract the mean, and then square the result. Finally, divide by the number of elements.

Here's some code as a guide:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
double sample_variance(int arr[], int size)
{
    double mean = average(arr, size); // you would make this function
    double sum = 0.0;
    for (int i = 0; i < size; i++)
    {
        int value = arr[i];
        double shifted = value - mean;
        double shifted_squared = shifted * shifted;
        sum += shifted_squared;
    }
    double svar = sum / size;
    return svar;
}
Last edited on
Thank you! That helped out a lot.
Topic archived. No new replies allowed.