Like Tree1Likes
  • 1 Post By qny

functions with arrays

This is a discussion on functions with arrays within the C Programming forums, part of the General Programming Boards category; Just learning functions and keep getting lost in the variables. So i took a step back and just tried to ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    10

    functions with arrays

    Just learning functions and keep getting lost in the variables. So i took a step back and just tried to do a very basic adding function and its still not working. Where am i going wrong?


    Code:
    #include <stdio.h>
    
    
    #define SIZE 50
    
    
    
    
    int sum_array(int a[], int b);
    
    
    int main( void )
    {
        int n,i;
        int numbers[n];
       
            printf( "How many numbers would you like to add?: " );
            scanf( "%d", &n );
    
    
          for ( i = 0; i < n; i++ ) {
                printf( "Enter number %d: ", i + 1 );
                scanf( "%d", &numbers[i] );
            }
    
    
    
    
    
    
              printf( "The total of all the numbers is %d\n",sum_array(numbers,n));
    
    
        return 0;
    }
    
    
    
    
    int sum_array(int a[], int b)
    {
        int i, sum =0;
        for ( i = 0; i < b; i++ ) {
            sum += a[i];}
            return sum;
           
     
    }
    Last edited by confusedd; 11-03-2012 at 11:11 AM.

  2. #2
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Code:
    int sum_array(int a[], int b)
    {
        int i, sum =0;
        for ( i = 0; i < b; i++ ) {
            sum += a[i];
            return sum;
         }
      
    }
    Your return is inside the loop. This way only the first time through the loop is executed and the loop terminates with the return.
    In other words, the return is executed when i is 0 ... and i never gets to be 1, or 2, or whatever.
    stahta01 likes this.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    10
    Ya, I just caught that one myself and edited it, however it's still not working. ?

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    Show the "fixed" code, and tell us how it's "still not working".

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    10
    It executes and asks for the numbers, but doesnt add them. I just get a warning and compiler stops working with a "**process returned**" error.

    Code:
    #include <stdio.h>
    
    
    #define SIZE 100
    
    
    
    
    int sum_array(int a[], int b);
    
    
    int main( void )
    {
        int n,i;
        int numbers[n];
       
            printf( "How many numbers would you like to add?: " );
            scanf( "%d", &n );
    
    
          for ( i = 0; i < n; i++ ) {
                printf( "Enter number %d: ", i + 1 );
                scanf( "%d", &numbers[i] );
            }
    
    
    
    
    
    
              printf( "The total of all the numbers is %d\n",sum_array(numbers,n));
    
    
        return 0;
    }
    
    
    
    
    int sum_array(int a[], int b)
    {
        int i, sum =0;
        for ( i = 0; i < b; i++ ) {
            sum += a[i];}
            return sum;
         
     
    }

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by confusedd View Post
    Code:
        int n,i;
        int numbers[n];
    n is uninitialized and has an undefined value. Therefore your numbers array has an undefined (unknown) size. It might be 0 or 1000000 or anything else. You also cannot resize the array later by changing the value of n.

  7. #7
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by confusedd View Post
    It executes and asks for the numbers, but doesnt add them. I just get a warning and compiler stops working with a "**process returned**" error.
    Sorry. No!

    Either it executes after the compiler produced an executable or the compiler failed to produce an executable.
    You simply cannot have both (compier failing to produce an executable and the executable executing).

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    10
    Ah! Thank you Christop!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with arrays and functions
    By joeman in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2010, 10:17 AM
  2. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 03:29 PM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 05:10 PM
  4. help with arrays and functions
    By DoItAllMom115 in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2003, 03:01 PM
  5. Arrays & functions
    By Shila in forum C Programming
    Replies: 8
    Last Post: 10-20-2002, 01:21 PM

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