# Extracting lowest and highest score from array

• 10-09-2003
sjalesho
Extracting lowest and highest score from array
I have posted before on this forum in this thread. This is basically a follow-up, but the old thread is not needed, because this is an isolated problom.

This is what I got now to extract the lowest and highest score of an 5 x 5 array.

Code:

```/* initialised the values to compute the highest and lowest score */    int low_score = 11;   int high_score = -1;   for(j = 0; j < NR_QUIZES ; j++)  {   total_score=0; /*reset total score for quizes */     for(i = 0; i < NR_STUDENTS ; i++) { {     total_score += score[i][j];     }                   /* Here is the highest and lowest score computed using 2 if statements */         if(score[i][j] < low_score)  { /* if the grade is lower than 11, then the low_score gets updated */                 low_score = score[i][j];                 }         if(score[i][j] > high_score) { /* if the grade is higher than -1, then the high_score gets updated */                 high_score = score[i][j];                 }         }           printf("\nQuiz %d: Lowest score = %d, Highest score = %d, Average score = %.1f", j+1, low_score, high_score,(float)total_score/NR_STUDENTS);         }```
It works for quiz 1, but it won't work for quiz 2 to 5. It keeps the highest score from quiz 1 for quiz 2 to 5. The lowest score from quiz 2 is used for quiz 2 to 5 also. I think the code is right, but I need to add more code probably to reset the highest and lowest score after each quiz. Right? (This is what I'm logically thinking).
I think it can be done by naming
Code:

```high_score=o; low_score=o;```
I have tried it several times, but it won't work. Tips appreciated.
• 10-09-2003
Hammer
Are you wanting to record the highest/lowest scores for each quiz? If so, you need an array to store these in:
Code:

```int LowScores[NR_QUIZES] = {0} int HighScores[NR_QUIZES] = {0} /* Note: all elements are initialised to 0 */ /* Then, in the loop, index the array using the i variable denoting the quiz number */ if(score[i][j] < LowScores[i])```

That sample won't work too well, as the low score array is initialised to 0. You can't get a score lower than zero, so the if statement will never be true. The best way to do this is to set the highest and lowest scores equal to the results of the first entry, then update them as needed when you go through the loop.
• 10-09-2003
sjalesho
I have also initialised the high_score and low_score, so basically I need to initialise 2 more? This is what I was thinking, but I didn't know how to do it. I'll try it out.

edit: the high_score and low_score for the first quiz, then update these till it gets higher or lower, how to code this and should I just put it below the 2 If statements?
• 10-09-2003
Zainny
Very simply, you need to reinitialise the variables high_score and low_score at the start of every quiz. Thus, you should have:
Code:

```low_score = 11; high_score = -1;```
directly underneath where you are saying total_score = 0;

Your comments could probably be a bit better also with regard to the if statements. The comments only are true in the first instance.

Cheers,
Z.
• 10-09-2003
sjalesho
Thanks a lot, my code is now finished and I'll adjust the comments. :)
• 03-01-2011
davewillis
How would u change that code to VBScript to run via Windows Host using Notepad?
• 03-01-2011
CommonTater
1) This is not a VB support forum.
2) That is an 8 year old thread.
3) It's "You", not "u".