Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Objective: Review file streams and sorting (or learn this if you haven\'t in

ID: 3709858 • Letter: C

Question

C++

Objective:

Review file streams and sorting (or learn this if you haven't in 22A)

Learn about vectors (if you haven't in 22A), string streams and sort

Assignment:

Write a program that reads a text file containing a set of test scores. Each line in the file contains scores of a specific student. For example, the first line has the scores for the first student and so forth. Average the scores for each student and save them in a vector of final scores. Then sort the final scores to calculate the class average and median. Print the final scores sorted from least to greatest, the class average and median.

Detailed set of requirements:

File processing:

Program should try to open a file using a default file path first, then check if it was successful, if not, ask the user for a different file name/path and try again until it succeeds. Do not try to process a file that you could not open. Remember to close the file when done reading.

To make sure you process each student's scores separately, read each line from the file using the getline function. You can find an example on how to use getline here: http://www.cplusplus.com/forum/beginner/11304/ (Links to an external site.)Links to an external site.

Convert each line, which should be a string, to a string stream, so it can be used as a source for the individual test scores:

stringstream lineStream(line) // line is the string read from the file.

Read the individual scores from the lineStream variable as you would from an ifstream.

Store all final scores in a vector (not an array!).

To add values to a vector, use function push_back.

Sort the data using the sort function (you will need this for the median)

Refer to http://www.cplusplus.com/reference/algorithm/sort/ (Links to an external site.)Links to an external site. to learn how to use sort.

Calculate the average and median of the values in the vector.

Average is calculated by adding all data points then dividing by the number of data points.

Median is the data point in the middle after you have sorted them. If there is an odd number of data points, there will be only one in the middle. If there is an even number of data points, the median is the average of the two in the middle. Your algorithm must work for both cases.

Define separate functions to calculate and return average and median.

Define a seprate function to print the required info on the screen:

This function should receive the vector, the average and median as parameters.

Use a range-based for loop to print the scores in the vector.

Do not use global variables!

Explanation / Answer

#include<iostream>
#include <fstream> // For ifstream
#include <stdlib.h> // For function exit()
#include<string>
#include <algorithm> // sort
#include <vector> // vector
using namespace std;

void readFile(vector<int> &scores)
{
// File stream object declared
ifstream fileRead;
// To store file name
string fileName;
// To store total score
int score, total = 0;
// For number of scores
int counter = 0;

// Loops till valid file name entered by the user
do
{
// Accepts file name from the user
cout<< " Enter the file name to read students scores: ";
getline(cin, fileName);

// Open the file for reading
fileRead.open(fileName.c_str());


// Check that file can be opened or not
// is_open() function returns true if a file is open and associated with this stream object.
// Otherwise returns false.
if(!fileRead.is_open())
{
// Displays error message
cout<<" Error: Unable to open the file! Please re - enter the file name.";
}// End of if condition
// Otherwise file can be opened
else
// Come out of the loop
break;
}while(1); // End of do while loop

// Loops till not end of the file
// eof() function returns true if the stream's eofbit error state flag is set (which signals that the End-of-File has been reached
// by the last input operation). Otherwise returns false.
while(!fileRead.eof())
{
// Reads an individual score
fileRead>>score;
// Calculates total
total += score;
// Increase the counter for number of scores
counter++;
// Checks if the score count is 4
if(counter == 4)
{
// Then set the score counter to zero for next student
counter = 0;
// Adds the total score to the vector
scores.push_back(total);
// Reset the total to zero for next student
total = 0;
}// End of if condition
}// End of while condition

// Close the file
fileRead.close();
}// End of function

// Function to display total score of all the students
void displayScores(vector<int> scores)
{
// Loops till end of the vector
for (vector<int>::size_type counter = 0; counter != scores.size(); ++counter)
// Displays total score
cout <<" Total of Student "<<(counter + 1)<<": "<<scores[counter] << " ";
}// End of function

// Function to calculate average
void calculateAverage(vector<int> scores)
{
// To store total
int total = 0;
// To store average
double average;
// Loops till end of the vector
for (vector<int>::size_type counter = 0; counter != scores.size(); ++counter)
// Calculates total
total += scores[counter];
// Calculates average
average = ((double)total / scores.size());
// Displays average
cout<<" Average: "<<average;
}// End of function

// Function to display median student score
void calculateMedian(vector<int> scores)
{
// Stores the middle index
int middle = scores.size() / 2.0;
// To store average
double average;
// Checks for even size
if (scores.size() % 2 == 0)
cout << " Median = "<< (scores[scores.size()/2 - 1] + scores[scores.size()/2]) / 2<< endl;
// Otherwise odd size
else
cout << " Median = " << scores[scores.size()/2]<< endl;
}// End of function

// main function definition
int main()
{
// Creates a vector of type integer to store total scores
vector<int> scores;
// Calls the function to read scores from file
readFile(scores);

// Sorting the int vector
sort(scores.begin(), scores.end());

// Calls the function to display total scores
displayScores(scores);
// Calls the function to calculate and display average scores
calculateAverage(scores);
// Calls the function to calculate and display median scores
calculateMedian(scores);
}// End of function

Sample Output:

Enter the file name to read students scores: TestScores.txt

Total of Student 1: 220
Total of Student 2: 283
Total of Student 3: 338
Total of Student 4: 363
Total of Student 5: 369
Average: 314.6
Median = 338

TestScores.txt file contents

95 85 86 97
48 55 50 67
95 49 97 97
68 74 60 81
87 89 99 94

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote