Question

PLEASE WRITE IT IN C++ Thank you Assignment: Write a program that reads a text file...

PLEASE WRITE IT IN C++ Thank you

Assignment:

Write a program that reads a text file containing a set of test scores. Each line in the file contains ten scores of a specific student. For example, the first line has the scores for the first student, the second line has the scores for the second student, and so forth. Average the scores of each student and save them in a vector of final scores. So, if there are 10 students, the vector should have 10 final scores. Sort the final scores to calculate the class median. Print the final scores sorted from least to greatest, the class average and the class median.

Detailed set of requirements:

  1. 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.
  2. Store all final scores in a vector (not an array!).
    • To add values to a vector, use function push_back.
  3. 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.
  4. 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.
  5. Define a separate 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.
  6. Do not use global variables! Do not use goto!
  7. Document your source code following the guidelines:
    • Lab Documentation Guidelines
  8. Attach the output of your program as a comment at the end of your source code.

    Lab Guidelines:2. Write a comment above each function header explaining what the function does and the purpose of each variable. Example:

  9. /******************************************************************

    This function adds the amount passed as parameter to the balance

    in the account and returns whether the operation was successful.

    *******************************************************************/

    bool deposit (double amount)

    {

    ...

    }

    3. Comment variables that are not self explanatories.

    For example, a variable named interestRate doesn't need an explanation if you are using it to store interest rate, but if you have a variable named x, you need to explain what you use it for.

    4. General guidelines:

    Comments are supposed to add meaning to your code. The following are examples of comments that do NOT have any purpose in a program and must be avoided:

    // variable declarations

    int x; // integer variable

    voidprintReport(); // function prototype

input.txt -

70 79 72 78 71 73 68 74 75 70

78 89 96 91 94 95 92 88 95 92

83 81 93 85 84 79 78 90 88 79

93 100 86 99 98 97 96 95 94 92

72 60 82 64 65 63 62 61 67 64

80 92 100 81 82 83 84 85 86 87

92 91 74 76 77 78 81 83 80 88

67 60 65 68 72 74 76 77 78 73

93 99 91 93 94 96 95 97 98 74

82 81 89 75 77 79 81 83 85 78

sample output:

Final scores: 66 71 73 81 82 84 86 91 93 95

This is the avrg grade in the class: 82.2

This is the median grade in the class: 83

to use Ginger
Limited mode
the average grade in the class
×
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <fstream>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <algorithm>

// FUNCTION TO SORT ELEMENTS OF VECTOR
bool tf(int i, int j){
return (i<j);
}

//FUNCTION TO COMPUTE AVERAGE OF VECTOR
double average_fx(std::vector<double> fscores) {
double summ = 0, avg;
for(int s=0;s<fscores.size();s++){
summ = summ+fscores[s];
}
avg = summ/fscores.size();
return avg;
}

//FUNCTION TO COMPUTE MEDIAN OF VECTOR
double median_fx(std::vector<double> fscores) {
double mdn = 0;
int L=fscores.size();
if((L%2) ==0){
mdn = 0.5*(fscores[L/2 - 1]+fscores[L/2]);
}else{
mdn = fscores[L/2];
}

return mdn;
}

//FUNCTION TO PRINT RESULTS
void printResults(std::vector<double> fscores,double avgg, double mdnn){

std::cout << " Sorted Vector: " << std::endl;


for (int g=0;g<fscores.size();g++){
std::cout << fscores[g] << std::endl;
}


// TO USE RAGE BASED for LOOP
/*
for (int g: fscores){
std::cout << fscores[g] << std::endl;
}

*/

std::cout << " Average: " << avgg<<std::endl;
std::cout << " Median: " << mdnn<<std::endl;
}


int main() {


//PROMPTING USER TO ENTER FILE NAME AND TRYING OPENNING FILE FROM DEFAULT PATH
std::ifstream in_file;
std::string filename,custom_paths;
std::cout<<"Enter FileName.txt ";
std::cin>>filename;
in_file.open(filename.c_str());

//HANDLING FAILURE TO OPEN FILE
while(in_file.fail())
{
in_file.clear();
std::cout<<"no such file exists in default path, please try to in-put correct name: ";
std::cin>>filename;
in_file.open(filename.c_str());

if(in_file.fail()){
std::cout<<"no such file exists, enter custom paths including the filename.txt. Use double back-slashes: ";
std::cin>>custom_paths;
in_file.open(custom_paths.c_str());
}

}
std::vector<double> scores;
std::vector<double> final_scores;

double num = 0.0;
while (in_file >> num) {
scores.push_back(num);
}
int m=0;
int n=10;
int nn = n-1;

//COMPUTING AVERAGE OF EACH STUDENT
std::cout << " Vector (Final Scores): " << std::endl;
for(int j = 0; j < scores.size()-nn; j=j+nn){

double sm = 0;
for (int i = j+m; i < j+n+m; ++i) {
sm = sm+scores[i];
}
final_scores.push_back(sm/n);

std::cout << final_scores[m] << std::endl;
m=m+1;
}


//SORTING VECTOR
std::sort(final_scores.begin(),final_scores.end(),tf);

//CALLING FUNCTIONS, COMPUTING AND DISPLAYING RESULTS
double average = average_fx(final_scores);
double median = median_fx(final_scores);
printResults(final_scores,average, median);

//CLOSING FILE
in_file.close();

return 0;
}



please give thumbs up

Add a comment
Know the answer?
Add Answer to:
PLEASE WRITE IT IN C++ Thank you Assignment: Write a program that reads a text file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT