Question #44347

Program to calculate sum and average of the elements stored in an array?

Expert's answer

#include <stdio.h>

int main()

{

float someArray[5] = {1.2, 2.2, 2.3, 3.3, 3.6};

float sum = 0; // sum of the elements stored in array

float average; // average of the elements stored in array

int i;

int size = sizeof(someArray) / sizeof(someArray[0]); // size of array

// loop to compute sum

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

sum = sum + someArray[i];

}

average = sum / size;

//output

printf("Array: ");

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

printf("%.3f ", someArray[i]);

}

printf("\n");

printf("Sum: %.3f\n", sum);

printf("Average: %.3f ", average);

return 0;

}

int main()

{

float someArray[5] = {1.2, 2.2, 2.3, 3.3, 3.6};

float sum = 0; // sum of the elements stored in array

float average; // average of the elements stored in array

int i;

int size = sizeof(someArray) / sizeof(someArray[0]); // size of array

// loop to compute sum

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

sum = sum + someArray[i];

}

average = sum / size;

//output

printf("Array: ");

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

printf("%.3f ", someArray[i]);

}

printf("\n");

printf("Sum: %.3f\n", sum);

printf("Average: %.3f ", average);

return 0;

}

## Comments

## Leave a comment