0% found this document useful (0 votes)
47 views

Arrays

Arrays are variables that contain multiple elements of the same data type. They can be one-dimensional, containing elements accessed via a single index, or two-dimensional with rows and columns accessed using two indices. Elements in an array can be accessed individually or sorted into a particular order using algorithms like bubble sort. Two-dimensional arrays are useful for representing grid-like data with multiple rows and columns.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Arrays

Arrays are variables that contain multiple elements of the same data type. They can be one-dimensional, containing elements accessed via a single index, or two-dimensional with rows and columns accessed using two indices. Elements in an array can be accessed individually or sorted into a particular order using algorithms like bubble sort. Two-dimensional arrays are useful for representing grid-like data with multiple rows and columns.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Arrays

Arrays are variables that are made up of many variables of the same data type but have only one name. Here is a visual representation of an array with 5 elements: 1 value 1 2 value 2 3 value 3 4 value 4 5 value 5 Arrays are declared in almost the same way as normal variables are declared except that you have to say how many elements you want in the array. program Arrays; var a: array[1..5] of Integer; begin end. We access each of the elements using the number of the elements behind it in square brackets. program Arrays; var a: array[1..5] of Integer; begin a[1] a[2] a[3] a[4] a[5] end. := := := := := 12; 23; 34; 45; 56;

It is a lot easier when you use a loop to access the values in an array. Here is an example of reading in 5 values into an array: program Arrays; var a: array[1..5] of Integer; i: Integer; begin for i := 1 to 5 do Readln(a[i]); end.

Sorting arrays
You will sometimes want to sort the values in an array in a certain order. To do this you can use a bubble sort. A bubble sort is only one of many ways to sort an array. With a bubble sort the biggest

numbers are moved to the end of the array. You will need 2 loops. One to go through each number and another to point to the other number that is being compared. If the number is greater then it is swapped with the other one. You will need to use a temporary variable to store values while you are swapping them. program Arrays; var a: array[1..5] of Integer; i, j, tmp: Integer; begin a[1] a[2] a[3] a[4] a[5] := := := := := 23; 45; 12; 56; 34;

for i := 1 to 4 do for j := i + 1 to 5 do if a[i] > a[j] then begin tmp := a[i]; a[i] := a[j]; a[j] := tmp; end; for i := 1 to 5 do writeln(i,': ',a[i]); end.

2D arrays
Arrays can have 2 dimensions instead of just one. In other words they can have rows and columns instead of just rows. 1 2 3 11 2 3 24 5 6 37 8 9 Here is how to declare a 2D array: program Arrays; var a: array [1..3,1..3] of Integer; begin end. To access the values of a 2d array you must use 2 numbers in the square brackets. 2D arrays also require 2 loops instead of just one. program Arrays;

var r, c: Integer; a: array [1..3,1..3] of Integer; begin for r := 1 to 3 do for c := 1 to 3 do Readln(a[r,c]); end. You can get multi-dimensional arrays that have more than 2 dimensions but these are not used very often so you don't need to worry about them.

You might also like