Hello everyone,
A string is called Palindrome if it stays the same when being read backward, for example, "axbxa" is a palindrome string.
I am attemping to write a program which generates random Palindrome. The program is complied in Free Pascal 2.4.0. Here is the source code: Palindrome.pas
And here is something I want to say about my code so you may find it easier when reading the code:
+) The procedure ntl at the beginning of the material is used to convert the number t1,t2 into letter that will be assigned to the variable t. t1 is the position of the letter in the alphabet; t2 is used to denote if the letter is capital or not (t2=0: normal; t2=1 : capital)
+)a is the variable used to store palindrome string. Each particle of the a, like a[1],a[2]..., is a string
+)Nog is the variable read from keyboard, used to denote how many palindrome string the user wants to generate.
+)The result is printed into the text file (f) with the file path (ip) inputed from keyboard.
NOW, HERE IS THE PROBLEM. The program runs smoothly if I step over every step in it (using the F8 button, you know).It prints out (nog) palindrome string in the (ip) file. However, when I just double-click on the program's excutable file complied by FPC, it justs generates palindrome string like "aaaaaa" ???
May you help me to find out why this happens?
If the program runs OK when step over with F8 buttons, then why it cannot run properly when being started directly?
I keep trying reinstalling FPC 2.4.0 but the problem persists.
If possible, may you try compling the code on your own computer; and if no problem found, please send me the result exe file. I think the problem may be at the FPC Complier engine.
Thanks for your help.
Problem with Free Pascal 2.4.0 Compiler
Started by daudang_47, May 17 2010 07:59 PM
2 replies to this topic
#1
Posted 17 May 2010 - 07:59 PM
|
|
|
#2
Posted 18 May 2010 - 07:19 AM
I've reformatted your code to make it (much) easier to read. That said, your choice of variable names is making it less than obvious what your logic is. I would strongly suggest you work on the variable names and comments, first.
{$mode objfpc}
program palindrome;
uses crt;
procedure ntl {number to letter} (t1,t2:LongInt; var t:Char);
var
i:LongInt; j:Char;
begin
i:=0;
If t2=0 then
begin
for j:='a' to 'z' do
begin
i:=i+1;
if i=t1 then
begin
t:=j;
break;
end;
end;
end;
i:=0;
If t2=1 then
begin
for j:='A' to 'Z' do
begin
i:=i+1;
if i=t1 then
begin
t:=j;
break;
end;
end;
end;
end;
var
a:array[1..100000] of array[1..30] of Char;
ip:String;
f:text;
nog,w,s,k,kold,nold,zh:LongInt;
n:array[1..100000] of LongInt;
begin
clrscr;
writeln('Welcome to Letter Fun/Palindrome Generator');
writeln('Please input the number of Palindrome you would like to generate');
readln(nog);
{GENERATE}
nold:=0;
for w:=1 to nog do
begin {Create a word}
repeat
randomize;
n[w]:=random(31);
until (n[w]>4) and (not (n[w]=nold));
nold:=n[w];
kold:=0;
If n[w] mod 2=0 then zh:=n[w] div 2 else zh:=(n[w]+1) div 2;
For s:=1 to zh do
begin
repeat
randomize;
k:=random(27);
until not ((k=kold) or (k=0));
ntl(k,0,a[w,s]);
k:=kold;
end;
If n[w] mod 2=0 then zh:=n[w] div 2 +1 else zh:=(n[w]+3) div 2;
for s:=n[w] downto round(n[w]/2) + 1 do a[w,s]:=a[w,n[w]+1-s];
clrscr;
writeln('Processing ',round(100*w/nog),'%');
end;
{OUTPUT}
writeln('Please input the desired output file path');
writeln;
readln(ip);
assign(f,ip);
rewrite(f);
for w:=1 to nog do
begin
for s:=1 to n[w] do write(f,a[w,s]); writeln(f);
end;
close(f);
writeln('Generation completed');
readln
end.
#3
Posted 19 May 2010 - 03:54 AM
Sorry for bugging you guys, but I have found the errors in my code. I misused the randomize function and instead of doing kold:=k; I wrote k:=kold; (That's my stupid mistake !)
The code of Palindrome is a part of my software. If you want to download my final products, you can go to here:
LetterFun_Start.exe
Thanks for your help, anyway.
The code of Palindrome is a part of my software. If you want to download my final products, you can go to here:
LetterFun_Start.exe
Thanks for your help, anyway.


Sign In
Create Account

Back to top









