This is a rework of a previous exercise, here is the question: Redo programming
ID: 3677342 • Letter: T
Question
This is a rework of a previous exercise, here is the question:
Redo programming exercise 12 at the end of chapter 8 using dynamic arrays. Creation of dynamic two-dimension arrays is discussed on page 836 of your textbook.
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 array (in this revision use dynamic array): 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.) The catch here is, instead of using parallel arrays, the program must use dynamic arrays
Here is the data file for runs.txt: (this is only an example, can be changed to any value you want)
Jason 7 12 30 9 3 40 55
Samantha 20 11 39 1 24 70 33
Ravi 10 9 8 7 6 5 4
Sheila 25 27 28 21 30 10 22
Ankit 12 18 38 40 11 35 6
The OUTPUT should look like this:
Name Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Total miles Average
Jason 10.00 15.00 20.00 25.00 18.00 20.00 26.00 134.00 19.14
Samantha 15.00 18.00 29.00 16.00 26.00 20.00 23.00 147.00 21.00
Ravi 20.00 26.00 18.00 29.00 10.00 12.00 20.00 135.00 19.29
Sheila 17.00 20.00 15.00 26.00 18.00 25.00 12.00 130.00 19.00
Ankit 16.00 8.00 28.00 20.00 11.00 25.00 21.00 129.00 18.43
Special Note: Don’t forget to use functions. The program needs to be run on dynamic arrays. If the program needs some parallel arrays in it, it can be added as well, as long as it includes dynamic arrays. This must be on C++ format and it needs to be a working code with Comments (I would appreciate it even if the comments are in the dynamic array parts only) . I have the original version using parallel array version posted below. This program just needs to be rewritten into a dynamic array with functions,.I would truly appreciate the help to get this solve, ths is stressing me out, so please help.
// original parallel array version
#include <iostream>
#include <string>
#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("runs.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;
}
Explanation / Answer
The parallel Array program has all the functions required and need not be changed. To change the code to dynamic arrays, only the declaration of the arrays has
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
//declare the global array of type double
double *average = new double[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 = new String[5];
//memory allocated for elements of rows
double **runningData = new double*[5];
//memory allocated for elements of each column
for(int i=0; i<5; i++)
runningData[i] = new double[8];
ifstream inputfile("runs.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;
}
to be done dynamically. The following is the modified code:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.