Need help on following program using C++ language. Jason, Samantha, Ravi, Sheila
ID: 3696463 • Letter: N
Question
Need help on following program using C++ language.
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. The input data is stored in a file which is attached (marathon.txt) and each line of data contains the name of the runner and the number of days he/she ran that week (each runner ran every day of the week).
Design with functions and use arrays.
Write code which should be commented as follows:
Analysis at the top of the file
Function analysis, preconditions, and postconditions after each function prototype
Major tasks identified as comments in main function
Comments in functions as needed
Explanation / Answer
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
#include <iostream>
using namespace std;
//variables before main function(global variables)
double matr[4][8];
double avg;
const int noofcols = 8;
const int noofrows = 4;
double row, col;
void openFile(ifstream& afile, double matr[][noofcols]);//function prototypes
void avgs(ifstream& afile, double matr[noofrows][noofcols]);
void prinMat(ifstream& afile, double matr[noofrows][noofcols]);
int main()
{
ifstream afile;
openFile (afile, matr);// function to open file
return 0;
}
void openFile(ifstream& afile, double matr[][noofcols])
{
string inpfile;//used to see inputing file name
cout << "Enter the input file name:";
cin >> inpfile; //open the file
afile.open(inpfile);
if(!afile) //If no file there then this will proceed
{
cout << "Input file opening failed.";
}
while(!afile.eof())//while for checking the end of file
{
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
afile >> matr[i][j]; // attempt to pass information from file into array
}
}
}
avgs(afile, matr); // function averages to calculate sum and average
}
void avgs(ifstream& afile, double matr[noofrows][noofcols])//this function doesnt work at all
{
cout << fixed << showpoint;
cout << setprecision(2);
double sum=0;
double avg=0;
for (int row=0; row < noofrows; row++)
{
for (int col = 0; col < noofcols; col++)
{
sum = sum + matr[row][col];
avg = sum / 7; //finding the average
}
prinMat(afile, matr);
}
}
void prinMat(ifstream& afile, double matr[][noofcols])
{
cout << fixed << showpoint;
cout << setprecision(2);
// making a array to display this information
string Days[9]= {"Name", "Day 1", "Day 2", "Day 3", "Day 4", "Day 5", "Day 6", "Day 7", "Averages"};
cout << Days;
cout << endl;
cout << matr[4][8];
}
Name Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Averages
Jason 20.00 18.00 10.00 20.00 15.00 25.00 26.00 19.14
Samantha16.00 15.00 18.00 29.00 23.00 26.00 20.00 21.00
Ravi 20.00 18.00 29.00 12.00 10.00 20.00 26.00 19.29
Sheila 17.00 20.00 15.00 18.00 26.00 25.00 12.00 19.00
Ankit 20.00 8.00 28.00 16.00 11.00 25.00 21.00 18.43
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.