Re: Steve Summit C notes , exercise



arnuld wrote:
this is the programme i created, for exercise 2, assignment 3 at

http://www.eskimo.com/~scs/cclass/asgn.beg/PS2.html

it runs fine. i wanted to know if it needs any improvement:

----------------- PROGRAMME ----------------------------
/* Steve Summit's C programming

Section 3 :: exercise 2

STATEMENT:
Write a program to compute the average of the ten numbers 1, 4,
9, ..., 81, 100,
that is, the average of the squares of the numbers from 1 to 10. (This
will be a
simple modification of Exercise 3 from last week: instead of printing
each square
as it is computed, add it in to a variable sum which keeps track of
the sum of all
the squares, and then at the end, divide the sum variable by the
number of numbers summed.)

*/

#include <stdio.h>
#define DIVISOR 10

int main()
{
int i;
double sum;

sum = 0;

Just set sum to zero at initialisation.

for(i = 0; i <= 10; ++i)

i should probably start from 1. Also replace the hardcoded 10 with
DIVISOR. That was it's purpose in the first place.

sum += i*i;

printf("the average is: %.1f\n", sum / DIVISOR);

return 0;

}

.



Relevant Pages

  • Steve Summit C notes , exercise
    ... this is the programme i created, for exercise 2, assignment 3 at ... the average of the squares of the numbers from 1 to 10. ... the sum of all ...
    (comp.lang.c)
  • Re: Steve Summit C notes , exercise
    ... the average of the squares of the numbers from 1 to 10. ... simple modification of Exercise 3 from last week: ... the sum of all ... int main ...
    (comp.lang.c)
  • Re: Steve Summit C notes , exercise
    ... the average of the squares of the numbers from 1 to 10. ... simple modification of Exercise 3 from last week: ... the sum of all ... int main ...
    (comp.lang.c)
  • Re: True probabilistic rounding [was Re: Precision ...]
    ... way down to below the actual precision of the sum. ... "almost all programming can be viewed as an exercise in caching" ...
    (comp.lang.fortran)
  • Re: How to measure speed
    ... ""Exercise 1.3. ... arguments and returns the sum of the squares of the two larger ... (sum-of-squares b c) ...
    (comp.lang.scheme)