Developing Software Solutions

 

 

 

 

 

 

 

 

 

 

Formatting output

Interactive

Strings

 

Example 2.1

            All programs "declare variables"; in this the variables are REAL (not integers or Boolean ..true or false). The last END must have a fullstop. Note the equals sign := and the multiplication *.  Write(Apples) and Writeln(Apples) give the answer but writeln goes to the next line after it has written the answer.

            Type in the following program

 

PROGRAM Multiplication;

 

VAR

            Apples, Pears, Bananas:real;

BEGIN

            Pears:=0.002;

            Bananas:=4700;

            Apples:=Pears*Bananas;

            Writeln(Apples)

END.

 

 

The output produced by the program is

 

9.4000000000E+00

 

 

Example 2.2

            Words can be printed as well as number in the answer using apostrophes ' in writeln statements.

 

PROGRAM Multiplication;

 

VAR

            Apples, Pears, Bananas:real;

BEGIN

            Writeln('This program will multiply two numbers together');

            Pears:=0.002;

            Bananas:=4700;

            Writeln('The first number is ',Pears);

            Writeln('The second number is ',Bananas);

            Apples:=Pears*Bananas;

            Writeln;

            Writeln('The product is ',Apples)

END.

 

    

 


Example 2.3

            This example shows how to "SPACE OUT" your answers across the screen.  Modify example 2 and include these extra lines

 

            Clrscr;

            Writeln;

            Writeln;

            Writeln;

            Writeln(' FIRST No':20,'SECOND No':20,'PRODUCT':20);

            Writeln('--------':20,'---------':20,'-------':20);

 

 

 

 

Example 2.4

            We start to "FORMAT" the output so that it is readable

            eg Apples:20 means that it takes up 20 spaces

 

PROGRAM Multiplication;

 

VAR

            Apples, Pears, Bananas:real;

BEGIN

            Clrscr;

            Writeln;

            Writeln;

            Writeln;

            Writeln(' FIRST No':20,'SECOND No':20,'PRODUCT':20);

            Writeln('--------':20,'---------':20,'-------':20);

            Pears:=0.0015;

            Bananas:=1000;

            Apples:=Pears*Bananas;

            Writeln(Pears:20,Bananas:20,Apples:20)

END.

 

OUTPUT is .....

 

    

 

Example 2.5

            A further "FORMAT" is

                        Pears:20:4 where 4 gives the number of decimal places to show

 

Modify the program with this :4.

 

OUTPUT is......

 


Example 2.6

 

            This is an example of an "interactive" program. The program stops; asks you a question and waits for an answer.

 

 

PROGRAM Multiplication;

 

VAR

            Apples, Pears, Bananas:real;

BEGIN

            Clrscr;

            Writeln('This program calculates the product of the two numbers entered');

            Writeln('Enter the first number');

            Readln(Pears);

            Writeln('Enter the second number');

            Readln(Bananas);

            Writeln;

            Writeln(' FIRST No':20,'SECOND No':20,'PRODUCT':20);

            Writeln('--------':20,'---------':20,'-------':20);

            Apples:=Pears*Bananas;

            Writeln(Pears:20,Bananas:20,Apples:20)

END.

 

 

    

 

Exercise 2.7

 

Write a program for Task 1.3. It is to be handwritten on A4 paper.

 

This is due 21st February

 

 


 

Make your programs user friendly

Example 2.8

 

PROGRAM TestAverage;

 

VAR

            StudentName : STRING[30];

            CourseName : STRING[30];

            Score1, Score2, Score3, Score4, Average: real;

 

BEGIN

            Clrscr;

            Writeln('This program will calculate the average of four')

            Writeln('test scores that you emter.');

            Writeln;

            Writeln;

            Writeln;

            Write('Enter the student name: Name= ');

            Readln(StudentName);

            Writeln;

            Write('Enter the course name: Course= ');

            Writeln;

            Write('Enter the first test score: Test #1 = ');

            Readln(Score1);

            Writeln;

            Write('Enter the second test score: Test #2 = ');

            Readln(Score2);

            Writeln;

            Write('Enter the third test score: Test #3 = ');

            Readln(Score3);

            Writeln;

            Write ('Enter the fourth test score: Test #4 = ');

            Readln(Score4);

            Average:=(Score1+Score2+Score3+Score4)/4;

            Writeln;

            Writeln;

            Writeln;

            Writeln ('Student Name:  ',StudentName);

            Writeln;

            Writeln('Course:  ',CourseName);

            Writeln;

            Writeln('Test #1':10,'Test #2':10;'Test #3':10,'Test #4':10,'Average':10);

            Writeln('-------':10,'-------':10,'-------':10,'-------':10,'-------':10,);

            Writeln;

            Writeln(Score1:10:2,Score2:10:2,Score3:10:2,Score4:10:2,Average:10:2);

END.                                                                                                                                                  

    

Hosted by www.Geocities.ws

1