Pgm Description:: Define a STUDENT class to store a student's USN, name, and marks for three tests. The program creates an array of 10 student objects, calculates the average of each student's two best marks, and displays their final records.
Pgm Details: This program utilizes an array of objects, which allows managing multiple student records under a single class structure. The logic specifically identifies and excludes the lowest test score to reward student performance.
Pgm Logic:
Start.
Define a class STUDENT with private members: USN, Name, Marks, and average_marks.
Define public member function setData() to accept USN, name, and three test marks from the user.
Define calculateAverage():
Find the minimum of the three marks.
Calculate the average using the formula: (sum of all marks - lowest mark) / 2.
Define displayData() to output the USN, Name, and calculated average.
In main(), declare an array of 10 STUDENT objects.
Use a loop to call setData() for each student.
Use a second loop to display the results for all students.
Stop.
Program Code:
// C++ program to manage student records and calculate average of best two marks
#include <iostream>
#include <string>
using namespace std;
class STUDENT {
private:
char USN;
char Name;
float Marks;
float average;
public:
void setData() {
cout << "Enter USN: ";
cin >> USN;
cout << "Enter Name: ";
cin.ignore();
cin.getline(Name, 30);
cout << "Enter Marks in 3 tests: ";
for (int i = 0; i < 3; i++) {
cin >> Marks[i];
}
}
void calculateAverage() {
float low = Marks;
if (Marks < low) low = Marks;
if (Marks < low) low = Marks;
average = (Marks + Marks + Marks - low) / 2.0;
}
void displayData() {
cout << USN << "\t" << Name << "\t\t" << average << endl;
}
};
int main() {
STUDENT students;
int n;
cout << "Enter the number of students: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "\nEnter details for Student " << i + 1 << ":\n";
students[i].setData();
students[i].calculateAverage();
}
cout << "\nUSN\t\tNAME\t\tAVERAGE MARKS\n";
cout << "--------------------------------------------\n";
for (int i = 0; i < n; i++) {
students[i].displayData();
}
return 0;
}
Output:
Enter the number of students: 2
Enter details for Student 1:
Enter USN: 2KD22EC001
Enter Name: ABHISHEK C
Enter Marks in 3 tests: 12 13 14
Enter details for Student 2:
Enter USN: 2KD22EC009
Enter Name: ANISHA
Enter Marks in 3 tests: 13 17 15
USN NAME AVERAGE MARKS
--------------------------------------------
2KD22EC001 ABHISHEK C 13.5
2KD22EC009 ANISHA 16
RESULT: Thus the program has been executed and the output was verified.
Remarks: For large inputs, using cin.ignore() before getline() is crucial to prevent the program from skipping name entries. This can be compiled and run in Code::Blocks or Visual Studio Code.
Program Explanation: The program uses a class to bundle data (USN, Name, Marks) with the logic to process it. By using an array of objects, the code can efficiently handle multiple records in a systematic way.
eBook ‘C++ Lab Programs Collection’ purchase Link: Google Play Store || Google Books
...till the next post, bye-bye & take care
No comments:
Post a Comment