Thread: array

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    array

    Hey guys im trying to add numbers together which get stored in the array how can i do that?

    Iv done that below but i get junk value when it gets printed out?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define sampleSize 10
    int rainfall(int rainfall[sampleSize]);
    
    
    int main(void)
    {
        int arr[sampleSize];
        
        double av;
        
        av = rainfall(arr);
        
        return 0;
        
    }
    
    int rainfall(int rainfall[sampleSize])
    {
      double average = 0.0;
      double sum = 0.0;
      int i;
      int result;
      
      printf("Enter 10 numbers\n");
      scanf("%i",rainfall);
      
      
          result = rainfall[0] + rainfall[1]+ rainfall[2]+
           + rainfall[3] 
           +rainfall[4]
           + rainfall[5]
           + rainfall[6]
            + rainfall[7]
             + rainfall[8]
              + rainfall[9];
           
          printf("sum: %d", result);
      
      return 0;    
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    double arrSum(double* array, size_t len)
    {
       double sum = 0.0;
       size_t i;
       for(i=0;i<len;i++)
       {
          sum += array[i];
       }
       return sum;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    printf("Enter 10 numbers\n");
    scanf("&#37;i",rainfall);
    You need a for loop to read each one

    Eg.
    scanf( "%i", &rainfall[num] );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM