* arnuld wrote in comp.programming:
>WHAT I GOT: it works
>
>Question-1: Look at the for loop, instead of looping for 1000 times and
>checking the value inside, I am simply looping it 1000/3 = 333 times. Ist
>that impressive ?
Not really.
>Question-2: Can it be improved ?
You do not say which kinds of improvements you are looking for, like if
you are using `const` the right way, whether you should have put some of
the code into a function, whether the debugging output should go to std-
err instead of stdout, whether you use unusual formatting, and so on. So
it's a bit difficult to give suitable feedback.
>/* Add all the natural numbers below one thousand that are multiples of 3
>or 5. */
>
>#include <stdio.h>
>#include <stdlib.h>
>
>int main(void)
>{
> const int limit = 7;
> const int multiple_A = 3;
> const int multiple_B = 5;
> unsigned long sum;
> int i, j;
> sum = 0;
>
> for(i = 0, j = 0; (i <= limit); i = i + multiple_A, j = j + multiple_B)
This is a bad formulation of the loop, a code reviewer would have to
think carefully about whether a check for `j` would also be needed,
especially if someone might change the constants later on.
The code does not actually seem to add all numbers 0 .. 1000 that're
integer multiplies of 3 or 5. Only goes to ten, and the sum is wrong
as you seem to forget adding 9. That might be related to the problem
with the loop condition.
--
Björn Höhrmann · mailto:
bjo...@hoehrmann.de ·
http://bjoern.hoehrmann.de
Am Badedeich 7 · Telefon:
+49(0)160/4415681 ·
http://www.bjoernsworld.de
25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 ·
http://www.websitedev.de/