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

please help, c++ please explain the process when reading in from the files. The

ID: 3716908 • Letter: P

Question

please help, c++
please explain the process when reading in from the files.

The objective of this Program is to process and determine statistics for several different data sets, as well as process and determine statistics for a data set formed by combining all the input data sets into one data set. This program is a required program for the course. The score on this program will not be dropped when calculating the final course grade. The program will input the individual data sets from different files, process each data set in order, and will save the data values to form a combined data set. The combined data set will then be processed in the same manner as the individual data sets For each data set input to the program, as well as for the combined data set, the following information will be printed: the unsorted data, as input to the program or as saved into the array for the combined data set the sorted data, from smallest to largest values the mean of the data set the median of the data set the mode of the data set, including cases of bimodal data You should use a loop to process the data sets, using the same array repeatedly for each input data set. As you complete the processing of each array, move the data into another array that will hold-all of the combined data. You should not use a separate array for each data set. You may use vectors instead of arrays if you choose. Do not combine the data sets into one file. Do not make a fourth file containing the combined data set. Do not re-read the values from the files when forming the array of combined values. You may design your program to expect any data set will be no larger than 100 elements, and thus the combined data set would be no greater than 300 elements. Potentially your program could process several different data sets, though you will only use the 3 sets provided for your submission. Design your program for generality: The code should not need to be changed if you were asked to run this program with 4 different data sets, or with 10 different data sets, or even 100 different data sets (except maybe change the dimensions of the arrays used).

Explanation / Answer

#include <vector>

#include <iostream>

#include <string>

#include <fstream>

#include <algorithm>

using namespace std;

// Asking the user to enter a file name and read data from it

vector<double> ReadDataFromFile()

{

string fileName;

cout << "Enter filename: ";

cin >> fileName;

ifstream inFile(fileName);

if (!inFile)

{

cerr << "unable to open the file.";

exit(1);

}

vector<double> data;

double value;

while (inFile >> value)

{

if (value == -999)

break;

data.emplace_back(value);

}

return data;

}

// Print array

void PrintArray(const vector<double>& data)

{

for (double val : data)

{

cout << val << endl;

}

}

// Sort the array and return

vector<double> SortArray(vector<double> data)

{

sort(data.begin(), data.end());

return data;

}

// Calculate and return the mean of the data

double CalculateMean(const vector<double>& data)

{

double sum = 0;

for (double val : data)

{

sum += val;

}

return sum / data.size();

}

// Calculate and return the median of the data

double CalculateMedian(const vector<double>& data)

{

// Need to sort the data to calculate the median

auto sortedData = SortArray(data);

if (data.size() % 2 == 0)

{

// Sum middle two numbers and return the avergae of them

size_t index = data.size() / 2;

return (sortedData[index - 1] + sortedData[index]) / 2;

}

else

return sortedData[data.size() / 2];

}

// Calculate and return the mode of the data

double CalculateMode(const vector<double>& data)

{

auto sortedData = SortArray(data);

double number = sortedData[0];

double mode = number;

int count = 1;

int countMode = 1;

for (int i = 1; i < sortedData.size(); i++)

{

if (sortedData[i] == number)

{

// count occurrences of the current number

++count;

}

else

{

// now this is a different number

if (count > countMode)

{

countMode = count; // mode is the biggest ocurrences

mode = number;

}

count = 1; // reset count for the new number

number = sortedData[i];

}

}

return mode;

}

int main()

{

vector<double> combinedData;

// Reading data from three different files

auto dataSet1 = ReadDataFromFile();

//Print Unsorted data set 1

PrintArray(dataSet1);

PrintArray(SortArray(dataSet1));

cout << "Mean of data set 1: " << CalculateMean(dataSet1);

cout << "Median of data set 1: " << CalculateMedian(dataSet1);

cout << "Mode of data set 1: " << CalculateMode(dataSet1);

//addding the data set to combined data set

combinedData = dataSet1;

//Print sorted data set 1

auto dataSet2 = ReadDataFromFile();

//Print Unsorted data set 1

PrintArray(dataSet2);

// Print sorted data set 2

PrintArray(SortArray(dataSet2));

cout << "Mean of data set 2: " << CalculateMean(dataSet2);

cout << "Median of data set 2: " << CalculateMedian(dataSet2);

cout << "Mode of data set 2: " << CalculateMode(dataSet2);

//appending data set 2 to combined data set

combinedData.insert(combinedData.end(), dataSet2.begin(), dataSet2.end());

auto dataSet3 = ReadDataFromFile();

//Print Unsorted data set 3

PrintArray(dataSet3);

// print sorted data set 3

PrintArray(SortArray(dataSet3));

cout << "Mean of data set 3: " << CalculateMean(dataSet3);

cout << "Median of data set 3: " << CalculateMedian(dataSet3);

cout << "Mode of data set 3: " << CalculateMode(dataSet3);

//appending data set 3 to combined data set

combinedData.insert(combinedData.end(), dataSet3.begin(), dataSet3.end());

//Print Unsorted combined data set

PrintArray(combinedData);

// print sorted combined data set

PrintArray(SortArray(combinedData));

cout << "Mean of combined data set: " << CalculateMean(combinedData);

cout << "Median of combined data set: " << CalculateMedian(combinedData);

cout << "Mode of combined data set: " << CalculateMode(combinedData);

return 0;

}