You are on page 1of 10

Arrays Introduction

Session 8.a
Array

• Array is a linear collection of


homogeneous elements
• Linear means consecutive (one below the
other) memory locations
• Homogeneous means ‘of the same data
type’
• These are used to store any set of items of
the same data type.
Array

• The array is a container, and all the


elements in this container are referred with
the container (array) name only.
• To identify each element of the array
individually , every element is given a
number from 0 to size -1. These numbers
are called index of its element.
• The array size is fixed and can not e
increased or decreased in the run time of
the program.
Array Declaration

Syntax: 0 65520
• datatype array_name[size]; 1 65522
– Datatype is any of int, float,2 65524
char..etc
3 65526
– Array_name is a user given
name subjected to identifier4 65528
rules.
– Size is a integer constant 0 65530
only.
1 65531
• Example: int ar[5];
2 65532
• Example: char nm[5];
3 65533
4 65534
Referring Array Element

• To use an array element 0 65520


– Array_name[index] is used 1 65522
– Array_name is the name
2 34 65524
given to the array
– Index is any number from 0 to 3 65526
size-1 4 21 65528
– Index can be an integer
constant or integer variable
0 65530
• Example:
1 65531
– ar[2] = 34;
– x = 4; 2 $ 65532
– ar[x] = 21; 3 65533
– nm[x-2]=‘$’; 4 65534
Array Initializations

• Int ar1[5] = {12,13,14,15,16};


• Int ar2[4] = {11,21,31,41};
• Int ar3[5] = {22,33};
• Int ar4[5] = {66};

12 11 22 66
13 21 33 0
14 31 0 0
15 41 0 0
16 0 0
Multi Dimensioned Arrays
Session 8.b
Multi-Dimenssioned Arrays

• Dimension means the way of arrangement of


elements
• Consider a set of 6 circles, they may be
arranged in various ways as below

Arrangement can be in any number of


dimensions as well like x rows , y columns
and z planes is a 3 dimensional arrangement

Double Dimensioned Arrangement In 2 Rows 3 Columns

Double Dimensioned Arrangement In 3 Rows 2 Columns

Single Dimensioned Arrangement


Double-Dimenssioned Arrays

• The major used dimensions are single and


double.
• The double dimensions arrays in C are
majorly used for matricies representation.
• The declaration of these resemble single
dim., arrays as the following examples
– int a[6]; //single dim array
– int a1[2][3]; //double dim array- 2 rows 3 cols
– Int a2[3][3]; //double dim array- 3 rows 2 cols
Double Dim Array Element
Usage
• To refer to each element in a single dim array we use one index, to
refer to each element in double dim array we use two index’s – row
index and column index.
• How ever, computer treats any dim as single dim only.
• Consider the previous examples
– int a[6]; //single dim array
– int a1[2][3]; //double dim array- 2 rows 3 cols
– Int a2[3][3]; //double dim array- 3 rows 2 cols

0 00 00
1 01 01
2 02 10
3 10 11
4 11 20
5 12 21

You might also like