9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia03/ex30.c (789B)
   1 //    Calculates the arithmetic mean of a certain amount of
   2 //    numbers. First ask for the quantity and then calculates
   3 //    it and prints it;
   4 
   5 #include <stdio.h>
   6 #include <stdlib.h>
   7 
   8 #define MAX_LEN 20
   9 
  10 int main (void) {
  11 
  12     int i, n;
  13     float x, suma, numbers[MAX_LEN];
  14     char buffer[MAX_LEN];
  15 
  16 //    Read the quantity of numbers to mean;
  17     if(fgets(buffer, sizeof(int) + 1, stdin) != NULL)
  18         n = atoi(buffer);
  19     else return 1;
  20 
  21 //    Read the values to mean  and stores it on numbers;
  22     for(i = 0, suma = 0; i < n; i++) {
  23         if(fgets(buffer, sizeof(int) + 1, stdin) != NULL) {
  24             numbers[i] = atof(buffer);
  25             suma += numbers[i];
  26         } else return 1;
  27     }
  28 //    Calculate the arithmetic mean and stores it on x;
  29     x = ((1 / (float)n) * suma); 
  30     
  31 //    Print the arithmetic mean;
  32     printf("x = %.3f\n", x);
  33     return 0;
  34 }