Thread: Need help in explaining passing an array to a function

  1. #1
    Registered User
    Join Date
    Feb 2013

    Need help in explaining passing an array to a function

    Please refer to the following code:

    #include <stdio.h>
    #define MAX 10
    int array[MAX+1], count;
    int largest(int num_array[]);
    int main( void )
        for (count = 0; count < MAX; count++)
            printf("Enter an integer value: ");
            scanf("%d", &array[count]);
            if ( array[count] == 0 )
                count = MAX;        
        array[MAX] = 0;
        printf("\n\nLargest value = %d\n", largest(array));
        return 0;
    int largest(int num_array[])
        int count, biggest = -12000;
        for ( count = 0; num_array[count] != 0; count++)
            if (num_array[count] > biggest)
                biggest = num_array[count];
        return biggest;
    Can anybody please explain how the for loop at the largest() function searches for the largest number entered in the array?

    Last edited by ntdvs; 03-04-2013 at 04:41 PM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Melbourne Australia
    Global variables are initialised to 0 and the last element of the array is preserved as 0 (note that this is means that the user may not enter 0, so it appears to be flawed program)

    The for loop goes through the array until the 0 is found, replacing the variable "biggest" with any number it finds that is bigger than the current value in " biggest"

    I think that the program should either pass a parameter of the array size, or make use of the define MAX.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Personally I find the random function very helpful when working with integer arrays and trying to understand them. I second Click_here's answer and recommendation. Have a look at this code, play around with the SIZE and RAND symbolic constants and see what it outputs.
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define SIZE 25
    #define RAND 251
    int find_largest(int*, int);
    int main(void){
       int storage[SIZE];
       int i;
       int j;
       printf("The contents of the array: ");
       for(i=0;i<SIZE; i++){
          j =(rand() % RAND );
          storage[i] = j;
          printf("%2.2d...\n", *(storage + i) );
       printf("\nFinally the largest int is %2.2d...\n", find_largest(storage, SIZE) );
      return 0;
    int find_largest(int* the_array, int elements){
       int cur;
       int largest;
       largest = the_array[0]; 
       printf("Initially the largest is %2.2d..\n", largest);
       for(cur = 1; cur < elements; cur++){
          if(the_array[cur] > largest){
             largest = the_array[cur];
             printf("The largest is now %2.2d...\n", largest);
      return largest;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-25-2012, 01:31 AM
  2. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 01:32 PM
  3. Passing array into function
    By kulfon in forum C Programming
    Replies: 2
    Last Post: 11-30-2010, 11:08 PM
  4. Passing an array into a function
    By Ryston in forum C++ Programming
    Replies: 4
    Last Post: 08-29-2006, 05:20 AM
  5. passing an int array into function
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2004, 05:15 AM