Issues raised
with regard to the programming assignment
Sample output will not be posted before we start programming.
If there is a problem with reset and rewrite it might be a path problem, see if it works at school. Otherwise, make sure you’re using the file and not the file name.
Use as many subprograms as possible, it will make life easier.
Pascal Notes
To declare an array:
Declare a type variable
type anArray
= array [1..5] of real;
Then declare a variable of type var.
var myArray:
anArray;
Make sure you always have begin and end. in your program.
Your procedure must have a name associated with it.
procedure myProc( );
Procedures also must have a “begin” and an “end;”.
NOTE: The end at the end of a program is followed by a ., the end at the end of a procedure is followed by a ; .
To assign a value to a variable use := as the assignment operator.
myReal := 5.28;
When she said don’t put a semicolon after a do, she meant after the WORD do.
for counter := 1 to 5 do
myArray[counter] := counter
* myReal;
How to declare a procedure with parameters:
Procedure fillArray (var someArray : anArray;
computationReal
: real;
arraySize :
integer);
NOTE: There are no types
before computationReal and arraySize
because they are value parameters, which is the default parameter passing type
in Pascal.
Unlike in FORTRAN, variables must always be declared. So, in the procedure fillArray, the variable counter must be declared as a var before it can be used in the for loop.
The code:
For
i :=
1 to 5 do
Writeln (myArray[i]:5:2);
Will print out array of 5 elements, and format the output to 5 digits with 2 after the decimal point.
Interestingly enough, if you made the above loop go from 1
to 6… Pascal doesn’t care.
Actually, when I ran it again, it
did care and gave a runtime error. The
results are unpredictable. Be careful
NEVER to exceed declared array bounds.
The procedure, fillArray
procedure fillArray( var someArray : anArray;
computationReal
: real;
arraySize :
integer);
var counter :
integer;
begin
for
counter := 1 to arraySize do
someArray[counter]
:= counter * computationalReal;
To call fillArray from the main program use:
fillArray(myArray, myReal, 5);
The procedure fillArray will replace the earlier code:
for counter := 1 to 5 do
myArray[counter] := counter * myReal;
Homework
There will be an in class assignment on Thursday, go to class!
Look over the chapter 5 slides and if there’s anything you
don’t understand, send Dr