Saturday, October 24, 2009

6. Output Manipulation

We use computers to do complex things faster and would like to present results in a prettier looking understandable format. There are various techniques to do that and we will examine one by one.

1. Using a writeln statement without any parameters to skip a line. EX: writeln;

2. Using string variables in a writeln statement.
Ex: writeln('First Name Last Name'); **** Prints heading First Name Last Name as written in the string.

3. Using field parameters for variables in writeln statement.
Ex: writeln('First name' : 10, 'Last Name' : 12);
Write First Name in 10 spaces. Write Last Name in 12 spaces. Right justified always.

4. using field parameter for decimal places in the case of real numbers.
Ex: writeln('number3= ': 10, number3 :12:2);
Write the string number3= in 10 spaces and the number3 real variable in 12 places with 2 decimals.

In the case of wrong space field specification, computer will correct and display the whole value of the variable.

Program using these techniques follows:

Program program6;
(*Author: Rao S Lakkaraju*)
{ This is my sixth pascal program }
(* Output Manipulation in Pascal *)
uses crt,graph;
var n,number1, number2,number3 : integer;
interest1, interest2,interest3 : real;
name1, name2, name3 : string;
rich : boolean;

(* Program begins here *)
begin
clrscr;
number1 := 1;
number2 := 10;
interest1 := 2.5;
interest2 := 5.4;
name1 := 'Rao';
name2 := ' Lakkaraju';

(**** Lesson 4 *****)
for number3 := number1 to number2 do write(number3);

write(' ');

for number3 := number2 downto number1 do begin write(number3); end;

writeln(' ');
name3 := name1 + name2;
interest3 := interest1 + interest2;

textbackground(white);
textcolor(Red);

Writeln('number1 = ', number1);
writeln('number2 = ', number2);
writeln('number3 = ', number3);
writeln('interest1 = ', interest1, ' interest2 = ', interest2);
writeln('interest3 = ', interest3);
writeln('My Name is ', name3);

(**** Lesson 5 *****)
rich := false;
if not rich then writeln('I am not rich ')
else writeln('I am rich');

while rich = false do rich := true;

n := 1;
repeat n := n + 1; Writeln('I am in repeat'); until n = 8;

n := 2;
case n of 2,3,6 : writeln('value of n is ', n);
otherwise writeln('end of case'); end;

(**** Lesson 6 *****)
writeln;
writeln('First Name' : 10, 'Last Name':12);
writeln;
writeln(name1:10, name2:12);
writeln;
writeln('interest3=' :10, interest3:12:2);
writeln;
writeln('My sixth Pascal Program');
readln;

(* Program ends here *)
end.

Summary: Output from write statements can be formatted by specifying field lengths for each variable to be printed.

No comments:

Post a Comment