Write a function named addarray() that returns the sum of the elements of an arr
ID: 3869854 • Letter: W
Question
Write a function named addarray() that returns the sum of the elements of an array of int values.Your function should take to two parameters, the array, and the number of elements in the array. Make your function work with the program below:
/* Synopsis: -Displays the value returned by the function addarray() with two different set of parameters.
Objective: -To provide a test program for the function addarray() written for an excercise. The anwsers should be 55 and 0.*/
#include <stdio.h>
/*Function Prototypes*
int addarray(int*, int);
int main(void)
{
int array1 [10] = { 1,2,3,4,5,6,7,8,9,10},
array2[4] = { 0,0,0,0};
printf("The sum of the elements in array1 is %d. ",
addarray(array1,10) );
printf("The sum of of the elements in array2 is %d. ",
addarray(array2, 4) );
return 0;
}
Explanation / Answer
#include <stdio.h>
/*Function Prototypes*/
int addarray(int*, int);
int main(void)
{
int array1 [10] = { 1,2,3,4,5,6,7,8,9,10},
array2[4] = { 0,0,0,0};
printf("The sum of the elements in array1 is %d. ",
addarray(array1,10) );
printf("The sum of of the elements in array2 is %d. ",
addarray(array2, 4) );
return 0;
}
int addarray(int a[], int n) {
int sum = 0;
int i;
for(i=0;i<n; i++){
sum = sum + a[i];
}
return sum;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.