Hello, I have a problem and I am having trouble searching for the solution. I had a program to write that required me to sort an array of student Ids and the information associated with them. Then I would need to perform a sequential search and binary search. I use a structure to house the various variables that I am using.
Here are the operations of the program:
1. Fill structured array from the input file
2. Prints an unsorted array.
3. Sort the array
4. Print the sorted array.
5. Receive user input and search for a value in the array (sequential search)/
Print associated information *** I do this for 6 times.
6. Receive user input and search for a value in the array (binary search)
Print associated information *** I do this for 6 times.
Everything works fine until I get to the binary search which seems to lock up after performing the first or second iteration. The console output screen just stops and the cursor is blinking. There is no response, no return statement at the bottom, it just blinks taunting me. Sometimes it would work, however, it would eventually start to "lock up" (no clue what to call it). I have tried to play around with the student id numbers but it always ends up being weird. Maybe there is something with the for loop, but I don't see anything wrong with it.
I already turned in the assignment so this is really just for me because it is bugging me. Also, I would try to figure this out for myself but I am truly stumped and I am having a hard time trying to explain the problem. Also if I missed anything please let me know and I will post it. I didn't think I would need to post the whole program but I can if need be. Thank you for any help!
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
|
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int SIZE = 12;
struct Student
{
int id;
string name;
string email;
};
void printAuthorInfo ( );
void fillArray (ifstream &, Student []);
void printArray (Student[]);
void sortArray(Student []);
int searchList(const Student [], int);
int binarySearch (const Student [], int);
void printStudentInfo(Student [], int);
int main( )
{
int value,
binary_results,
sequential_results;
Student info[SIZE];
//*****other code stuff*****//
cout << "*****Binary Search*****" << endl << endl;
cout << "Please enter 6 student IDs" << endl;
for (int count = 0; count < 6; count++)
{
cout << "Student ID " << count + 1 << ":";
cin >> value;
binary_results = binarySearch(info,value); // function call
if (binary_results == -1)
cout << "Student Id not found" << endl << endl;
else
printStudentInfo(info, binary_results);
}
//***** function def *****//
int binarySearch (const Student info[], int value)
{
int first = 0,
last = SIZE - 1,
middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last / 2);
if (info[middle].id == value)
{
found = true;
position = middle;
}
else if (info[middle].id > value)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
|