1. Engineering
  2. Computer Science
  3. a complete c program runningavelocalc which should read integers from...

Question: a complete c program runningavelocalc which should read integers from...

Question details
a) Complete C program runningAveLocal.c, which should read integers from the standard input, and computes the running (current) average of the input integers. The program terminates when a -1 is entered. Observe • how the code display the running average with 3 decimal points. • how a pre-processing macro MY_PRINT(x,y,z) printf( …… )is defined, which displays the result as shown in the sample outputs. (Thus, the program use MY_PRINTF, rather than printf() to display averages.)  define a function double runningAverage(int currentSum, int inputCount)which, given the current sum currentSum and the number of input inputCount, computes and returns the running average in double. The current sum and input count are maintained in main. See the code below:
#include <stdio.h>
#define MY_PRINT(x,y,z)  printf("running average is %d / %d = %.3f\n", x,y,z)
double r_avg(int, int);
int main(int argc, char *argv[])
{
      int input; int count=0; int sum=0;
      double resu;
      printf("enter number (-1 to quit):  ");
      scanf("%d", &input);
      while(input != -1)
      {
          ......
          ......
          resu = r_avg(sum, count);
          My_PRINT(sum, count, resu);  
          /* read again */
          printf("enter number (-1 to quit):  
          scanf("%d", &input);
       }  
       return 0;
}
double r_avg(int sum, int count)
{
} 

output: enter number (-1 to quit): 10

running average is 10 / 1 = 10.000 F

enter number (-1 to quit): -1   

part b):

Modify the above program, simplifying communications between functions. Implementation • name the program runningAveLocal2.c. • define a function double runningAverage(int currentInput),which, given the current input currentInput, computes and returns the running average in double. Notice that compared against the previous program, this function takes only one argument current input and does not take current sum and input count as its arguments. In such a implementation, current sum and input count are not maintained in main. Instead, main just pass currentInput to runningAverage(), assuming that runningAverage() somehow maintains the current sum and input count info. • do not use any global variable. How can runningAverage maintain the current sum and input count info? Hint: static can be used to local variables to make their lifetime permanent.

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution