Write a program for the following problem. You\'re given a file that contains a
ID: 3627820 • Letter: W
Question
Write a program for the following problem. You're given a file that contains a collection of ID's and scores (type int) for an exam in your computer course. You're to compute the average of these scores and assign grades to each student according to the following rule:If a student's score is within 10 points above or below of the average, assign a grade of satisfactory. If a student's score is imore than 10 points above the average, assign a grade of outstanding. If a studen'ts score is more than 10 points below average, assign a grade of unsatisfactory.
The output from your program should consist of a labeled three-column list that shows each ID, score and corresponding grade. Note: To run your program, you need to place scores.txt in the project folder
Here is the skeleton code he gave us
// Lab5a.cpp
// Problem 2, page 556
// Header files
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
// Function Prototypes
void readStudData(ifstream &rss, int scores[], int id[], int &count, bool &tooMany);
float mean(int scores[], int count);
void printTable(int score[], int ID[], int count);
void printGrade(int oneScore, float average);
// Only accept MAX_SIZE entries, this will be the size of our arrays
const int MAX_SIZE = 10;
int main()
{
// variables. Need to create two int arrays: scores and ID.
// Need to create one int: count. Need to create one bool: tooMany
// Call readStudData to read data from the file that was opened above
// inFile is the name of the file opened
readStudData(inFile, scores, ID, count, tooMany);
// Don't forget to close the file
inFile.close();
// If there are more than MAX_SIZE records in the file, then tooMany should be
// set to true. We need to warn the user that not all the records were used
if( tooMany ) cout << " Warning! Some data is missing ";
// Print the data
printTable(scores, ID, count);
cout << endl;
system("Pause");
return 0;
}
// Description:
// Pre-condition:
// Post-condition:
void readStudData(ifstream &rss, int scores[], int id[], int &count, bool &tooMany)
{
// Set tooMany to false and count to 0
tooMany = false;
count = 0;
// Loop through the file until rss.eof() is true && count < MAX_SIZE
// Make sure you increment count everytime you read a line from the file
// id's need to be put in the id array, scores need to be put in the scores array
// YOUR CODE GOES HERE
// Determine if we have too many records in the file
if( !rss.eof() && count == MAX_SIZE ) tooMany = true;
}
// Description:
// Pre-condition:
// Post-condition:
float mean(int scores[], int count)
{
// Store the sum of the scores here
int sum = 0;
// Loop through and accumulate the scores from the array
// YOUR CODE GOES HERE
// Return the average
return (float)sum/count;
}
// Description:
// Pre-condition:
// Post-condition:
void printTable(int score[], int ID[], int count)
{
// Need the average
float average = mean(score, count);
// Display the average to the user
cout << "Average: " << average << endl;
// Display the header for the table
cout << "ID " << "Score " << "Grade" << endl;
// Need to display all the information in the arrays
// Should probably use a for statement
// You will need to call: printGrade(score[i], average); in the loop to
// display the grade under the third column
// YOUR CODE GOES HERE
}
// Description:
// Pre-condition:
// Post-condition:
void printGrade(int oneScore, float average)
{
// Code to cout the correct grade: Outstanding, Satisfactory, or Unsatisfactory
// See the text for grade criterion
// Recommend that you use if-else if-else
// YOUR CODE GOES HERE
}
And this is from the scores.txt file
1001 85
1002 97
1003 49
1004 75
1005 62
1006 58
1007 92
1008 27
1009 81
1010 79
Explanation / Answer
// Lab5a.cpp // Problem 2, page 556 // Header files #include <iostream> #include <iomanip> #include <fstream> using namespace std; // Function Prototypes void readStudData(ifstream &rss, int scores[], int id[], int &count, bool &tooMany); float mean(int scores[], int count); void printTable(int score[], int ID[], int count); void printGrade(int oneScore, float average); // Only accept MAX_SIZE entries, this will be the size of our arrays const int MAX_SIZE = 10; int main() { // variables. Need to create two int arrays: scores and ID. // Need to create one int: count. Need to create one bool: tooMany int scores[MAX_SIZE]; int ID[MAX_SIZE]; int count; bool tooMany; // Call readStudData to read data from the file that was opened above // inFile is the name of the file opened ifstream inFile; inFile.open("scores.txt"); readStudData(inFile, scores, ID, count, tooMany); // Don't forget to close the file inFile.close(); // If there are more than MAX_SIZE records in the file, then tooMany should be // set to true. We need to warn the user that not all the records were used if( tooMany ) cout << " Warning! Some data is missing "; // Print the data printTable(scores, ID, count); cout << endl; system("Pause"); return 0; } // Description: Fills the arrays with the student's test and id information makes sure there arent more than MAX_SIZE data values // Pre-condition: input file stream opened, variables count and tooMany made, arrays scores, id made // Post-condition: values are input into the given variables and arrays by reference void readStudData(ifstream &rss, int scores[], int id[], int &count, bool &tooMany) { // Set tooMany to false and count to 0 tooMany = false; count = 0; // Loop through the file until rss.eof() is true && count < MAX_SIZE // Make sure you increment count everytime you read a line from the file // id's need to be put in the id array, scores need to be put in the scores array while(!rss.eof()) { if(count == MAX_SIZE) break; rss >> id[count] >> scores[count]; count++; } // Determine if we have too many records in the file if( !rss.eof() && count == MAX_SIZE ) tooMany = true; } // Description: Calculates the average class grade // Pre-condition: scores must be filled // Post-condition: returns the class average float mean(int scores[], int count) { // Store the sum of the scores here int sum = 0; // Loop through and accumulate the scores from the array for(int i = 0; i < count; i++) { sum += scores[i]; } // Return the average return (float)sum/count; } // Description: Display all the information in the arrays // Pre-condition: Scores, ID arrays are filled with data // Post-condition: prints out the ID followed by the score name void printTable(int score[], int ID[], int count) { // Need the average float average = mean(score, count); // Display the average to the user cout << "Average: " << average << endl; // Display the header for the table cout << "ID " << "Score " << "Grade" << endl; // Should probably use a for statement // You will need to call: printGrade(score[i], average); in the loop to // display the grade under the third column for(int i = 0; i < count; i++) { cout << ID[i] << ' ' << score[i] << ' '; printGrade(score[i], average); } } // Description: Code to cout the correct grade: Outstanding, Satisfactory, or Unsatisfactory // Pre-condition: Student's score must be read and average must be calculated // Post-condition: Outputs the correct grade void printGrade(int oneScore, float average) { // See the text for grade criterion // Recommend that you use if-else if-else if(oneScore < average + 10 && oneScore > average - 10) cout << "Satisfactory "; //If a student's score is within 10 points above or below of the average, assign a grade of satisfactory. else if(oneScore > average + 10) cout << "Outstanding "; //If a student's score is imore than 10 points above the average, assign a grade of outstanding. else cout << "Unsatisfactory "; //If a studen'ts score is more than 10 points below average, assign a grade of unsatisfactory. }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.