0% found this document useful (0 votes)
1 views

devassignmet (1)

Uploaded by

anshukumar18366
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

devassignmet (1)

Uploaded by

anshukumar18366
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Question: Implement CGPA Calculator using C++ Concepts and Basics

You are required to implement a CGPA Calculator using various concepts and

basics of C++ programming. The system should meet the following

requirements and demonstrate your understanding of reading/writing data,

creating classes, accessing class members, handling pointers, arrays, strings,

functions, and more.

Here is The code:-


#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Student;

class PerformanceDetails {
public:
void displayPerformance(const Student &s);
};

class Student {
private:
string name;
int rollNumber;
int *credits;
float *grades;
int numberOfCourses;
static int studentCount;

public:
Student(int courses) : numberOfCourses(courses) {
credits = new int[courses];
grades = new float[courses];
studentCount++;
}

~Student() {
delete[] credits;
delete[] grades;
}

void inputDetails();
void calculateGPA();
float calculateCGPA();

static void displayStudentCount() {


cout << "Total Number of Students: " << studentCount <<
endl;
}

friend void compareCGPA(Student &s1, Student &s2);


friend class PerformanceDetails;
};

int Student::studentCount = 0;

void Student::inputDetails() {
cout << "Enter Student Name: ";
cin >> name;
cout << "Enter Roll Number: ";
cin >> rollNumber;
cout << "Enter " << numberOfCourses << " Grades: ";
for (int i = 0; i < numberOfCourses; i++) {
cin >> grades[i];
}

cout << "Enter " << numberOfCourses << " Credits: ";
for (int i = 0; i < numberOfCourses; i++) {
cin >> credits[i];
}
}

void Student::calculateGPA() {
float totalPoints = 0;
int totalCredits = 0;

for (int i = 0; i < numberOfCourses; i++) {


totalPoints += grades[i] * credits[i];
totalCredits += credits[i];
}

float gpa = totalPoints / totalCredits;


cout << "GPA: " << fixed << setprecision(2) << gpa << endl;
}

float Student::calculateCGPA() {
float totalPoints = 0;
int totalCredits = 0;

for (int i = 0; i < numberOfCourses; i++) {


totalPoints += grades[i] * credits[i];
totalCredits += credits[i];
}

return totalPoints / totalCredits;


}

void compareCGPA(Student &s1, Student &s2) {


float cgpa1 = s1.calculateCGPA();
float cgpa2 = s2.calculateCGPA();

if (cgpa1 > cgpa2) {


cout << s1.name << " has a higher CGPA." << endl;
} else if (cgpa2 > cgpa1) {
cout << s2.name << " has a higher CGPA." << endl;
} else {
cout << "Both have the same CGPA." << endl;
}
}

void PerformanceDetails::displayPerformance(const Student &s)


{
cout << "Student Name: " << s.name << endl;
cout << "Roll Number: " << s.rollNumber << endl;
cout << "Grades: ";
for (int i = 0; i < s.numberOfCourses; i++) {
cout << s.grades[i] << " ";
}
cout << endl;
cout << "Credits: ";
for (int i = 0; i < s.numberOfCourses; i++) {
cout << s.credits[i] << " ";
}
cout << endl;
}

int main() {
const int NUM_STUDENTS = 2;
Student* students[NUM_STUDENTS];
PerformanceDetails pd;

for (int i = 0; i < NUM_STUDENTS; i++) {


int courses;
cout << "Enter number of courses for Student " << (i + 1)
<< ": ";
cin >> courses;
students[i] = new Student(courses);
students[i]->inputDetails();
students[i]->calculateGPA();
}

Student::displayStudentCount();
if (NUM_STUDENTS >= 2) {
compareCGPA(*students[0], *students[1]);
}

for (int i = 0; i < NUM_STUDENTS; i++) {


pd.displayPerformance(*students[i]);
}

for (int i = 0; i < NUM_STUDENTS; i++) {


delete students[i];
}

return 0;
}

You might also like