L-5 MCS 260 Wed 29 Aug 2001
Below are listings for programs we discussed in class.
/* L-5 MCS 260 Wed 29 Aug 2001 : average of floats */
#include <stdio.h>
int main(void)
{
float x;
float sum = 0.0;
int cnt = 0;
printf("Give sequence of floats, terminate with RETURN+CTRL-D :\n");
while (scanf("%f", &x) == 1)
{
sum = sum + x;
cnt = cnt + 1;
}
printf("The average %f/%d = %f\n", sum, cnt, sum/cnt);
return 0;
}
The second program does the same, using operators :
/* L-5 MCS 260 Wed 29 Aug 2001 : average of floats, with operators */
#include <stdio.h>
int main(void)
{
float x;
float sum = 0.0;
int cnt = 0;
printf("Give sequence of floats, terminate with RETURN+CTRL-D :\n");
while (scanf("%f", &x) == 1)
{
sum += x;
cnt++;
}
printf("The average %f/%d = %f\n", sum, cnt, sum/cnt);
return 0;
}