asked 99.6k views
4 votes
Write a program to help them analyze their data. Your program must contain parallel arrays: an array to store the names of the runners and a two-dimensional array of five rows and seven columns to store the number of miles run by each runner each day. Furthermore, your program must contain at least the following functions: - a function to read and store the runners' names and the numbers of miles run each day; - a function to calculate the average number of miles run each day; - and a function to output the results. (You may assume that the input data is stored in a file and each line of data is in the following form: runnerName milesDay 1 milesDay 2 milesDay 3 milesDay4 milesDay5 milesDay6 milesDay7.) \begin{tabular}l ch8_Ex12Data.txt & & \multicolumn{4}{c}{ main.cpp } \\ \hline 1 & Jason & 10 & 15 & 20 & 25 & 18 & 20 & 26 \\ 2 & Samantha & 15 & 18 & 29 & 16 & 26 & 20 & 23 \\ 3 & Ravi & 20 & 26 & 18 & 29 & 10 & 12 & 20 \\ 4 & Sheila & 17 & 20 & 15 & 26 & 18 & 25 & 12 \\ 5 & Ankit & 16 & 8 & 28 & 20 & 11 & 25 & 21 \end{tabular} Expected Output (3) Name Day 1 Day 2 Day 3 Day 4 Day 5 Jason 10.00 15.00 20.00 25.00 18.00 20.0 Ravi 20.0026.0018.0029.0010.0012.0

asked
User MRHwick
by
8.7k points

1 Answer

6 votes



C++


#include <iostream>

#include <fstream>

#include <iomanip>

const int NUM_RUNNERS = 5;

const int NUM_DAYS = 7;

// Function to read and store runner names and miles run each day

void readData(std::string runnerNames[], double milesRun[][NUM_DAYS], std::ifstream& inputFile) {

for (int i = 0; i < NUM_RUNNERS; i++) {

inputFile >> runnerNames[i];

for (int j = 0; j < NUM_DAYS; j++) {

inputFile >> milesRun[i][j];

}

}

}

// Function to calculate the average miles run each day

void calculateAverage(double milesRun[][NUM_DAYS], double averages[]) {

for (int j = 0; j < NUM_DAYS; j++) {

double sum = 0.0;

for (int i = 0; i < NUM_RUNNERS; i++) {

sum += milesRun[i][j];

}

averages[j] = sum / NUM_RUNNERS;

}

}

// Function to output the results

void outputResults(std::string runnerNames[], double milesRun[][NUM_DAYS], double averages[]) {

std::cout << std::setw(10) << "Name";

for (int j = 0; j < NUM_DAYS; j++) {

std::cout << std::setw(8) << "Day " << j + 1;

}

std::cout << std::endl;

for (int i = 0; i < NUM_RUNNERS; i++) {

std::cout << std::setw(10) << runnerNames[i];

for (int j = 0; j < NUM_DAYS; j++) {

std::cout << std::setw(8) << std::fixed << std::setprecision(2) << milesRun[i][j];

}

std::cout << std::endl;

}

std::cout << std::setw(10) << "Average";

for (int j = 0; j < NUM_DAYS; j++) {

std::cout << std::setw(8) << std::fixed << std::setprecision(2) << averages[j];

}

std::cout << std::endl;

}

int main() {

std::string runnerNames[NUM_RUNNERS];

double milesRun[NUM_RUNNERS][NUM_DAYS];

double averages[NUM_DAYS];

// Open the input file

std::ifstream inputFile("ch8_Ex12Data.txt");

if (!inputFile) {

std::cerr << "Error: Cannot open the input file." << std::endl;

return 1;

}

// Read and store the data

readData(runnerNames, milesRun, inputFile);

// Calculate the average miles run each day

calculateAverage(milesRun, averages);

// Output the results

outputResults(runnerNames, milesRun, averages);

// Close the input file

inputFile.close();

return 0;

}

answered
User Yershuachu
by
8.3k points