Pascal Arrays

Arrays
An Array is a structured data type formed by an organized collection of variables (components) of the same data type and is referenced by a common (single) name e.g. one dimensional and multidimensional (two dimensional and three dimensional) arrays.

It is a sequence of items/ objects of the same data type. These items are called elements of the array and are numbered using an index (subscript).

Each component can be referred to and accessed individually by specifying its position (index) within the collection.

One Dimensional Array
Its memory situation is denoted by only one row / column and it is also called a list. Its elements (components) can be accessed by just giving the index/subscript. Syntax: Var arrayname: Array[Lower-bound .. Upper-bound] of Data Type; {arrayname should be a valid identifier} Example: var studName : Array[1..15] of String[20]; maths: Array[1..15] of integer; Example 2: Finding the Maximum number and position in an array. program maximumpos(input, output); var numbers : Array [1..20] of integer; counter, max, maxpos: integer; begin writeln('Enter 20 numbers one by one'); for counter := 1 to 20 do  readln(numbers[counter]); max := numbers[1]; maxpos := 1; for counter := 2 to 20 do if (max< numbers[counter]) then begin max := numbers[counter]; maxpos := counter; end; writeln('The Maximum Number is', max,'Its position is',maxpos); End.

Bubble sort
It is a sorting algorithm used to arrange a list of quantities in a desired manner (ascending or descending)

Searching an Array using the linear search
Using this algorithm, scanning starts from the first element to the end of the array, if the search value coincides with an array element then the control immediately comes out of the loop after displaying the appropriate message e.g "Number found", otherwise a "number not found message" is displayed.

We can use the standard identifiers Exit or Goto to come out of the loop. program searching(input, output); var numbers : Array [1..10] of Integer; i, searchNo, n : Integer; begin writeln('How many numbers do you want to input?'); readln(n); writeln('Now enter ',n,' Numbers'); for i := 1 to n do       readln(numbers[i]); writeln('Enter number to search'); readln(searchNo); for i := 1 to n do      if (numbers[i] = searchNo) then begin writeln('Number found'); Exit; end; writeln('Number not present'); end.

Inserting an element in an array
For the process to work, you should supply the number of elements for the array, an insertion position and the value to be fed.

This algorithm first moves the last element to one location above it, the process continues until the index reaches the position that the value was entered. program inserting(input, output); var numbers : Array [1..10] of Integer; i, insertNo, n, pos : Integer; begin writeln('How many numbers do you want to input?'); readln(n); writeln('Now enter ',n,' Numbers'); for i := 1 to n do       readln(numbers[i]); writeln('Enter position to insert'); readln(pos); writeln('Enter value to insert); readln(insertNo); for i := n downto pos do      numbers[i+1] := numbers[i]; numbers[i] := insertNo; writeln('The array after insertion'); for i := 1 to n+1 do        writeln(nubers[i]); end.

Deleting an Array element
While having an array of n elements, we can delete an element in this array by specifying (feeding) the position of the element to delete.

The element in position index+1 should be moved to the location pos, therefore decreasing one location for all elements till the index reaches the required number of locations. The result will be n-1 elements. program inserting(input, output); var numbers : Array [1..10] of Integer; i, n, pos : Integer; begin writeln('How many numbers do you want to input?'); readln(n); writeln('Now enter ',n,' Numbers'); for i := 1 to n do       readln(numbers[i]); writeln('Enter position to delete'); readln(pos); for i := pos to n do      numbers[i] := numbers[i+1]; writeln('The array after deletion'); for i := 1 to n-1 do        writeln(nubers[i]); end.

Character Array
An array of characters is called a string. For example, if you want to store 20 names where each name has a maximum of 15 characters, then the declaration will be as follows: var stdNme : Array [1..20] of string[15]; Example: program names(input, output); var name : Array[1..20] of string[15]; i : integer; begin writeln ('Enter 20 names'); for i := 1 to 20 do       readln(name[i]); writeln('Here are the names'); for i := 1 to 20 do       writeln(name[i]); end.

Checking string length
program checkString(input,output); var givenStr: string; sum, i : integer; begin sum := 0; writeln('Enter any word'); readln(givenStr); for i := length(givenStr) downto 1 do       sum := sum + 1; writeln('The word has ',sum,' Characters'); end.

Two Dimensional Array
This is an array whose memory situation is denoted by two or more rows and columns. Its elements are referenced by using a row index followed by a column index e.g. names[1,4]. This is a reference to row index 1 and column index 4 (an element in row 1 and column 4) of an array named names.

Syntax: var arrayname : Array [lower-bound..upperbound, lower-bound..upperbound] of datatype; Example 1: var score :Array[1..10, 1..4] of Integer; Example 2: program matrices(input, output); var matrix :Array[1..4, 1..3] of integer; i, j :integer;{these are the counters for rows and columns} begin writeln('Enter nine elements for the matrix'); for i := 1 to 3 do       for j := 1 to 3 do           readln(matrix[i,j]); writeln('The output of the matrix'); for i := 1 to 3 do       begin for j := 1 to 3 do            write(matrix[i,j]:5); writeln; end; end.

Multidimensional Arrays
This is an array with more than two dimensions, in that it has rows, columns and an additional z axis. It is advisable not to use more than three dimensions as it occupies more memory space.

Syntax: var arrayname : Array[lb..ub, lb..ub, lb..ub]; Example: numbers : Array[1..10, 1..10, 1..10, 1..10]; {will occcupy 10,000 locations in memory}