Need help with binary search


int main()
{
StudentRecord student[100];
//int num_std;
char key[10];
int number_of_students=0;
enterKey(key);
getInformation(student,number_of_students);
calculateAveAndLetter(student,number_of_students, key);
displayResults(student,number_of_students);



return 0;
}
//Populates array of strutures with the student name, ID, and Answers from user
void getInformation(StudentRecord student[],int&number_of_students)
{
char enter;
int i=0;
cout<<"Would you like to enter the student information?"<<endl;
cin>>enter;
if (enter=='y'||enter=='Y')
{
for(i=0; i<100; i++)
{
cout<<"Enter student "<<i+1<<" information:"<<endl;
cout<<"Enter name: ";
cin.get();
getline(cin,student[i].name);
cout<<"Enter ID: ";
cin>>student[i].ID;
cout<<"Enter this students answers: ";
cin>>student[i].answer.answer1;
cin>>student[i].answer.answer2;
cin>>student[i].answer.answer3;
cin>>student[i].answer.answer4;
cin>>student[i].answer.answer5;
cin>>student[i].answer.answer6;
cin>>student[i].answer.answer7;
cin>>student[i].answer.answer8;
cin>>student[i].answer.answer9;
cin>>student[i].answer.answer10;
number_of_students = i+1;
cout<<"Would you like to enter the student information?"<<endl;
cin>>enter;

if (enter=='y'||enter=='Y')
{
continue;
}
else if(enter=='n'||enter=='N')
{
return;
}
}
// num of student
}
else if(enter=='n'||enter=='N')
{
return;
}

}

//Allows user to enter the Answer Key
void enterKey(char key[])
{
cout<<"Enter the answer key."<<endl;
for(int i = 0; i<10; i++)
{
cout<<"Answer "<<i+1<<": ";
cin>>key[i];
}
}
//Calculates average grade for each student
void calculateAveAndLetter(StudentRecord student[], int number_of_students, char key[])
{
for(int i=0;i<number_of_students;i++)
{
student[i].score = 0;
student[i].average= 0;
if(key[0]==student[i].answer.answer1)
{
student[i].score += 10;
}
if(key[1]==student[i].answer.answer2)
{
student[i].score +=10;
}
if(key[2]==student[i].answer.answer3)
{
student[i].score +=10;
}
if(key[3]==student[i].answer.answer4)
{
student[i].score +=10;
}
if(key[4]==student[i].answer.answer5)
{
student[i].score +=10;
}
if(key[5]==student[i].answer.answer6)
{
student[i].score += 10;
}
if(key[6]==student[i].answer.answer7)
{
student[i].score +=10;
}
if(key[7]==student[i].answer.answer8)
{
student[i].score +=10;
}
if(key[8]==student[i].answer.answer9)
{
student[i].score +=10;
}
if(key[9]==student[i].answer.answer10)
{
student[i].score +=10;
}
student[i].average=student[i].score;
if(student[i].average>=90)
{
student[i].letter_grade='A';
}
else if(student[i].average>=80 && student[i].average<90)
{
student[i].letter_grade='B';
}
else if(student[i].average>=70 && student[i].average<80)
{
student[i].letter_grade='C';
}
else if(student[i].average>=60 && student[i].average<70)
{
student[i].letter_grade='D';
}
else if(student[i].average<60)
{
student[i].letter_grade='F';
}

}


}
//This function is bonus. Sorts array of structures in ascending order byName.
//void sortRecordsByName(StudentRecord student[],int number_of_students)
//{
// bool swap;
// int temp;
//
// do {
// swap=false;
// for (int i = 0; i <(number_of_sudents - 1); i++)
// {
// if (student[i] > student[i+1])
// {
// temp =student[i];
// student[i]=student[i+1];
// student[i+1]=temp;
// swap=true;
// }
// }
// }while (swap);
//}


//Displays all information for each student
void displayResults(StudentRecord student[],int number_of_students)
{
cout<< " StudentID StudentName Answers TotalPts Average LetterGrade" <<endl;
cout<< " --------- ----------- ------- -------- ------- -----------" <<endl;
for (int i=0; i<number_of_students; i++)
{
cout <<" "<< student[i].ID<<" ";
cout << student[i].name;
cout <<" "<< student[i].answer.answer1<<"";
cout <<student[i].answer.answer2<<"";
cout << student[i].answer.answer3<<"";
cout <<student[i].answer.answer4<<"";
cout <<student[i].answer.answer5<<" ";
cout<<student[i].score<<" ";
cout<<student[i].average<<" ";
cout<<student[i].letter_grade<<endl;
}
for (int i=0; i < number_of_students; i++)
{
if(student[i].average>=70)
{
cout << "Students Admitted to the Graduated Program"<<endl;
cout<< " StudentID StudentName Answers TotalPts Average LetterGrade" <<endl;
cout<< " --------- ----------- ------- -------- ------- -----------" <<endl;
cout <<" "<< student[i].ID<<" ";
cout << student[i].name;
cout <<" "<< student[i].answer.answer1<<"";
cout <<student[i].answer.answer2<<"";
cout << student[i].answer.answer3<<"";
cout <<student[i].answer.answer4<<"";
cout <<student[i].answer.answer5<<" ";
cout<<student[i].score<<" ";
cout<<student[i].average<<" ";
cout<<student[i].letter_grade<<endl;
}
else if(student[i].average>=60)
{
cout << "Student with Conditional Admission to the Graduated Program:"<<endl;
cout<< " StudentID StudentName Answers TotalPts Average LetterGrade" <<endl;
cout<< " --------- ----------- ------- -------- ------- -----------" <<endl;
cout <<" "<< student[i].ID<<" ";
cout << student[i].name;
cout <<" "<< student[i].answer.answer1<<"";
cout <<student[i].answer.answer2<<"";
cout << student[i].answer.answer3<<"";
cout <<student[i].answer.answer4<<"";
cout <<student[i].answer.answer5<<" ";
cout<<student[i].score<<" ";
cout<<student[i].average<<" ";
cout<<student[i].letter_grade<<endl;
}
else if(student[i].average>=0)
{
cout << "Students Not Allowed Admission:"<<endl;
cout<< " StudentID StudentName Answers TotalPts Average LetterGrade" <<endl;
cout<< " --------- ----------- ------- -------- ------- -----------" <<endl;
cout <<" "<< student[i].ID<<" ";
cout << student[i].name;
cout <<" "<< student[i].answer.answer1<<"";
cout <<student[i].answer.answer2<<"";
cout << student[i].answer.answer3<<"";
cout <<student[i].answer.answer4<<"";
cout <<student[i].answer.answer5<<" ";
cout<<student[i].score<<" ";
cout<<student[i].average<<" ";
cout<<student[i].letter_grade<<endl;
}
}

}
//searches student by ID
int binarySearch(StudentRecord student[], int number_of_students, int ID)
{
int low = 0;
int high = number_of_students - 1;

int mid;

while (low <= high)
{
mid = (low + high) / 2;

if(ID == student[mid])
{
return mid;
}
else if (ID > student[mid])
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}

return -1;
}
]
Topic archived. No new replies allowed.