Simplifying Pascal Code

Soldato
Joined
5 May 2004
Posts
4,462
Location
Northern Ireland
Hello there.

Is there any way to simplify this code by reducing the readln sections? I have managed to write the programme and execute it but its untidy.

infile
Code:
abc
def
ghi
jkl
mno
prs
tuv
wxy

Programme
Code:
Var
Numbers : Array [rows] of Array [cols] of char;
infile: text;
key: text;
row: rows;
col: cols;
ch: char;
i: integer;

Begin
 Assign(infile,'letters.txt');
  Reset(infile);    
  Assign(key,'press.txt');
	Reset(key);

Read(infile,ch);
Numbers[2,1]:=ch;
Read(infile,ch);
Numbers[2,2]:= ch;
Readln(infile,ch);
Numbers[2,3]:= ch;

Read(infile,ch);
Numbers[3,1]:= ch;
Read(infile,ch);
Numbers[3,2]:= ch;
Readln(infile,ch);
Numbers[3,3]:=ch;

Read(infile,ch);
Numbers[4,1]:=ch;
Read(infile,ch);
Numbers[4,2]:=ch;
Readln(infile,ch);
Numbers[4,3]:=ch;

Read(infile,ch);
Numbers[5,1]:=ch;
Read(infile,ch);
Numbers[5,2]:=ch;
Readln(infile,ch);
Numbers[5,3]:=ch;

Read(infile,ch);
Numbers[6,1]:=ch;
Read(infile,ch);
Numbers[6,2]:=ch;
Readln(infile,ch);
Numbers[6,3]:=ch;

Read(infile,ch);
Numbers[7,1]:=ch;
Read(infile,ch);
Numbers[7,2]:=ch;
Readln(infile,ch);
Numbers[7,3]:=ch;

Read(infile,ch);
Numbers[8,1]:=ch;
Read(infile,ch);
Numbers[8,2]:=ch;
Readln(infile,ch);
Numbers[8,3]:=ch;

Read(infile,ch);
Numbers[9,1]:=ch;
Read(infile,ch);
Numbers[9,2]:=ch;
Readln(infile,ch);
Numbers[9,3]:=ch;

Writeln('************');
Write(Numbers[4,2]);
Write(Numbers[3,2]);
Write(Numbers[5,3]);
Write(Numbers[5,3]);
Write(Numbers[6,3]);
Write(' ');
Write(Numbers[8,1]);
Write(Numbers[4,2]);
Write(Numbers[4,3]);
Write(Numbers[7,3]);
Write(' ');
Write(Numbers[4,3]);
Write(Numbers[7,3]);
Write(' ');
Write(Numbers[7,1]);
Write(Numbers[3,2]);
Write(Numbers[8,1]);
Write(Numbers[3,2]);
Write(Numbers[7,2]);
Writeln;

Writeln('************');


Repeat
Read(key,row,col);
Write(Numbers[row,col]);
Until EOF (key);

Writeln;
close(infile);
close(key);
End.

Thanks

Blackvault
 
Not having done pascal since school you can simplify the first bit as

Code:
Begin
    Assign(infile,'letters.txt');
    Reset(infile);    
    Assign(key,'press.txt');
    Reset(key);

    for i := 2 to 9 do
        begin
            for j := 1 to 2 do
                begin
                    Read(infile,ch);
                    Numbers[i,j]:=ch;
                end;
            Readln(infile,ch);
            Numbers[i,3]:=ch;
        end;

Couple of points, why do you start using Numbers from 2 in the first index?
You might be able to simplify the write bit as well, I cant work out (or be bothered ;)) what it is doing.

The array definitions looks a little odd compared to the pascal I remember, I used to do

Code:
IntArray = Array [1..100] of Int;

It seems odd that you are using a variable for the size of the array that never seems to have a value assigned to it.

Syntax is only approximate, I haven't done pascal for so long :)

Ric.
 
Last edited:
hi Ric

Thanks for getting back. Managed to get it sorted - asked my tutor in uni :) As regards why I start from 2 is that the programme is used to use key presses to output the corresponding letter - bit like a mobile phone keypad.

Blackvault
 
Back
Top Bottom