Please use C++ Please don\'t answer this if you don\'t use C++! People have answ
ID: 3574304 • Letter: P
Question
Please use C++
Please don't answer this if you don't use C++! People have answered it in Python and Java but not C++!! I have done this code in array's but I don't know anything about vectors, that's why I need your help!! Thanks in advance!!
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 vectors: an vector 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. (Your 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 program should be able to accommodate any number of runners.
Explanation / Answer
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
int main()
{
std::ifstream file("file.txt");
std::string line;
std::vector<string> nameData;
float data[5][7];
int i=0 , j=0;
float sum[5] ={0,0,0,0,0};
while(std::getline(file, line))
{
std::stringstream lineStream(line);
string name;
lineStream >> name;
nameData.push_back(name);
float val;
while(lineStream >>val)
{
data[i][j]=val;
sum[i] = sum[i]+ data[i] [j];
j++;
}
i++;
j=0;
}
vector<string>::iterator it;
i=0;
for (it = nameData.begin(); it != nameData.end(); ++it)
{
std::cout << *it << " Avg miles :" << sum[i]/7<<endl;
i++;
}
}
/*
john 1 2 3
rick 1 3 4
angela 4 5 6
*/
/*output
john Avg miles :0.857143
rick Avg miles :1.14286
angela Avg miles :2.14286
*/