On Thu, 07 May 1998 23:12:03 +0200, FPL wrote:
program loop2(input, output); var i, j : integer;
begin j:=0; repeat for i:=0 to 10 do if (i div 2)=0 then begin write('*'); j:=j+1; end; writeln; until j=10; end.
Do you agree the output should be a rectangular array of 5 stars by ten, namely, something like
No. Note that for i from 0 to 10 "i div 2" will only equal 0 when i = 0 and when i = 1, otherwise, 2 will go into i at least once.
This is what I get for output: ** ** ** ** **
Which is correct, as you increment j everytime you print a star. Putting in a write(j); before you increment j may help you understand what is going on.
If you want 10 rows then you need to increment j outside of the 'if' block, which is probably most easily done by just removing the begin/end pair. [...] if (i div 2) then write('*'); j := j + 1 writeln; [...]
If you want 5 stars across, then make i go from 1 to 10 and change the test to (i mod 2).
hope this helps, -Kevin -- Kevin A. Foss --- kfoss@mint.net
No. Note that for i from 0 to 10 "i div 2" will only equal 0 when i = 0 and when i = 1, otherwise, 2 will go into i at least once.
That's true indeed! I thought to mod operator and wrote div :( and once replaced, it does work as expected. Now it's getting late and I'll stop bug off everybody. However, I invite you to look at how gdb is handling this piece of code. My other tests proved GPC to work okay with this code (provided the statement to be the good one!), but I don't think gdb does manage the stuff correctly. Peter was right, my code is likely to be bugged.
Thanks.
According to Kevin A. Foss:
No. Note that for i from 0 to 10 "i div 2" will only equal 0 when i = 0 and when i = 1, otherwise, 2 will go into i at least once.
This is what I get for output: ** ** ** ** **
Huppsa! #-) Thanks, Kevin, for correcting my wrong interpretation.
Peter
Just a thought.... Those file "Not open for write" errors are reminiscent of a problem I had while working on a school project back in '92 on Borland Pascal for Dos.
The problem I had was that I was still using the "program progName (input, output)" like the book said rather than "program progName" which works just as well. the (input, output) part is there, in BP at least, for backward compatibility. It is just saying that the standard input device (keyboard) is the input device the program will use and the standard output device (crt) is the output device the program will use. If you change input or output to the name of a file (or is it a file variable? I forget... :-) then that file is treated as standard input/standard output device.
At the time, I was getting groggy on coffee and at around 3 AM two days before I had to had in the project I typed out a filename instead of "input". Needless to say, I went ballistic until a friend with a reasonable set of nerves calmed me down, explained the situation and switched me to decaf....
Antoine
Peter Gerwinski wrote:
According to Kevin A. Foss:
No. Note that for i from 0 to 10 "i div 2" will only equal 0 when i = 0 and when i = 1, otherwise, 2 will go into i at least once.
This is what I get for output: ** ** ** ** **
Huppsa! #-) Thanks, Kevin, for correcting my wrong interpretation.
Peter
-- Peter Gerwinski, Essen, Germany, http://home.pages.de/~Peter.Gerwinski/ Maintainer GNU Pascal - http://home.pages.de/~GNU-Pascal/ - gpc-980420 PGP: see homepage - 6C 94 45 BE 28 A4 96 - 0E CC E9 12 47 25 82 75 Fight the SPAM and UBE! - http://spam.abuse.net/ - http://maps.vix.com/