Re: Steve Summit C notes , exercise



arnuld said:

<snip>

#include <stdio.h>

#define DIVISOR 10

int main()
{
int i;
double sum;

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

The loop iterates i through these values: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
and 10. Now, 1 to 10 clearly gives us ten values, and 0 gives us an
eleventh, so DIVISOR should surely be 11, not 10?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.



Relevant Pages

  • Re: Steve Summit C notes , exercise
    ... int main ... double sum; ... eleventh, so DIVISOR should surely be 11, not 10? ... the value 10 is written twice in the program, both representing the same upper limit. ...
    (comp.lang.c)
  • Re: Steve Summit C notes , exercise
    ... int main ... double sum; ... eleventh, so DIVISOR should surely be 11, not 10? ...
    (comp.lang.c)
  • Re: Steve Summit C notes , exercise
    ... int main ... if you take my advice above about using an int above, just be careful to promote sum to a float here, e.g. sum / DIVISOR ... If I choose float, it is for a specific reason. ...
    (comp.lang.c)
  • Re: Algorithm
    ... Wont sum of all positive numbers will be the largest sub-array? ... int getint ... struct sofar *next; ... struct sofar *discard(struct sofar *trail) ...
    (comp.lang.c)
  • Integer division, speed test
    ... Once the divisor is largish, the win from the constant divisor ... void divmodgen(unsigned int x, unsigned int y) ... case 1: LOOP divkkk; break; ...
    (comp.programming)