Sums the elements of an array - C Array

C examples for Array:Introduction

Description

Sums the elements of an array

Demo Code

#include <stdio.h>
#define SIZE 10/*from w  w w .j ava2  s.c  om*/
int sum(int ar[], int n);
int main(void)
{
    int marbles[SIZE] = {210,110,15,319,14,116,191,216,131,201};
    long answer;
    
    answer = sum(marbles, SIZE);
    printf("The total number of marbles is %ld.\n", answer);
    printf("The size of marbles is %zd bytes.\n", sizeof marbles);
    
    return 0;
}

int sum(int ar[], int n)     // how big an array?
{
    int i;
    int total = 0;
    
    for( i = 0; i < n; i++)
        total += ar[i];
    printf("The size of ar is %zd bytes.\n", sizeof ar);
    
    return total;
}

Result


Related Tutorials