This my fourth time posting this same question, so please before posting please
ID: 3678241 • Letter: T
Question
This my fourth time posting this same question, so please before posting please check first if the solution works and compiles. the previous 3 solutions were wasted since it made errors when compiled. The program is almost done and just need a few tweaks to work. This is my last post of this question, so if it finally works i would really appreciate it. Here is the question again:
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. This must be on C++ format. I have the program almost done, except for some compiler errors. I will post my program below, and i will also post the compiler errors i received. If the compiler errors are resolved, then the program is done. So when doing this, please compile it and check it first, before posting the solution (this is my last post of this question, please check it first). I would really appreciate the help for this.
Compiler errors list:
erasethislater.cpp:(.text+0x106): undefined reference to `getData(std::basic_ifstream<char, std::char_traits<char> >&, std::string*, double**, int)'
erasethislater.cpp:(.text+0x15d): undefined reference to `calculateAverage(double**, int)'
erasethislater.cpp:(.text+0x179): undefined reference to `print(std::string*, double**, int)'
[Error] ld returned 1 exit status
Here is my program:
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#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;
}
Explanation / Answer
Please find the corrected code below :
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
//declare the global array of type double
double *average = new double[5];
//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, 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, 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, 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;
}
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.