Finding largest number in array

This is a discussion on Finding largest number in array within the C Programming forums, part of the General Programming Boards category; Why can't I just put grade[0] in the if statement below inside the for loop? Different code from my project ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    4

    Question Finding largest number in array

    Why can't I just put grade[0] in the if statement below inside the for loop?

    Different code from my project but illustrates what I'm talking about.
    Code:
    /*
    Determine the average of a elements of an array.
    Display the highest grade
    */
    
    #include <stdio.h>
    
    int main(void){
    	const int NUM_ELEMS = 10;
    	
    	double grade[NUM_ELEMS]; 
    	double h_grade;
    	double average;
    	double sum = 0;
    	int exam;
    	
    	
    	h_grade = grade[0]; 
    //for some weird reason I can't just put grade[0] in if statement below
    	
    	for (exam = 1; exam <= NUM_ELEMS; exam++){
    		printf("Enter grade for exam %d: ", exam);
    		scanf("%lf", &grade[exam]);
    		sum = sum + grade[exam];
    		
    		if (grade[exam] > h_grade){
    			h_grade = grade[exam];
    		}
    		
    	}
    	
    	average = sum / NUM_ELEMS;
    	
    	printf("Average %.1lf\n", average);
    	printf("Highest grade %.1lf\n", h_grade);
    
    	return 0;	
    }

    for example, if I just put it like this..it will just give me the last number I entered

    Code:
    		if (grade[exam] > grade[0]){
    			h_grade = grade[exam];
    		}
    But...

    when I assigned grade[0] to a variable, it does the trick. It displays the highest value of the array with no problems.

    I'm sure I'm not understanding a concept somewhere but it's not that obvious to me right now. Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because when you use grade[0] you are no longer trying to find the largest item in the list, but the last item on the list larger than the first one. If you want to find the largest item in the list, you must compare with the largest-found-so-far, and the largest-found-so-far is not (always) grade[0].

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    That way, you test whether the current value is higher than the first value. If it is, it will be stored. The proper version compares it to the current highest value. If this one exceeds it, it is saved, and it is compared to this one later.

    Imagine the array:
    1 8 3
    It will compare 3 to 1. Since 3>1, it will assume it's the highest so far in the wrong version.

    In the correct version, it will set the highest to the first value (which is 1). Then, since 8>1, it will set the highest to 8. Then, it will compare the highest - not the first - with the 3. But since 3<8, it will not change the current highest value.

    Try to find out what the loop does for those values for yourself.


    Hope that makes sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  2. Largest number program
    By rebel in forum C++ Programming
    Replies: 10
    Last Post: 12-01-2005, 03:20 AM
  3. Replies: 3
    Last Post: 03-29-2005, 03:24 PM
  4. finding the element number in an array
    By tommy69 in forum C Programming
    Replies: 7
    Last Post: 04-02-2004, 03:26 AM
  5. can u help me with pointers?plzzzzzzzz
    By itcs in forum C Programming
    Replies: 7
    Last Post: 07-11-2003, 01:29 AM

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