PASCAL PROGRAMMING 


ARRAY IN PASCAL:

To learn array, we need to first learn Subrange datatype.

Subrange type

A Subrange type defines a subset of the values of a particular type. Subrange helps to create a given boundary and it can be of integer, char. You can only use values within the specified range. If you assign value outside the give range, it will generate error.

Syntax:

type

variableName = startValue..endValue

var

newVariableName : variableName    //you create a new variable from the subrange type

 

TIP: Start with the type keyword. to specify the startValue and endValue, you add two dots in between them.

Program exercises implementing subrange type in Pascal programming

Lab1

//define range of test scores and initialize

program lab1;

type

score = 0..40;      //number ranging from 0 to 40

var

s1 : score;

s2 : score;

 

begin

  //assign values within the specified range

  s1 := 27;

  s2 := 39;

 

  //display

  writeln ('value within range:: ', s1);

  writeln ('value within range:: ', s2);

 

end.

 

Lab2

//define range of exam grade and initialize

program lab1;

type

grade = 'a'..'f';      //number ranging from 0 to 40

var

g1 : grade;

g2 : grade;

 

begin

  //assign values within the specified range

  g1 := 'b';

  g2 := 'd';

 

  //display

  writeln ('value within range:: ', g1);

  writeln ('value within range:: ', g2);

 

end.

 

APPLICATION OF SUBRANGE TYPE ON ARRAY IN PASCAL

What is array?

An array is a collection of similar types of data. For example, if we want to store the names of 5 students, then we can create an array of the string type that can store 5 names. Instead of declaring individual variables, such as fn, sn, tn, ftn, and ltn. You declare one array variable such as fullname as fullname[1], fullname[2], fullname[3], fullname[4], and fullname[5] to represent individual variables. A specific element in an array is accessed by an index (index uses the square bracket operator [ ]). Note that we have 1-dimensional, 2-dimensional and multi-dimensioanl arrays.

General syntax

type

arrayVariableName = array[startValue..endValue] of datatype

var

newVariableName : arrayVariableName    //you create a new variable from the array type

 


Program exercises implementing Array type in Pascal programming 

Lab3

//define array, initialize it and print them using for loop for iteration

program lab3;

type

score = array[1..5] of integer;

var

xcount : integer;

tscore : score;

 

begin

  //assign values

  tscore[1] := 30;

  tscore[2] := 15;

  tscore[3] := 27;

  tscore[4] := 8;

  tscore[5] := 12;

 

  //display

  for xcount := 1 to 5 do

  begin

  writeln ('Array value:: ', tscore[xcount]);

   end;

 end.

 

Lab4

//sum elements in array

program lab4;

type

score = array[1..5] of integer;

var

xcount, sum : integer;

tscore : score;

 

begin

  sum := 0;

  //assign values

  tscore[1] := 30;

  tscore[2] := 15;

  tscore[3] := 27;

  tscore[4] := 8;

  tscore[5] := 12;

 

  //iterate the elements, store them in sum variable

  for xcount := 1 to 5 do

  begin

  sum := sum +  tscore[xcount];

  end;

  //display the total

  writeln ('Array value:: ', sum);

 

end.

 

Lab5

//read or accept data into array

program lab5;

type

fullname = array[1..5] of string;

var

xcount, ycount : integer;

sname : fullname;

 

begin

 

  //iterate and insert values

  for xcount := 1 to 5 do

  begin

  writeln('insert your lastname and firstname');

  readln(sname[xcount]);

  end;

  //iterate and display values entered

  for ycount := 1 to 5 do

  begin

  writeln ('Value inserted into the array:: ', sname[ycount]);

  end;

end.

OUPTUT SCREENSHOT:

 


Comments

Popular posts from this blog