Jason, Samantha, Ravi, Sheila, and Ankit are preparing for an upcoming marathon. Each day of the week, they run a certain number of miles and write them into a notebook. At the end of the week, they would like to know the number of miles run each day, the total miles for the week, and average miles run each day. 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 find the total miles run by each runner and 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 milesDay1 milesDay2 milesDay3 milesDay4 milesDay5 milesDay6 milesDay7.)

Respuesta :

Answer:

C++ program explained below for the problem

Explanation:

// UpComingMarathon.cpp : Defines the entry point for the console application.

//header files

#include "stdafx.h" //optional

#include <iostream>

#include <cstring>

#include <fstream>

#include <iomanip>

using namespace std;

//declare the gobal array of type double

double average[5];

//declare the function prototypes

void getData(ifstream& inf, string n[], double runData[][8], int count);

void calculateAverage(double runData[][8], int count);

void print(string n[], double runData[][8], int count);

int main()

{

   string names[5];

   double runningData[5][8];

   ifstream inputfile("input.txt");

   if(inputfile)

   {

        //call the method getData

        getData(inputfile, names, runningData, 5);

   }

   else

   {

        //error message

        cout<<"Sorry! Unable to open the file."<<endl;

        system("pause");

        return 0;

   }

   //close the file

   inputfile.close();

   //call the method calculateAverage to compute the

   //average miliage of each runner

   calculateAverage(runningData, 5);

   //call display the names and their weekly run rate and their averages

   print(names, runningData, 5);

   system("pause");

   return 0;

}

//definition of method getData that reads the data from the file and

//stores the names into array n and run data into runData array

//simultaneously.

void getData(ifstream& inf, string n[], double runData[][8], int count)

{  

   while(!inf.eof())

   {

        for(int i=0;i<count; i++)

        {

            inf>>n[i];

            for(int j=0;j<7;j++)

            {

                inf>>runData[i][j];

            }

        }

   }

}

//definition of method calculateAverage that comptes the total first

//then stores the value of average into the average array

void calculateAverage(double runData[][8], int count)

{

   double total;

   for(int i=0;i<count;i++)

   {

        total=0;

        for(int j=0;j<7;j++)

        {

            total+=runData[i][j];

        }        

        average[i]=total/7;      

   }

}

//definition of method print that prints the output

//in a tabular format

void print(string n[], double runData[][8], int count)

{

   cout<<setfill('=')<<setw(80)<<"=";

   cout<<setfill(' ');

   cout<<endl;

   cout<<"Name"<<setw(6)<<"";

   for(int i=0;i<7;i++)

        cout<<setw(7)<<"Day "<<(i+1);

   cout<<setw(12)<<"Average"<<endl;

   cout<<setfill('=')<<setw(80)<<"=";

   cout<<setfill(' ')<<endl;

   for(int i=0;i<count;i++)

   {

        cout<<n[i]<<setw(8)<<fixed<<"";

        for(int j=0;j<7; j++)

        {

            cout<<setprecision(2)<<fixed<<runData[i][j]<<setw(3)<<"";

        }

        cout<<setw(8)<<average[i];

        cout<<endl;

   }

   cout<<setfill('=')<<setw(80)<<"=";

   cout<<endl;

}

ACCESS MORE
EDU ACCESS