Write a program in C to calculate sum all of the elements in an array
#include <stdio.h>
void print_arr(int myArray[], int elements);
int sum_arr(int myArray[], int elements);
int main(void)
{
int myArray[5] = {78,34,25,98,12 };
int sum;
printf("Array info: ");
print_arr(myArray,5);
sum = sum_arr(myArray,5);
printf("The sum of the array is : %d\n",sum);
return 0;
}
void print_arr(int myArray[], int elements)
{
int i;
for(i = 0;i < elements;i++)
{
printf("%d ",myArray[i]);
}
printf("\n");
}
int sum_arr(int myArray[], int elements)
{
int i, sum = 0;
for(i = 0;i < elements;i++)
{
sum = sum + myArray[i];
}
return(sum);
}
Post a Comment