Functions read ints until a failure occurs and return the


functions read ints until a failure occurs and return the number of ints successfully read. If no ints were successfully read in, the functions return zero and do not modify (mutate) the contents of any of the parameters. Otherwise, the functions mutate the contents of their (pointer) parameters as follows:

  1. int read_and_range(int *max, int *min, int *count_max, int *count_min)
  • *max: the largest number that was successfully read
  • *min: the smallest number that was successfully read
  • *count_max: the number of times the largest number was read
  • *count_min: the number of times the smallest number was read

Here is my solution, but it doesnt work, what is wrong?

#include

int read_and_calc_stats(int *sum, double *mean, double *var) {

  int count_total = 0;

 int sqrt_sum = 0;

 while(1) {

  int n ;

  scanf("%d", &n);

  if (scanf("%d",&n)==0 && count_total == 0) {

   return 0;

  } else if (scanf("%d",&n)==0) {

   break;

  } else {

   count_total++;

   *sum += n;

   sqrt_sum += n*n;

  }

 }

 if (count_total == 1) {

  *mean = *sum;

  *var = 0;

 } else {

  double result_1 = *sum;

  result_1 = result_1 / count_total;

  *mean = result_1;

  double result_2 = sqrt_sum;

  result_2 = result_2 / count_total;

  result_2 = result_2 - result_1 * result_1;

  *var = result_2;

 }

 return count_total;

}

int main(void) {

 int sum = 0;

 double mean = 0;

 double var = 0;

 int count = read_and_calc_stats(&sum, &mean, &var);

 printf("sum = %dn", sum);

 printf("mean = %.2fn", mean);

 printf("variance = %.2fn", var);

 printf("count = %dn", count);

}

Solution Preview :

Prepared by a verified Expert
Business Management: Functions read ints until a failure occurs and return the
Reference No:- TGS02822738

Now Priced at $10 (50% Discount)

Recommended (96%)

Rated (4.8/5)