Question: In C Modify array.c so that it reads doubles from the standard input and computes their average. The number of doubles to read should first
In C
Modify array.c so that it reads doubles from the standard input and computes their average.
The number of doubles to read should first be read from the standard input. For example, a sample run might look like:
Provide a desired number of doubles? 5 Provide the doubles? 1.0 2.0 3.0 4.0 5.0 Data: 1.0 2.0 3.0 4.0 5.0 Average: 3.0
Note that scanf with the proper format string (i.e. one to read a double) will search for doubles in the standard input independent of the placement on the input line, so inputs should be accepted if they are on the same line or on different lines or some mix of these.
A maximum number of doubles that the program can handle is defined by the MAX_NUM_OF_CELLS definition. Your program should create an array with that many elements. It should also verify that the number of elements entered by the user is no larger than this limit and that the number of elements entered by the user is at least 1. If the user enters 0 or a negative input for the number of doubles, then the number read should default to 1. If the user enters a number which is too large, the number of inputs should default to MAX_NUM_OF_CELLS. In either of these cases, a message should be printed informing the user of their mistake, and of what was done to compensate for their mistake.
The program should print all of the input doubles on one line, separated by spaces, and should then print the average on the next line (see sample run above).
#include#define MAX_NUM_OF_CELLS 128 int main(void) { int number[MAX_NUM_OF_CELLS]; int sum = 0; for (int index = 0; index < MAX_NUM_OF_CELLS; index++) { number[index] = index; } /* now, number[index]=index; will cause error: why ?*/ for (int index = 0; index < MAX_NUM_OF_CELLS; index = index + 1) { sum += number[index]; /* sum array elements */ } printf("sum = %d ", sum); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
