Sunday, December 14, 2008

program to calculate sum all of the elments in an array

#include
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);
}

No comments:

Post a Comment