Question
Write a program that reads a text file containing a set of test scores, calculates the average and median. The file name must be entered
Write a program that reads a text file containing a set of test scores, calculates the average and median. The file name must be entered by the user. Your program should follow these steps:
1.Ask the user for the file name
2.Read the file name and use it to open the file
3.Check if the file opening was successful. If not, ask the user to enter it again until you succeed.
4.Read all scores to a vector (not an array!).
5.Sort the data using the sort function (you will need this for the median)
6.Calculate the average and median
7.Print on the screen:
The scores sorted from least to greatest
The average (rounded to one-decimal place) and median(rounded to one-decimal place)
Definitions:
Average: all data points added together and divided by the number of data points.
Median: 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.
Use the sort function to sort the vector. 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.
You will be graded on following the specification and on how well-structured your program is. Please, refer to the rubric.
Define separate functions to calculate average, median and mode.
The calculateAverage function will receive the vector as parameter, calculate and return the average rounded to the nearest tenth (one decimal place).
The calculateMedian function will receive the sorted vector, determine and return the median rounded to the nearest tenth (one decimal place). Make sure it works for an even or odd number of data points.
Also define functions to read the data from the file into a vector and to print a report with the required data.
The readData function will ask the user for the file name, open the file and populate the vector with the scores. You will need to pass the vector by reference to this function as you will need its content after you return. Important to remember: vectors are objects (not arrays) and will not be automatically passed by reference.
The printReport function should print all requested information nicely formatted on the screen.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started