Answer on Question #44656, Programming, C++
Problem.
Design a solution algorithm and write program that will read a file of student test results and produce a student test grades report. Each test record contains the student number, name and test score (out of 50). The program is to calculate for each student the test score as a percentage and to print the student's number, name, test score (out of 50) and letter grade on the report. The letter grade is determined as follows:
A = 90 - 100%
B = 80 - 89%
C = 70 - 79%
D = 60 - 69%
F = 0 - 59%
Task need to do:
Create report for 10 students.
1. Design algorithm [Note: Pseudocode and Flowchart]
2. Write program and use control structures required
- A DOWHILE loop to control the repetition
- Decision symbols to calculate the grade
Solution.
File (data.txt)
1 Oles 34
2 Andrew 22
3 Jack 44
4 Sam 21
5 Lina 32
6 Lima 37
7 Jemmy 43
8 Barack 17
9 Bred 38
10 Liam 30Code
#include <iostream>
#include <iomanip> // for formatted output
#include <fstream>
#include <string>
using namespace std;
struct student {
int number; // student number
string name; // student name
int score; // student score
};
int main() {
student students[50]; // student array
ifstream file("data.txt");
if (file.is_open()) {
int n = 0; // number of students
// Data reading
while (file >> students[n].number >> students[n].name >> students[n].score) {
n++;
}
// Data output
for(int i = 0; i < n; i++) {
cout << left << setw(5) << students[i].number << setw(10) << students[i].name << setw(5) << students[i].score;
float result = ((float) students[i].score / 50) * 100;
if (result >= 90) {
cout << setw(5) << "A";
}
if ((80 <= result) && (result < 90)) {
cout << setw(5) << "B";
}
if ((70 <= result) && (result < 80)) {
cout << setw(5) << "C";
}
if ((60 <= result) && (result < 70)) {
cout << setw(5) << "D";
}
if (result < 60) {
cout << setw(5) << "F";
}
if (i != n) {
cout << endl;
}
}
}
return 0;
}Flowchart (data reading)
Flowchart (data output)
Result
Process returned 0 (0x0) execution time : 3.045 s
Press any key to continue.
http://www.AssignmentExpert.com/
Comments