Like Tree1Likes
  • 1 Post By hex_dump

Need help in explaining passing an array to a function

This is a discussion on Need help in explaining passing an array to a function within the C Programming forums, part of the General Programming Boards category; Please refer to the following code: Code: #include <stdio.h> #define MAX 10 int array[MAX+1], count; int largest(int num_array[]); int main( ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    11

    Need help in explaining passing an array to a function

    Please refer to the following code:

    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?

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

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    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
    Posts
    88
    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.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define SIZE 25
    #define RAND 251
    
    int find_largest(int*, int);
    
    int main(void){
    
       srand(time(NULL));
       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;
    
    }
    ntdvs likes this.

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, 12:32 PM
  3. Passing array into function
    By kulfon in forum C Programming
    Replies: 2
    Last Post: 11-30-2010, 10: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, 04:15 AM

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