Pascal programming problems - pascal

I wrote the following lines in pascal:
procedure editor;
begin
clrscr;
frame;
levframe;
assign (level,'level.dat');
rewrite(level);
for i:=1 to 600 do write(level,'0');
for i:=1 to 30 do for j:=1 to 20 do levmem(i,j):='0';
(The variables are declarated)
The translator says:
Lp1.pas(53,43) Error: Illegal expression
Lp1.pas(53,43) Fatal: Syntax error, ";" expected but "(" found
Question: why doesn't it work? (53,43) is in the last line, at 'levmem(i,j)'. Thanks for answers.

Assuming levmem is declared as a 2D array, the syntax should be:
for i:=1 to 30 do for j:=1 to 20 do levmem[i][j]:='0';
^^^^^^

Related

There is an invalid floating point operation, but where?

I'm currently writing a code the takes a number given a prints all the prime numbers that fit the format 4n+1. This is what I have so far. They problem is that this gives me a runtime error 207 which I think means invalid floating point operation, but I can't see how it ended up doing an invalid floating point operation. The only the code should be dealing with negative numbers in the line "if num-(iter*iter)> then".
program TwoSquares;
var
num, numSqrt, iter, bigSqr,smallSqr: integer;
begin
num:=29;
while num>4 do
begin
numSqrt:=trunc(sqrt(num));
for iter:=2 to numSqrt do
begin
if num mod iter = 0 then
num:=num - 1;
continue;
end;
if (num-1) mod 4 = 0 then
begin
iter:=(num-1) div 4;
while iter>0 do
begin
if num-(iter*iter)>0 then
bigSqr:=iter;
break;
iter:=iter-1;
end;
smallSqr:=trunc(sqrt(num-(iter*iter)));
writeln(num,' ', smallSqr,' ',bigSqr);
num:=num - 1;
end;
end;
end.
Your check is not directly before the place where it is used. Think; is the break; statement after that num-(iter*iter) if-then check the only way that while loop can terminate?
Try to single step your program, this can also verify if the block structure works as you think. It doesn't seem very consistent, with begin..end in some places, and indentation in others.

Runtime error 104 Pascal

Im very new to using files and im really struggling to fix this any help would be great.
It seems that the error is coming from my read array function but not entirely
sure i am also not to sure what the 104 error really means
thanks in advance
program ReadFromFile;
type
lineArray = array [0..19] of String;
procedure PrintArray(lines: lineArray);
var
i: Integer;
begin
for i:=0 to High(lines) do
begin
WriteLn('Text is: ', lines[i], ' Line number is: ', i);
end;
end;
function ReadArray(var myFile: TextFile):lineArray;
var
count : Integer;
lines : lineArray;
i: Integer;
begin
ReadLn(myFile, count);
for i := 0 to count do
begin
ReadLn(myFile, lines[i]);
end;
result := lines;
end;
procedure Main();
var
myFile: TextFile;
line: lineArray;
begin
AssignFile(myFile, 'mytestfile.dat');
ReWrite(myFile);
line:=ReadArray(myFile);
Close(myFile);
AssignFile(myFile, 'mytestfile.dat');
Reset(myFile);
PrintArray(line);
Close(myFile);
end;
begin
Main();
end.
You don't know what that error means. Neither do I off the top of my head. So, let's look it up in the documentation and find out. Websearch takes us here: https://www.freepascal.org/docs-html/user/userap4.html
File not open for input
Reported by Read, BlockRead, Eof, Eoln, SeekEof or SeekEoln if the file is not opened with Reset.
You have your calls to open the file the wrong way round. Call Reset to open for reading, Rewrite to open for writing.
Notes:
Looping from 0 to count will perform count + 1 iterations. I'd expect to see you looping from 0 to count - 1.
You don't check whether your array is long enough. You therefore run the risk of a buffer overrun. A dynamic array would avoid this.
It's not clear why you open the file for a second time when you print the contents to the console.
You could have looked up the error code yourself. Please take the hint to do web search the next time you encounter an error like this.

i don't know why the program didn't run

