Binary Search Help

Pages: 12
oh okay thanks.

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);

}

Idk what to write to sort the testScores
Just swap the values at count and count + 1 as you are doing with studentName.
Can you show it for me. i dont really understand
1
2
3
4
5
6
7
8
9
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);

	int iTemp = testScores[count];
	testScores[count] = testScores[count + 1];
	testScores[count + 1] = iTemp;
}


void main(){
int scores[5];
char students[5][6];
char key[6];
char answer[4] = "yes";
int size;
int index;

cout << "Enter size for arrays: ";
cin >> size;
getData(size,scores,students);
printData(students,scores);
sortData(students, scores, 5);

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;



}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;
		}
		else if (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?
Last edited on
No, you return a position, which is the same for the name and score array.

1
2
int found = searchData(/*...*/);
cout << studentName[position] << " " << testScores[position] << "\n";
where do i put that/
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.
so how do i write that into my program to show the name and testScores. im not getting it, its really late here in america for me
On main, after you read key:
1
2
index = searchData(students, size, key);
cout << students[index] << " " << scores[index] << "\n";

I'm 2 time zones ahead of you, so it's even latter here =).
Topic archived. No new replies allowed.
Pages: 12