Extracting lowest and highest score from array

This is a discussion on Extracting lowest and highest score from array within the C Programming forums, part of the General Programming Boards category; I have posted before on this forum in this thread . This is basically a follow-up, but the old thread ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    27

    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.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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])
    [edit]
    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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    27
    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?
    Last edited by sjalesho; 10-09-2003 at 07:00 AM.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    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.
    Beware the fury of a patient man.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    27
    Thanks a lot, my code is now finished and I'll adjust the comments.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    How would u change that code to VBScript to run via Windows Host using Notepad?

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) This is not a VB support forum.
    2) That is an 8 year old thread.
    3) It's "You", not "u".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-22-2009, 02:54 PM
  2. Sorting an int array highest to lowest
    By switchback in forum C Programming
    Replies: 3
    Last Post: 07-27-2008, 04:30 AM
  3. Trying to sort numbers from lowest to highest
    By jw232 in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2008, 04:03 PM
  4. Lowest Score Drop - C++
    By getName(C-Dub) in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2008, 07:02 PM
  5. Can't find the lowest number in array?
    By Nate2430 in forum C++ Programming
    Replies: 1
    Last Post: 11-20-2001, 10:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21