I seem to be stuck on my switch/case statement and my binary search for highest test score. I sort the test scores high-to-low before I do the binary search, but it just returns a high score of 70, student ID of 70 and a grade of F when they should be 92, 1955, and A, respectively. Additionally, the switch/case statement just gives me one count for each grade letter and not the 2 As, 2 Bs, 3 Cs, 1 D, and 3 Fs I expect. Can someone provide a nudge? Thanks.
Input data:
1653
77
1945
72
1020
50
1955
92
1030
40
1200
72
1500
91
1100
90
1900
81
1005
95
1790
43
1800
85
0
Code:
#include <stdio.h>
#define MAX_ENTRIES 50
int high_score_search (int test_score[], int student_ID[], int count);
char char_val_func (char grade[], int count);
int main (void)
{
/*I need to set my array to allow for up to 50 students and their
test scores and set my "count" variable as well as the i. There will
also need to be a variable for average test score, highest score,
and the corresponding grade values. */
int student_ID[MAX_ENTRIES]; //student ID counter
int test_score[MAX_ENTRIES]; //test score counter
int count = 0; /*number of elements in ID, score
and grade arrays*/
int high_score; //one test score to rule them all
int high_stud_ID; //you're the best around. Nothin's gonna keep you down
int A, B, C, D, F; //the number of each grade letter
char grade[MAX_ENTRIES]; //total number of grades
char high_grade; //you're the tops
count=fill_array(student_ID, test_score); //invoke the function to fill my arrays
high_score=high_score_search(test_score, student_ID, count); //display the highest score...
high_stud_ID=high_score_search(test_score, student_ID, count); //the corrosponding student ID...
high_grade=high_score_search(test_score, student_ID, count); //and with highest grade letter
printf("The highest test score is %i.\n", high_score);
printf("The student ID with the highest test score is %i.\n", high_stud_ID);
printf("The highest letter grade is %c.\n", high_grade);
A=char_val_func(grade, count); //Print the number of each grade letter from the char_val_func function...
printf ("The number of As is: %2i\n", A);
B=char_val_func(grade, count);
printf ("The number of Bs is: %2i\n", B);
C=char_val_func(grade, count);
printf ("The number of Cs is: %2i\n", C);
D=char_val_func(grade, count);
printf ("The number of Ds is: %2i\n", D);
F=char_val_func(grade, count);
printf ("The number of Fs is: %2i\n", F);
return 0;
}
int high_score_search (int test_score[], int student_ID[], int count)
{
int i;
int first = 0;
int last = count - 1;
int middle;
int high_score = 0;
int high_stud_ID = 0;
char high_grade;
high_score = test_score[0];
for(i = 0; i < count; ++i)
{
if( test_score[i] > high_score )
{
high_score = test_score[i];
high_stud_ID = student_ID[i];
return high_stud_ID;
}
if ( high_score >= 90 )
high_grade = 'A';
else if ( high_score >= 80 )
high_grade = 'B';
else if
( high_score >= 70 )
high_grade = 'C';
else if
( high_score >= 60 )
high_grade = 'D';
else if
(high_score <= 59 )
high_grade = 'F';
return high_grade;
}
while (first <= last)
{
middle = (first + last) / 2;
if (high_score == test_score[middle])
{
return high_score;
}
else (high_score > test_score[middle]);
{
first = middle + 1;
}
}
return high_score;
} //end high_score_search
char char_val_func (char grade[], int count)
{
int i = 0;
int A;
int B;
int C;
int D;
int F;
while (i < count)
{
switch (grade[i])
{
case 'A':
++A;
return A;
case 'B':
++B;
return B;
case 'C':
++C;
return C;
case 'D':
++D;
return D;
case 'F':
++F;
return F;
}
}
}