Q:

C program to find sum of the array elements (pass an integer array to a function and return the sum)

0

C program to find sum of the array elements (pass an integer array to a function and return the sum)

Given an array of integers (one dimensional array) and we have to find sum of all elements using user define function in C.

Here, we will pass array to the function and function will return the sum of the elements.

Here is the function that we have used in the program,

int sum_of_elements(int *arr , int n)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to find the sum of all array elements using function in C

/*  
* C program to a pass integer array elements to 
* a function and return the sum of all elements
*/

#include <stdio.h>

// function to calculate the sum of array
// elements
int sum_of_elements(int *arr , int n)
{
   int i=0,sum=0;
   
   for(i=0; i<n ; i++)
   {
       sum = sum + arr[i];
   }
   
   return sum;
}

// main function
int main()
{
    int total = 0;
    int array[10] = {1,2,3,4,5,6,7,8,9};
    
    // Passing array to Function
    total = sum_of_elements(array,9);
	
    printf("\nThe sum of all array elements is : %d",total);
	
    return 0;
}

Output

The sum of all array elements is : 45

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to swap elements of two integer arrays u... >>
<< C program to pass a one dimensional (1D) array to ...