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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
#include<algorithm>
using namespace std;
//define a structure named student
typedef struct Student {
int studentId;
int test1;
int test2;
int test3;
double avg;
};
//Function prototype
int readFile(Student*);
void selectionSort(Student[],int size);
bool sortAverage(Student a,Student b);
void binarySearchDisplay(Student[],int n, double search);
void greater70(Student[],int);
void greaterEqual85(Student[],int);
void binarySearch(Student[], int n, int search);
int main()
{
//structure array
Student student[30];
//Call to read file system call passing array stucture
int size=readFile(student);
//Call selection sort
cout << "After Selection sort:" << endl;
selectionSort(student, size);
//Call average 70
cout << "Display Student details of average greater than equal 70:" << endl;
greater70(student,size);
//Call average 85
cout << "Display Student details of average greater than equal 85:" << endl;
greaterEqual85(student,size);
//Loop to display each student details using id
char ch;
int id;
do {
cout << "Enter the student id to search:";
cin >> id;
binarySearch(student, size, id);
cout << "Do you want to serach again(Y/N):";
cin >> ch;
ch = toupper(ch);
} while (ch=='Y');
return 0;
}
//This search serch the given avg attained students details
//This function used to find student average 70 and 85
void binarySearchDisplay(Student s[],int n,double search) {
int first = 0,flag=0;
int last = n - 1;
int middle = (first + last) / 2;
while (first <= last)
{
if (s[middle].avg < search)
{
first = middle + 1;
}
else if (s[middle].avg >= search)
{
cout << s[middle].studentId << " " << s[middle].test1 << " " << s[middle].test2 << " " << s[middle].test3 << " " << s[middle].avg << endl;
last = middle - 1;
flag = 1;
}
else
{
last = middle - 1;
}
middle = (first + last) / 2;
}
if (first > last &&flag==0)
{
cout << "Not found! " << search << " is not present in the list."<<endl;
}
}
//Function to read a file details and store details in a struct array
//Return the number of lines read
int readFile(Student*s) {
ifstream in;
int i = 0;
in.open("pr2data.txt");
if (!in) {
cout << "File not open!!!" << endl;
exit(0);
}
else {
while (!in.eof()) {
in >> s[i].studentId >> s[i].test1 >> s[i].test2 >> s[i].test3;
s[i].avg = double(s[i].test1 + s[i].test2 + s[i].test3) / 3;
i++;
}
}
return i;
}
//Selection sort function
//Sosrt and display data according id
//Display number of comparisons
void selectionSort(Student s[],int size) {
Student temp;
int counter = 0;
for (int i = 0; i<size; i++)
{
for (int j = i + 1; j<size; j++)
{
counter++;
if (s[i].studentId>s[j].studentId)
{
temp.studentId = s[i].studentId;
temp.test1 = s[i].test1;
temp.test2 = s[i].test2;
temp.test3 = s[i].test3;
temp.avg = s[i].avg;
s[i].studentId = s[j].studentId;
s[i].test1 = s[j].test1;
s[i].test2 = s[j].test2;
s[i].test3 = s[j].test3;
s[i].avg = s[j].avg;
s[j].studentId = temp.studentId;
s[j].test1= temp.test1;
s[j].test2 = temp.test2;
s[j].test3 = temp.test3;
s[j].avg = temp.avg;
}
}
}
for (int i = 0; i < size; i++) {
cout << s[i].studentId << " " << s[i].test1 << " " << s[i].test2 << " " << s[i].test3 << " " << s[i].avg << endl;
}
cout << "Number of comparisons=" << counter << endl << endl;
}
//This used to sort array according to avg
//This call inside 70 and 85 average found method
bool sortAverage(Student a, Student b)
{
return (a.avg < b.avg);
}
//Find all students whose average >=70
void greater70(Student s[],int size) {
sort(s,s+size,sortAverage);
binarySearchDisplay(s, size, 70.0);
}
//Find all students whose average >=85
void greaterEqual85(Student s[],int size) {
sort(s, s + size, sortAverage);
binarySearchDisplay(s, size, 85.00);
}
//Binary search performed based on id
void binarySearch(Student s[], int n, int search)
{
int first = 0;
int last = n - 1;
int middle = (first + last) / 2;
while (first <= last)
{
if (s[middle].studentId < search)
{
first = middle + 1;
}
else if (s[middle].studentId == search)
{
cout << s[middle].studentId << " " << s[middle].test1 << " " << s[middle].test2 << " " << s[middle].test3 << " " << s[middle].avg << endl;
break;
}
else
{
last = middle - 1;
}
middle = (first + last) / 2;
}
if (first > last)
{
cout << "Not found! " << search << " is not present in the list."<<endl;
}
}
|