-
Notifications
You must be signed in to change notification settings - Fork 0
/
a1.9.cpp
103 lines (85 loc) · 2.69 KB
/
a1.9.cpp
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
94
95
96
97
98
99
100
101
102
103
/*
* 9.
* WAP in CPP to sort the details of n students entered by the user in the
* order of their marks using OOP concept. Also display the details of
* students obtaining highest and third highest marks. Assume n>=3.
*/
#include <iostream>
#define MAX_SIZE 1000
using namespace std;
class Student {
public:
void getSudentCount();
void getStudentInfo();
void sort();
void printStudentInfo(int);
private:
int n;
float marks[MAX_SIZE];
int rollNum[MAX_SIZE];
void checkStudentCount();
};
void Student::checkStudentCount(){
if(n < 3){
cout << "Please enter number greater than 2." << endl;
getSudentCount();
};
}
void Student::getSudentCount(){
cout << "\nNo. of students: ";
cin >> n;
checkStudentCount();
}=
void Student::getStudentInfo(){
for (int i = 0; i < n; i++){
cout << "\nFor student " << i+1 << ": " << endl;
cout << "Roll number: ";
cin >> rollNum[i];
cout << "Marks: ";
cin >> marks[i];
}
}
void Student::sort(){ //bubble sort algorithm
float tmpMarks;
int tmpRollNum;
for(int i = 0; i < (n-1); i++){
for(int j = 0; j<(n-i-1); j++){
if(marks[j] < marks[j+1]){ //descending order
tmpMarks = marks[j];
marks[j] = marks[j+1];
marks[j+1] = tmpMarks;
tmpRollNum= rollNum[j];
rollNum[j] = rollNum[j+1];
rollNum[j+1] = tmpRollNum;
}
}
}
}
void Student::printStudentInfo(int position){
if (position == 0){
for (int i = 0; i < n; i++){
cout << "For student " << i+1 << ": " << endl;
cout << "Roll number: " << rollNum[i] << endl;
cout << "Marks: " << marks[i] << endl << endl;
}
}
else{
cout << "Roll number: " << rollNum[position-1] << endl;
cout << "Marks: " << marks[position-1] << endl;
}
}
int main(){
Student student;
student.getSudentCount();
cout << "\n::Details of students:: " << endl;
student.getStudentInfo();
cout << endl;
student.sort();
cout << "\nDetails of all students in descending order of marks:" << endl;
student.printStudentInfo(0); //prints details of all students
cout << "\n\nDetails of student with the highest marks: " << endl;
student.printStudentInfo(1); //prints the details according to highest marks
cout << "\nDetails of student with the third highest marks: " << endl;
student.printStudentInfo(3); //prints the details according to third highest marks
return 0;
}