Please tell me where am i wrong, i couldn't fine my mistake in 2 programs. I try to use recursive in pascal.
This one is running but it gives me wrong resuts
program fatorial;
var
n: integer;
function f(n: longint): longint;
begin
if((n=0) or (n=1)) then
f:=1
else
*f:= n*f(n-1);*
read(f);
end;
begin
write('n:='); read(n);
f(n);
write('result:', f(n));
readln;
end.
This one told me "Error: illegal expression" but i don't know how to fix it
program Greatest_common_divisor;
var
gcd,p,q: integer;
r:=real;
begin
write('p:'); read(p);
write('q:'); read(q);
r:= p mod q;
if r <> o then
begin
p:=q;
q:=r
*gcv:= gcv(q,r);*
end;
write('Greatest common divisor:', gcv(p.q));
readln;
end.
You should not read f in the function.
You should write a function rather than use the internal function gcv()
First question:
I think reading f in the function isn't correct.
But the second question:
Don't use := in the command: r:=real; , only :
o and gcv are what kind of variables? You didn't identify o and gcv after var .
Put ; after q:=r

Pascal Why won't this simple random range work?

Trying to get a number in the range of a (variable -2) and (variable +2).
For example, if X = 7, then i would like a random number generator that gives a value in the range 5 - 9. See below my attempt which gives me unexpected results:
var x, xRange, i,count,xLower, xhigher:integer;
begin
x:=7;
xLower:=x-2;
xHigher:=x+2;
for count:= 1 to 20 do
begin
i:=random(xHigher)+xLower;
writeln(i);
end;
readln;
end.
As was mentioned by the user 500 - Internal Server Error and Marat Talipov
you want:
i:=random(xHigher-xLower)+xLower;
You should also use the Randomize; call before you call Random so that you get a new random value each time. So in the end:
var x, xRange, i,count,xLower, xhigher:integer;
begin
Randomize;
x:=7;
xLower:=x-2;
xHigher:=x+2;
for count:= 1 to 20 do
begin
i:=random(xHigher-xLower)+xLower;
writeln(i);
end;
readln;
end.

Reading integer numbers in Pascal

I'm using Pascal. I have a problem when dealing with reading file.
I have a file with integer numbers. My pascal to read the file is:
read(input, arr[i]);
if my file content is 1 2 3 then it's good but if it is 1 2 3 or 1 2 3(enter here) (there is a space or empty line at the end) then my arr will be 1 2 3 0.
From what I can recall read literally reads the file as a stream of characters, of which a blank space and carriage return are, but I believe these should be ignored as you are reading into an integer array. Does your file actually contain a space character between each number?
Another approach would be to use readLn and have the required integers stored as new lines in the file, e.g.
1
2
3
I have tested the problem on Delphi 2009 console applications. Code like this
var
F: Text;
A: array[0..99] of Integer;
I, J: Integer;
begin
Assign(F, 'test.txt');
Reset(F);
I:= -1;
while not EOF(F) do begin
Inc(I);
Read(F, A[I]);
end;
for J:= 0 to I do write(A[J], ' ');
Close(F);
writeln;
readln;
end.
works exactly as you have written. It can be improved using SeekEOLN function that skips all whitespace characters; the next code does not produce wrong additional zero:
var
F: Text;
A: array[0..99] of Integer;
I, J: Integer;
begin
Assign(F, 'test.txt');
Reset(F);
I:= -1;
while not EOF(F) do begin
if not SeekEOLN(F) then begin
Inc(I);
Read(F, A[I]);
end
else Readln(F);
end;
for J:= 0 to I do write(A[J], ' ');
Close(F);
writeln;
readln;
end.
Since all that staff is just a legacy in Delphi, I think it must work in Turbo Pascal.
You could read the string into a temporary and then trim it prior to converting it.
It doesnt hurt to mention basics like what type of Pascal on what platform you're using in order that people can give a specific answer (as the article notes, there isnt a nice way OOTB in many Pascals)
If I recall there was a string function called Val that converts a string to a number...my knowledge of Pascal is a bit rusty (Turbo Pascal v6)
var
num : integer;
str : string;
begin
str := '1234';
Val(str, num); (* This is the line I am not sure of *)
end;
Hope this helps,
Best regards,
Tom.

Resources