Having an issue with searching through an array

so im about done writing this program but ive run into a problem near the end. When i run the program and the results are not entirely correct. the program does not seem to display the students with the correct averages. also when attempting to search from a number near the bottom of the list of ids it seems that it does not exist. I have attached all my code and i will also put the input file info here in the description. I hope someone here can spare the time to give me a hand!

-----pr2data.txt---------
1234 52 70 75
2134 90 75 90
3124 90 95 98
4532 11 17 81
5678 20 12 45
6134 34 45 55
7874 60 100 56
8026 70 10 66
9893 34 9 77
2233 78 20 78
1947 45 40 88
3456 78 55 78
2877 55 50 99
3189 87 94 74
2132 75 100 80
4602 89 50 91
3445 78 60 78
5405 11 11 0
4556 78 20 10
6999 88 98 89

-----here are the results i get when running the program-----
After Selection sort:
1234 52 70 75 65.6667
1947 45 40 88 57.6667
2132 75 100 80 85
2134 90 75 90 85
2233 78 20 78 58.6667
2877 55 50 99 68
3124 90 95 98 94.3333
3189 87 94 74 85
3445 78 60 78 72
3456 78 55 78 70.3333
4532 11 17 81 36.3333
4556 78 20 10 36
4602 89 50 91 76.6667
5405 11 11 0 7.33333
5678 20 12 45 25.6667
6134 34 45 55 44.6667
6999 88 98 89 91.6667
7874 60 100 56 72
8026 70 10 66 48.6667
9893 34 9 77 40
4203394 0 4667536 0 1.55585e+006
Number of comparisons=210

Display Student details of average greater than equal 70:
3189 87 94 74 85
3445 78 60 78 72
3456 78 55 78 70.3333
Display Student details of average greater than equal 85:
3189 87 94 74 85
Enter the student id to search:9893
Not found! 9893 is not present in the list.
Do you want to serach again(Y/N):



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;
   }
}
The error is at the returned value of 'readFile(). It's some sophisticated: the eof() occurs not when you read the last number, but when you try to read further of the stream. So let's us examine the base case, that the input file is empty: If you open it, the file is at good() stage which lets you branch into line 106. Now you try to read some values (at line 107). Although you get now an eof() and nothing will be read in, you increment your 'size' variable.

Try to change your reading from line 107 to:
1
2
3
4
5
    while (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++;
    }


Also at your selectionSort(), it's sufficient to write
1
2
3
4
5
6
    if (s[i].studentId>s[j].studentId)
    {
        temp = s[i];
        s[i] = s[j];
        s[j] = tmp;
    }


*edit*
It seems that the student array order got resorted at greater70() and greaterEqual85(). Hence the list is not more in studentId order and thus your binarySeach fails
Last edited on
Topic archived. No new replies allowed.