Saturday, March 14, 2015

Coding Array of For Loop in Pascal

As you know, arrays are simple indexed elements, with the elements being the variable values.

Reminder:  To code the For loop in Pascal, we note:

For x = 1 to 10 do

is replaced with

For i := 1 to 10 do
begin

next

is replaced with

end;

Now we code the array.  As you may recall, the array in an algorithm is declared on the first line of the algorithm.  In Pascal, however, that is not the case.  The array(s) is declared under the variable declaration section.  Example below:

Algorithm - output the 3rd and 5th names entered of 10 names.

dim nam (1 to 10) as string
for x = 1 to 10 do
print "enter your name"
input nam$(x)
next
print "the 3rd and 5th names entered are:", nam$(3), nam$(5)
input key


Pascal -output the 3rd and 5th names entered of 10 names.

Program Names (input,output);
Var
nam : array [1..10] of string;
i : integer;
Begin
for i := 1 to 10 do
begin
writeln ('enter your name');
readln (nam[i]);
end;
writeln ('the 3rd and 5th names entered are:', nam[3], name[5]);
readln;
end.

You try one:  
Determine and output the largest of 5 numbers entered, and output the 3rd and 4th numbers entered.  Code in Pascal.

No comments:

Post a Comment