-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
93 lines (75 loc) · 2.97 KB
/
Copy pathmain.cpp
File metadata and controls
93 lines (75 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream> // For input/output operations
#include <vector> // To use dynamic arrays (vectors)
#include <iomanip> // For formatting output (setw, setprecision)
using namespace std;
// Function to calculate and return the letter grade based on average marks
string calculateGrade(float average) {
if (average >= 90)
return "A+";
else if (average >= 80)
return "A";
else if (average >= 70)
return "B";
else if (average >= 60)
return "C";
else if (average >= 50)
return "D";
else
return "F";
}
int main() {
int numStudents, numSubjects;
// Prompt user for number of students
cout << "Enter number of students: ";
cin >> numStudents;
// Prompt user for number of subjects
cout << "Enter number of subjects: ";
cin >> numSubjects;
// Vector to store subject names
vector<string> subjectNames(numSubjects);
// Input names of subjects
for (int i = 0; i < numSubjects; ++i) {
cout << "Enter name of subject " << i + 1 << ": ";
cin >> subjectNames[i];
}
// Vector to store each student's name
vector<string> studentNames(numStudents);
// 2D vector to store marks of each student in all subjects
vector<vector<float>> studentMarks(numStudents, vector<float>(numSubjects));
// Vectors to store total marks, average, and grade for each student
vector<float> totals(numStudents, 0); // Initialized with 0
vector<float> averages(numStudents);
vector<string> grades(numStudents);
// Input marks for each student
for (int i = 0; i < numStudents; ++i) {
cout << "\nEnter student name: ";
cin >> studentNames[i]; // Store the student's name
float sum = 0; // Sum of marks for current student
// Input marks for each subject
for (int j = 0; j < numSubjects; ++j) {
cout << "Enter marks for " << subjectNames[j] << ": ";
cin >> studentMarks[i][j]; // Store mark
sum += studentMarks[i][j]; // Add to sum
}
// Calculate total and average
totals[i] = sum;
averages[i] = sum / numSubjects;
// Calculate and store letter grade
grades[i] = calculateGrade(averages[i]);
}
// Display report card header
cout << "\n📝 Student Report Card\n";
cout << left << setw(15) << "Name"
<< setw(10) << "Total"
<< setw(10) << "Average"
<< setw(8) << "Grade" << endl;
cout << "----------------------------------------\n";
// Display report card for each student
for (int i = 0; i < numStudents; ++i) {
cout << left << setw(15) << studentNames[i] // Name
<< setw(10) << fixed << setprecision(2) << totals[i] // Total
<< setw(10) << fixed << setprecision(2) << averages[i] // Average
<< setw(8) << grades[i] << endl; // Grade
}
return 0;
}