9951 explained code solutions for 126 technologies


cHow to find the sum of all the elements of an integer array?


int sum(int arr[], int n) {
    int sum = 0;
    for (int i=0; i<n; i++)
      sum += arr[i];
    return sum;
}ctrl + c
int arr[]

The name of the integer array of which sum is to be calculated

int n

Number of elements in the array named arr

for

For loop in C through which we can iterate multiple number of times


Usage example

#include <stdio.h>
int sum(int arr[], int n) {
    int sum = 0;
    for (int i=0; i<n; i++)
        sum += arr[i];
    return sum;
}
int main() {
    int my_array[9] = {1, 2, 3, 4, 6, 7, 8, 9, 10};
    int number_of_elements = 9;
    printf("%d: is the sum of the given array", sum(my_array, number_of_elements));
    return 0;
}
output
50: is the sum of the given array