IHYPRESS.NET
C
HOME | ASP | C | CSS | HTML | JAVASCRIPT | PERL | PHP | PYTHON | RUBY | SVG
Earn Free Bitcoin
CLoops ❯ Sums and Averages
<04.05>
/* sums and averages */ /* this program calculates the average of all the numbers in a file of integers */
#include <stdio.h> int main (void) { int number, sum, count; double average; FILE *in; in = fopen ("numbers.data", "r"); /* sums and counts must always be initialized to zero */ sum = 0; count = 0; /* read number by number until the end of file */ while (fscanf (in, "%d", &number) != EOF) { /* count and sum the numbers */ sum = sum + number; count = count + 1; } fclose (in); /* final report */ printf ("There are %d numbers in the file.\n", count); printf ("The sum of all the numbers is %d.\n", sum); average = (double)sum / count; printf ("There average of all the numbers is %5.2lf.\n", average); return (0); }
Hergestellt in Deutschland / Made in Germany
numbers.data: 11 18 5 101 6 78 47 3
There are 8 numbers in the file. The sum of all the numbers is 269. There average of all the numbers is 33.63.
COPYRIGHT © 2015-2024 IHYPRESS.NET. A DIVISION OF IHY PRESS. ALL RIGHTS RESERVED.