{ Programmer : E. Adams
Course
: CS 430/CS 530
Date Due
: September 22, 2005
Language
: Pascal
Compiler
: Free Pascal
Source Filename : Homework4.pp
Executable Filename: Homework4.exe
Professor : E. Adams
}
{
Purpose: To write a brief Pascal progrm
that obtains 3 numbers from the user, finds
their sum and average and prints
out the 3 numbers, their sum and their
average with labels
Input:
3 integers in response to a prompt
Output: intro, prompt, echo of entered data,
sum of entered data, average of entered
data
General Comments: program does not error check for correctness
of entered data values
}
{ Pascal
has 2 forms of comments - a single line comment delimited by (* and
*) and a multi-line comment delimited by
curly braces
}
{ All
Pascal programs begin with the reserved word program followed by an identifier
and an optional parameter list specifying
input and output devices (if any) followed
by a semi-colon. This is called the header statement.
If there is to be input, then input should
be a parameter in the header statement
If there is to be output, then output should
be a parameter in the header statement
input and output refer to the default input
and output devices: keyboard and screen
whether they appear as (input, output) or (output, input) doesn't matter
}
program
homework4 (input, output);
{ All
variables used in a Pascal program must be declared. The declarations are
preceded by the reserved word var. In this program there are four integer
identifiers.
They are separated by commas and followed
by a colon and then their type and then
a semi-colon.
There is one real identifier.
Note that the reserved word var is not
repeated
}
var number1,number2, number3, sum : integer;
average : real;
(* the statements of the program follow the
reserved word begin *)
begin
{ the writeln statement prints the desired
output to the screen. After printing
the values, it moves the cursor to the next
line.
}
writeln (' this program will ask you to enter
three integers and will find ');
writeln (' their sum and average and echo the
integers, and print the sum ');
writeln (' and average with appropriate
labels ');
{ the
write statement leaves the cursor on the line (does not insert a CR/LF )
output literals are surrounded by single
quotes
}
write (' please enter an integer and hit
return : ');
{ the
readln statement picks up a value entered by the user and then moves the
cursor to the start of the next line
}
readln (number1);
write (' please enter an integer and hit
return : ');
readln (number2);
write (' please enter an integer and hit
return : ');
readln (number3);
{ Note that in Pascal, strings and identifiers can be in the same
writeln statement
(unlike in Java) and they are separated by
commas
}
writeln (' the numbers you entered are
', number1, ', ', number2, ', and ', number3);
{ write, writeln, read and readln are calls to
predefined procedures and have
variable length parameter lists. writeln ( ' something '); is equivalent to
writeln (output, ' something '); and
readln (' something '); is
equivalent to
readln (input, ' something ');
}
(* the
assignment operator in Pascal is colon-equal
:= *)
sum := number1 + number2 + number3;
(* the
slash (/) is the division operator that produces a real result *)
average := sum / 3.0;
{
the :5:3 after average in the ouput
statement formats the output so that it does
not appear in the default, exponential format
}
writeln (' the sum is ' , sum , ' and the
average is ' , average:5:3);
(* the
main program in Pascal ends with an
end. (end followed by a
period) *)
end.