Computer Science Group
Question:
Write a program that accepts values for a floating point array of any size, finds the sum, mean, and smallest values and display them.
Answers:
-
eNotes Editor
Posted by enotechris on Friday November 28, 2008 at 4:44 AM/* FunWithArray.c */
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZEOFARRAY 100
int main()
{
int intArraySize, count;
double dblArraySum, dblArrayMean, dblArraySmallest = 0.;
double list[MAXSIZEOFARRAY];
/* Read in a value for intArraySize */
printf("nHow many numbers do you want in the array? ");
scanf("%d", &intArraySize);
/* Check that intArraySize is not too large or too small */
if ((intArraySize > MAXSIZEOFARRAY) || (intArraySize <= 0))
{
printf("nError: Array size is too small or too large.n");
exit(1);
}
/* Read in and print off the numbers */
for (count = 0; count < intArraySize; ++count)
{
printf("element = %d value = ", count + 1);
scanf("%lf", &list[count]);
dblArraySum += list[count];
/* Find the smallest value in the array */
if list[count] < dblArraySmallest
{
dblArraySmallest = list[count]
}
}
/* Calculate and display the sum mean smallest */
printf("nThe sum is: %5.2fnn", dblArraySum);
dblArrayMean = dblArraySum / (double) intArraySize;
printf("nThe mean is: %5.2fnn", dblArrayMean);
printf("nThe smallest value is: %5.2fnn", dblArraySmallest);
return 0;
}
/ * This should generate something like this: */
How many numbers in the array? 5
element = 1 value = 4.6
element = 2 value = -2.3
element = 3 value = 8.7
element = 4 value = 0.12
element = 5 value = -2.7
The sum is: 8.42
The mean is: 1.684
The smallest value is: -2.7


