Question #44350

write a Program in c to find the sum and average of 1D array?

Expert's answer

int calculateSum(int *arr, int size)

{

int sum = 0;

for(int i = 0; i < size; ++i)

sum += arr[i];

return sum;

}

int main()

{

const int size = 5;

int arr[size] = {1, 2, 3, 4, 5};

int sum = calculateSum(arr, size);

printf("sum: %i\navg: %f\n", sum, (float)sum / size);

return 0;

}

{

int sum = 0;

for(int i = 0; i < size; ++i)

sum += arr[i];

return sum;

}

int main()

{

const int size = 5;

int arr[size] = {1, 2, 3, 4, 5};

int sum = calculateSum(arr, size);

printf("sum: %i\navg: %f\n", sum, (float)sum / size);

return 0;

}

Learn more about our help with Assignments: C

## Comments

## Leave a comment