array

This is a discussion on array within the C Programming forums, part of the General Programming Boards category; Hey guys im trying to add numbers together which get stored in the array how can i do that? Iv ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    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;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 02: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

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