If we want to store 5 names in the computer, we have to name them (name1,name2 etc.) and assign values to them individually. To retrieve also we have to retrieve them individually. Imagine how tedious it is going to be if we are writing a program to do pay roll for a walmart.
To make life easy, Pascal gave us, array. Pascal reserves space as indicated in the array definition and allowed us to access the space through an index. There are two ways of defining an array:
1. Here I defined a type called numbers with 15 spaces reserved
type numbers = array[1..15] of integer;
Here I equated a variable to numbers array
var numb : numbers;
2. var name :array[1..20] of string; (* Here I defined array named name with 20 spaces*)
Following program describes the manipulation of arrays in Pascal:
Program program9;
(*Author: Rao S Lakkaraju*)
(* My pascal program9*)
{ Example of arrays }
uses crt,graph;
type numbers = array[1..15] of integer;
var numb : numbers;
name :array[1..20] of string;
index1 : integer;
begin
clrscr;
textbackground(white);
textcolor(red);
writeln(' My pascal program9');
writeln(' Array Values');
for index1 := 1 to 10 do
begin
numb[index1] := index1 + 1;
write(numb[index1]);
end;
writeln;
writeln('please enter name');
readln(name[1]);
for index1 := 2 to 10 do
begin
name[index1] := name[1]+ name[index1 - 1] ;
write(name[index1]);
writeln('yes');
end;
readln;
end.
Summary: Best way to store multiple values of a same type of variable in Pascal is the array[]. Pascal reserves space depending on the size and type of the array. We can use an index to store and retrieve values for processing.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment