Now how do i get my data to sort the test scores with the names they belong to.
This is what i have to sort my data
void sortData(char studentName[][6],int testScore[],int size){
bool swap;
char temp[6];
do {
swap = false;
for (int count = 0; count < (size - 1); count++) {
if (strcmp(studentName[count], studentName[count+1]) > 0) {
strcpy_s(temp,6, studentName[count]);
strcpy_s(studentName[count],6, studentName[count+1]);
strcpy_s(studentName[count+1],6, temp);
swap = true;
} // end if
} // end for
} while (swap);
do {
cout << "Enter a student name as search key: ";
cin >> key;
index = searchData(students, 5, key);
if (index == -1)
cout << "Not found." << endl;
else
cout << "Test score for "<< key << "is " << score[5]s << endl;
// end if
cout << "Do you want another search? yes/no: ";
cin >> answer;
}
while (strcmp(answer, "yes") == 0);
okay for this part now it wont show the name of the students name and the test score when it says "Test score for "<< key << "is " << score[5]s << endl;
int searchData(char studentName[][6], int size, char searchKey[]){
int first, last, mid;
bool found = false;
int position = -1;
first = 0;
last = size - 1;
while (first <= last && ! found) {
mid = (first + last) / 2;
if (strcmp(studentName[mid], searchKey) == 0) {
position = mid;
found = true;
}
elseif (strcmp(studentName[mid], searchKey) > 0)
last = mid - 1;
else
first = mid + 1;
// end if
} // end while
return position;
}
should there be something about testScore in this binary search?
No, I was just showing you how you use the value returned by searchData. If you order both arrays (name and score) correctly, the value the variable index gets from searchData on main is the index of the student on the name and score arrays.