PDA

View Full Version : Would anyone mind checking this?



stevengreen22
23-02-2011, 04:01 PM
Hi Guys,

Assignment 2...

idead being a different word is returned if integer is divisible by 3,5 or 15.
I've used 'whitespaces' in the write ln for spacing.

UNsure if this is the best method.

Would anyone mind giving it the once over and checking for any errors?

Thanks - the program does work as intended and displays ok, just a confirmation is what I'm after.

Thanks again.



program BUZZFIZZ(output);

uses crt;

const columncount = 5; {5 columns as 100 is div perfectly by 5}
columnwidth = 15; {either needs whitespaces or set in writeln}
Title: string = (' BUZZ FIZZ ');
By: string = (' By Steve Green ');

var i,j: integer;
ch: char;

begin {main program}
ClrScr;
writeln(Title);
writeln;
writeln(by);
writeln;

(* Hi, Welcome to etc etc, blaaaaaaaaaaaa, press return to*)
readln(ch);


for i := 1 to 9 do
begin
if ((i mod 5) =j) then
write(i,' = FIZZ ')
else
if ((i mod 3) =j) then
write(i,' = BUZZ ')
else
write(i,' = ')
end;

for i := 10 to 100 do
begin
if ((i mod 15)=int) then
write(i,' = BUZZ FIZZ ')
else
if ((i mod 5) =int) then
write(i,' = FIZZ ')
else
if ((i mod 3) =int) then
write(i,' = BUZZ ')
else
write(i,' = ')
end;


writeln;
writeln;
writeln ('Press Return to continue',ch);

read(ch);
end.

Murmandamus
26-02-2011, 05:54 PM
Title: string = (' BUZZ FIZZ ');
By: string = (' By Steve Green ');


You don't need the parentheses. Hence:


Title: string = ' BUZZ FIZZ ';
By: string = ' By Steve Green ';


Next..

You declare the variable "j", but never assign it a value; you are depending on the value of an uninitialized variable being 0, which is not a good idea. What reason are you using that variable? If it is just to compare to 0, then the code would be more meaningful if you just used 0.

Similarly, you use the variable "int", which is neither declared nor initialized anywhere (the code as you pasted it shouldn't even compile), for the same reason. Again, if you just are comparing the result of the mod operation to zero, just use 0. Hence:



if ((i mod 5) = 0) then
write(i,' = FIZZ ')
else
if ((i mod 3) = 0) then
...
if ((i mod 15) = 0) then
write(i,' = BUZZ FIZZ ')
else
if ((i mod 5) = 0) then
write(i,' = FIZZ ')
else
if ((i mod 3) = 0) then



Lastly,


writeln ('Press Return to continue',ch);

I pointed this out in the previous thread. There is no need to output the dummy input variable you are using to pause output. This will work fine:


writeln ('Press Return to continue');

stevengreen22
06-03-2011, 11:29 PM
Hi,

Cheers for taking the time to look at it. Seems it takes some time before the threads are posted here? Since that variant of it changes were made, I was over complicating it. In the end I think the code was quite simple. Or 'cleaner' anyway. (both int and j were remainder from my earlier prog as i was amending that one instead of writing from fresh)

the "press ret to cont" bit, I tried it as you said and it wouldn't compile, so i've reverted to adding the 'ch' again.
I don't understand why it doesn't work.

file:///C:/Users/steve/AppData/Local/Temp/moz-screenshot-5.png

User137
07-03-2011, 06:17 AM
If it doesn't compile, it usually gives information on why it doesn't. writeln ('Press Return to continue',ch); makes no logic sense at all. You are basically printing a undefined char variable after the word 'continue'.

Also, you can't link images from your local computer, they gotta be made either attachments to post or uploaded on a image hosting site.

stevengreen22
07-03-2011, 07:43 AM
OK, It annoys me that soimethign that obvious wasn't picked up on by the lecturer...You guys have so far given me far more help and feedback than I've received in class. For that, thanks!

Aha! Magic :) I had ch assisgned as a var char and I guess that was the issue, when I removed that and amended the final line to "readln()" it worked fine!
also good to know i don't need brackets around the title strings, makes it cleaner and thats what I want.

Would you recommend lazarus at all?

stevengreen22
07-03-2011, 07:54 AM
here's the code for scrutiny :)



for i := 1 to 100 do
begin
if (i mod 15=0) then
write('BUZZ FIZZ':10)
else
if (i mod 5=0) then
write('FIZZ':10)
else
if (i mod 3=0) then
write('BUZZ':10)
else
write(i:10)
end;
writeln;
writeln;
writeln ('Press <Return> to continue');

readln();
end.