input problem

Write here if you have problems with your Pascal source code

Moderator: Board moderators

Post Reply
chessmaster
New poster
Posts: 5
Joined: Sat Sep 28, 2002 3:46 pm

input problem

Post by chessmaster »

Hello, I got trouble when I meet problems with input like this

1 2
6 7
8 9

my trouble is i must repeat read until when?

repeat
read(a);
read(b);
until ???
fpnc
System administrator
Posts: 201
Joined: Sun Oct 07, 2001 2:00 am
Location: Valladolid, Spain

Post by fpnc »

It depends. Sometimes you're given a number of test cases, and you can do

[pascal]
for k:=1 to n do begin
read(i,j);
{ ... do whatever you may need here ... }
end;
[/pascal]

When not, try something like

[pascal]
while not eof(input) do begin
read(i,j);
{ ... do whatever you may need here ... }
end;
[/pascal]

Hope this helps.
Best regards,

Fernando N
Picard
Learning poster
Posts: 96
Joined: Mon Jun 24, 2002 1:22 pm
Location: Hungary
Contact:

Post by Picard »

i think you should be careful about read and eof. example in this case after the last read there is still a newline char in the input, and eof will still return false, but the next read will raise an exception (that will cause runtime error if not caught)

btw i'am not using pascal, but it's not really "fair" that pascal runtime error is not signaled to sender, just wrong answer. (i know it's different as c/c++ because pascal catches all exceptions and raises no signals)
chessmaster
New poster
Posts: 5
Joined: Sat Sep 28, 2002 3:46 pm

Post by chessmaster »

[quote="fpnc"]It depends. Sometimes you're given a number of test cases, and you can do

[pascal]
for k:=1 to n do begin
read(i,j);
{ ... do whatever you may need here ... }
end;
[/pascal]

When not, try something like

[pascal]
while not eof(input) do begin
read(i,j);
{ ... do whatever you may need here ... }
end;
[/pascal]

thanx, but in the line

while not eof(input)

what is input? it is a text file?
fpnc
System administrator
Posts: 201
Joined: Sun Oct 07, 2001 2:00 am
Location: Valladolid, Spain

Post by fpnc »

Input and Output are two "special" files which you don't open or close.

For example, let's have a problem which says "You'll have a lot of lines, in which there are two numbers a and b. Your output should be a+b for each couple".

[pascal]
program pXXX(input, output);
var a,b,sum:integer;
begin
while not eof(input) do begin
readln(a,b); { could be readln(input, a, b) also }
sum:=a+b;
writeln(sum); { could be writeln(output, sum) also }
end;
end.[/pascal]

So just use the "program" header as in this example, and don't care about input/output files. Just use input as the file for the keyboard, and output as the file for the console, and do not try to open or close them.

Good luck!
Best regards,

Fernando N
arc16
Learning poster
Posts: 62
Joined: Sun Aug 04, 2002 1:05 am
Location: Indonesia

Post by arc16 »

fpnc wrote:Just use input as the file for the keyboard, and output as the file for the console, and do not try to open or close them.
btw, just want to add that we still can open/close that special input/output file and direct them to standard input/output by using '' as parameter.

for example:
[pascal]
assign(output,''); rewrite(output);
assign(input,''); reset(input);
[/pascal]
fpnc
System administrator
Posts: 201
Joined: Sun Oct 07, 2001 2:00 am
Location: Valladolid, Spain

Post by fpnc »

... but you'll get a perfect "Restricted function" when doing that in our system, as file access is forbiden.
Best regards,

Fernando N
arc16
Learning poster
Posts: 62
Joined: Sun Aug 04, 2002 1:05 am
Location: Indonesia

Post by arc16 »

fpnc wrote:... but you'll get a perfect "Restricted function" when doing that in our system, as file access is forbiden.
i used it on the local contest, i've never used it on the program i submit to OJ :D
chessmaster
New poster
Posts: 5
Joined: Sat Sep 28, 2002 3:46 pm

Post by chessmaster »

fpnc wrote:Input and Output are two "special" files which you don't open or close.

For example, let's have a problem which says "You'll have a lot of lines, in which there are two numbers a and b. Your output should be a+b for each couple".

[pascal]
program pXXX(input, output);
var a,b,sum:integer;
begin
while not eof(input) do begin
readln(a,b); { could be readln(input, a, b) also }
sum:=a+b;
writeln(sum); { could be writeln(output, sum) also }
end;
end.[/pascal]

So just use the "program" header as in this example, and don't care about input/output files. Just use input as the file for the keyboard, and output as the file for the console, and do not try to open or close them.

Good luck!
thanx.
but what about with multiple input? How to handle that?
arc16
Learning poster
Posts: 62
Joined: Sun Aug 04, 2002 1:05 am
Location: Indonesia

Post by arc16 »

there are two ways:

the first, which i used, is:

Code: Select all

readln(input,count);
while (count>0) do
begin
readln(input);   {read blank space}
.... {read input like usual problem}
count:=count-1;
end.
or you can also use this:

Code: Select all

readln(input,temp);
while not eof(input) do
begin
readln(input);   {read blank space}
.... {read input like usual problem}
end.
SMBfromRU
New poster
Posts: 7
Joined: Wed Sep 11, 2002 9:19 am

Post by SMBfromRU »

Hi to all!

I'd like to resume discussion on input problems.
All advices above work well in case of known amount of numbers in the line. But if you need to read indefinite (may be very big) number of integers? Same problem with very long Strings.
I've got many WA, TLE, MLE with several "primitive" ACM probs (like 492-Pig Latin, 484 and other) and consider their source is incorrect reading of input.
Has anybody used long strings in Free Pascal, and what kind of them (PChar or AnsiStrings)? Is there some tricks with using of them?

I will appreciate any hints and help.
ggggqqqqihc
New poster
Posts: 12
Joined: Sun Dec 07, 2003 10:45 am

Post by ggggqqqqihc »

But when I use Program(input,output), how to end input?
Any file ends up with ^Z, but when I type Ctrl+Z, the problem will crash.
junjieliang
Experienced poster
Posts: 169
Joined: Wed Oct 31, 2001 2:00 am
Location: Singapore

Post by junjieliang »

To SMBfromRU:
For Pig latin what I do is to read and process each word individually. In other words, read a stretch of letters, read a stretch of non-letters, read a stretch of letters, etc etc. You get the point. This also works for many other problems.

For 484 I suppose your problem is not with reading, but the memory you need to declare. Since no limits are given it is rather difficult to guage. I suggest using pointers in this case, so you only need as much memory as the test case needs. You could try using linked list or something similar to the realloc() function if C if it exists.

To ggggqqqqihc:
Ctrl+Z works well for me. Perhaps you could send me your code through pm so I can tell you what's wrong? The problem may be like what's discussed above, where you use read with eof.
Post Reply

Return to “Pascal”