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

Write a program that will calculate statistical information on a list of integer

ID: 3861517 • Letter: W

Question

Write a program that will calculate statistical information on a list of integer numbers.

1. Your main program must call three functions: readData, calculateStats, and saveResults.

2. Function readData will prompt the user for a file that contains the data, one number per line. Reads the data into the array and return the actual number of numbers read. Do not read more than the maximum number that your program can handle. Output “Too many values, max. = #”

3. Function calculateStats will return the double average and standard deviation via call by reference parameters. The function itself does not return anything.

4. Function saveResults will prompt the user for a file name to send the output to. The required output format:

Data: filename

### ### ### ### ### ###

### ###

Number of inputs: ###

Average: #.##

Standard deviation: #.##

The data are to be printed 6 values per line.

5. Your program must be able to process up to 100 numbers. Must use partially-filled array. Only process up to this maximum numbers and ignore the rest.

6. Call calculateStats only if there are data read from the file (actualSize > 0).

7. The result output file will contain all the numbers 10 per line follows by the average and standard deviation.

8. All data must be passed between the functions. NO global variables or arrays.

Explanation / Answer

Here is the code for you:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#define MAXSIZE   100
using namespace std;

// 2. Function readData will prompt the user for a file that contains the data, one number
// per line. Reads the data into the array and return the actual number of numbers read.
// Do not read more than the maximum number that your program can handle.
// Output “Too many values, max. = #”
int readData(int array[])
{
    string fileName;
    cout<<"Enter the file name that contains data: ";
    cin>>fileName;
    ifstream fin;
    fin.open(fileName);
    int count = 0;
    // 5. Your program must be able to process up to 100 numbers. Must use partially-filled
    // array. Only process up to this maximum numbers and ignore the rest.
    while(!fin.eof() && count < MAXSIZE)
        cin>>array[count++];
    return count;  
}

// 3. Function calculateStats will return the double average and standard deviation via
// call by reference parameters. The function itself does not return anything.
void calculateStats(int array[], int size, double &avg, double &stdDev)
{
    double sum = 0;
    for(int i = 0; i < size; i++)
        sum += array[i];
    avg = sum / size;
   
    if(size == 0)
        stdDev = 0;
    sum = 0;
    for(int i = 0; i < size; i++)
        sum += pow((array[i]- avg), 2);
    stdDev = sum / size;
    stdDev = sqrt(stdDev);   
}

// 4. Function saveResults will prompt the user for a file name to send the output to.
// The required output format:
// Data: filename
// ### ### ### ### ### ###
// …
// ### ###
// Number of inputs: ###
// Average: #.##
// Standard deviation: #.##
// The data are to be printed 6 values per line.
void saveResults(int array[], int size, double avg, double stdDev)
{
    string fileName;
    cout<<"Enter the file name to send the output: ";
    cin>>fileName;
    ofstream fout;
    fout.open(fileName);
    fout << fileName << endl;
    for(int i = 0; i < size; i++)
    {
       if(i%6 == 0)
           fout << endl;
       fout << array[i]<<" ";  
    }
    fout << "Number of input: "<<size<<endl;
    fout << "Average: "<<fixed<<setprecision(2)<<avg<<endl;
    fout << "Standard Deviation: "<<fixed<<setprecision(2)<<stdDev<<endl;
}

//1. Your main program must call three functions: readData, calculateStats, and saveResults.
int main()
{
    int array[MAXSIZE], size;
    double avg = 0, stdDev = 0;
    size = readData(array);
    //6. Call calculateStats only if there are data read from the file (actualSize > 0).
    if(size > 0)
        calculateStats(array, size, avg, stdDev);
    saveResults(array, size, avg, stdDev);  
}

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