(Please give all the solutions and steps, if you give the answer by picture plea
ID: 3919891 • Letter: #
Question
(Please give all the solutions and steps, if you give the answer by picture please post a clear picture. Please don't answer the question if you can't do all the questions, THANKS).
3. (30 points) Write a function called courseGrades that takes no arguments and returns an average of the entire list of grades as a double. The function will open an input file called "grades.txt" from the same folder, which has the scores stored as int's, one per ine. The number of scores can vary so it should not be hard-coded. Use a vector to store the scores and use an iterator to go through the vector to read each element when calculating the average. Assume all necessary header files/libraries have been already listed in the main file. You may benefit from using the following constructs: vector.push_back(newItem) /Appends a new item at the end of a vector vector: :iterator myIterator; // Declares a new iterator to step myVector.beginO // Iterator pointing to the start of a vector myVector.endO; // Logical value to indicate whether the end of a streamName.open("myfile.extension"); // Opens a file and makes a // through each element in a vector of YourType // named myVector I7 v // logical connection to streamName ector has been reached getline(streamName, variable); // Reads a line from a stream into variable streamNane. eof(); // Logical condition to ndicate the end of a stre?/fileExplanation / Answer
double courseGrades()
{
double n;
double count=0;
double ans=0;;
ifstream fin;
vector<int> v;
vector<int<::iterator it;
fin.open("grades.txt");
while(!fin.eof())
{
getline(fin,n);
v.push_back(n);
}
for(it=v.begin();it!=v.end();it++)
{
ans=ans+(*it);
count++;
}
ans=ans/count;
return ans;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.