Page 1 of 1

input problem

Posted: Sat Sep 28, 2002 3:50 pm
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 ???

Posted: Sat Sep 28, 2002 4:54 pm
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.

Posted: Sat Sep 28, 2002 5:19 pm
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)

Posted: Sun Sep 29, 2002 1:24 am
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?

Posted: Sun Sep 29, 2002 12:50 pm
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!

Posted: Sun Sep 29, 2002 1:42 pm
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]

Posted: Sun Sep 29, 2002 1:50 pm
by fpnc
... but you'll get a perfect "Restricted function" when doing that in our system, as file access is forbiden.

Posted: Sun Sep 29, 2002 2:25 pm
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

Posted: Mon Sep 30, 2002 4:39 pm
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?

Posted: Mon Sep 30, 2002 5:53 pm
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.

Posted: Fri Oct 25, 2002 11:45 am
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.

Posted: Mon Dec 08, 2003 12:27 pm
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.

Posted: Mon Dec 08, 2003 12:48 pm
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.