INTRODUCTION
The Pascal programs from the previous labs was written by someone else. In this lab, you will be able to read a source file and modify it. Simple input and output commands will be introduced.
NEW CONCEPTS
• Modifying source code
• Variables
• Input and output
OBJECTIVES
In this exercise you should acquire the following skills:
• Reading source codes
• Modifying or/and creating source codes
PREPARATION
• Read this through this lab and the previous Pascal
lam carefully.
• Read the description of the Think Pascal built-in drawing
procedures available on reserve in the Science Library. These are summarized
at the end of your lab manual (Appendix).
• Read pages 256-273 in the text.
• Read the Appendix again—a few minutes
now will save you a lot of time later.
• Bring your second disk to class, containing the "programming
labs" folder.
WHAT TO TURN IN
• Preparation sheet
• Assignment sheet
INSTRUCTIONS
program add2;
{Adds two numbers N1 and N2.}
var
{Notice that there are three variables of type integer}
{You must declare your variables before you can use them in
the program}
N1, N2, Sum: integer;
begin
{Simple print statement}
writeln('Enter two numbers :');
{readln gets input from user}
readln(N1, N2);
{performs addition}
Sum := N1 + N2;
{outputs the Sum}
writeln(N1, ' plus ', N2, ' Equals ', Sum);
writeln('Enter another two numbers :');
readln(N1, N2);
Sum := N1 + N2;
writeln(N1, ' plus ', N2, ' Equals ', Sum);
end.
Figure 1: Program add2
The writeln('Screen output ') command prints "Screen output" to the screen. The readln( ) commands get user input from the keyboard. So readln (N1, N2) require will get two variables from the user, N1 and N2. Both N1 and N2 are integers, since they are declared before readln() was used. The best way to understand how these command works is to run and modify the program. Observe what happens when you change the program.
Save this program as "add2.src". Try running the program. If it runs, modify to add three variables of type integer. (Hints: Remember to declare the third variable before the readln function. The readln function should have three variables instead of two.
Here is program that prints out the fullname after the user inputs the first and last names.
program fullname;
var
First_Name:string[10];
Last_Name: string[20];
begin
{Simple print statement}
writeln('Enter your first name :');
{readln gets input from user}
readln(First_Name);
writeln('Enter your last name :');
{readln gets input from user}
readln(Last_Name);
writeln('Hello ', First_Name, ' ', Last_Name);
end.
Figure 2: program fullname
Notice how the variables are declared in var. There are two variables. All of them are strings. The First_Name variable has length 10, while the Last_Name has length 20. Save this file and run it. Modify it so that the program will ask the user for his/her age. The program should print the name and age on the screen. (Hint: Look at both programs and create a new one.)
Steps to create and run your programs:
1. Locate and launch Think Pascal
2. Create a new project file (For example: cs2prog.prj)
You will see a project window.
3. Create a new file by selecting New from
the File menu.
4. Type the source code into the new window.
5. Save the source code by selecting Save As...
from the File menu. Give the file a name (Example: cs002prog1.src)
6. Include this file in a project. You have
already created this from step two. Select Add <source
file> from the Project menu.
7. Select Go from the Run menu
8. The program should run. A text window
will be opened to allow the user to enter the variables.
The program below will print three lines of "Hello" and one line on "End of Program". The program declares cond_variable to be an integer and initializes it to be 3. The while statement checks cond_variable to see if it is positive. If it is positive, then execute the statements within the while loop. Inside this loop, the program will print "Hello" and decrease cond_variable by one. There will be a time when cond_variable will not be positive. At this point, the program will not go into the while loop. It will print the line "End of Program".
program ShowWhile;
var
cond_variable: integer;
begin {program}
cond_variable := 3;
while (cond_variable > 0) do begin
writeln('Hello');
cond_variable := cond_variable - 1;
end;
writeln('End of Program');
end.
Figure 3: program ShowWhile