Module3
MODULE 3
ARRAYS
Introduction :
An array is a collection of similar data elements of same data type stored at consecutive
memory locations.
All the elements of the array share a common name .
Each element in the array can be accessed by the subscript(or index) and array name.
The arrays are classified as:
1. One dimensional array
2. Two dimensional array
3. Multidimensional array
One dimensional array:
An array which has only one subscript is known as one dimensional array.
Declaring an one dimensional array:
Syntax
datatype array_name[size];
where,
datatype : It indicates the type of data stored in it. It can be
int,float,char,double. array_name : It is the name of the array and it should be an
valid identifier.
Size : An integer constant that indicates the total number of elements in array.
Here, index starts with 0 and ends with (size-1).
Example : int a[5];
Here, we declared an array ‘a’ of integer type and its size is ‘5’, meaning that it can hold five integer
values. It allocates 5*2=10 Bytes of memory for the array.
Initialization of 1D array:
To store the values in array, there are mainly two methods.
1. Compile time initialization
a) Initializing all elements of an array.
b) Partial array initialization.
c) Initialization without size.
d) Assigning Values to individual elements of an arrays.
2. Run time initialization
Mrs. Bhavya P S.,CSE dept. Page 1
Module3
1. Compile time initialization
a) Initializing all elements of an array.
In this type of array initialization, initialize all the elements of specified memory size.
Example : int a[5]={2,4,34,3,4};
b) Partial array initialization:
If the number of values to be initialized is less than the size of array then it is called as
partial array initialization.
The remaining elements will be initialized to zero automatically by the compiler.
Example : int a[5]={10,20};
10 20 0 0 0
a[0] a[1] a[2] a[3] a[4]
c) Initialization without size:
If the size of an array is not initialized, then the compiler will set the size based on the
number of initial values.
Example: int a[ ]={10,20,30,40,50};
In the above example the size of an array is set to 5.
10 20 30 40 50
a[0] a[1] a[2] a[3] a[4]
d) Assigning Values to individual elements of an array.
Using assignment operators, we can assign values to individual elements of arrays.
For example:
int a[3];
a[0]=10;
a[1]=20;
a[2]=30;
10 20 30
a[0] a[1] a[2]
2. Run time initialization:
An array can be filled by inputting values from the keyboard.
Example: initializing using for
loop for(i=0;i<5;i++)
scanf(“%d”,&a[i]);
Mrs. Bhavya P S.,CSE dept. Page 2
Module3
Accessing elements of an array :
For accessing an individual element of the array, the array subscript must be used.
For example, consider
int a[5]={10,11,12,13,14,15}
10 11 12 13 14
a[0] a[1] a[2] a[3] a[4]
Here, in order to access fourth element we must write a[3].
Calculating the length of the array:
The formula to calculate length of the array
is Length = upper_bound-lower_bound+1
Where,
upper_bound = index of last element in the array
lower bound= index of first element in the array.
Example : int a[5]={10,11,12,13,14,15}
10 11 12 13 14
a[0] a[1] a[2] a[3] a[4]
Here, length= 4-0+1=5.
Memory occupied by 1D array:
Total memory occupied is given by
Total memory = array_size * size of data_type
Example : Example : int a[5]={10,11,12,13,14,15}
10 11 12 13 14
a[0] a[1] a[2] a[3] a[4]
Total memory = 5*2=10 bytes.
Problem : given an array int a[5]={10,11,12,13,14,15}, calculate the address of a[4] if base address is
1000.
10 11 12 13 14
a[0] a[1] a[2] a[3] a[4]
1000 1002 1004 1006 1008
Address of a[4] is 1008
Mrs. Bhavya P S.,CSE dept. Page 3
Module3
Example : Write a C program to read N elements from keyboard and to print N elements on screen.
(Write a C program to read and print 1D array)
#include<stdio.h>
void main()
{
int i,n,a[10];
printf(“enter number of array elements\
n”); scanf(“%d”,&n);
printf(“enter array elements\
n”); for(i=0; i<n;i++)
scanf(“%d”,&a[i]);
printf(“array elements are\n”);
for(i=0; i<n;i++)
printf(“%d”,a[i]);
}
Example 2: Write a C program to find sum of n array elements .
#include
void main()
{
int i,n,a[10],sum=0;
printf(“enter number of array elements\
n”); scanf(“%d”,&n);
printf(“enter array elements\
n”); for(i=0; i<n;i++)
scanf(“%d”,&a[i]);
printf(“array elements are\n”);
for(i=0; i<n;i++)
printf(“%d”,a[i]);
for(i=0; i<n;i++)
sum=sum+ a[i];
printf(“sum is %d\n”,sum):
}
Two dimensional arrays
Declaration and initialization :
An array which has only one subscript is known as one dimensional array.
Declaring a two dimensional array:
Syntax
datatype array_name[row_size][column_sze];
Mrs. Bhavya P S.,CSE dept. Page 4
Module3
where,
datatype : It indicates the type of data stored in it.
array_name : It is the name of the array and it should be an valid identifier.
Row_size : number of values stored in rows and ranges from ‘0’ to ‘row_size-1’.
Column_size : number of values stored in columns and ranges from ‘0’ to ‘column_size-
1’.
Consider a 2D array,’a’ of integer data_type which contains ‘m’ rows and ‘n’ columns and can be shown
as int a[m][n], where,
‘m’ represents row, ranges from ‘0’ to ‘m-1’.
‘n’ represents column, ranges from ‘0’ to ‘n-
1’.
0 1 2 ………. n-1
0 a[0][0] a[0][1] a[0][2] ………. a[0][n-1]
1 a[1][0] a[1][1] a[1][2] ………. a[1][n-1]
2 a[2][0] a[2][1] a[2][2] ………. a[2][n-1]
. . . . .
. . . . ………. .
. . . . .
m-1 a[m-1][0] a[m-1][1] a[m-1][2] ………. a[m-1][n-1]
Example : int a[2][3];
Initialization of 2D array:
There are mainly two methods.
1. Compile time initialization
a) Initializing all elements of an array.
b) Partial array initialization.
c) Initialization without size.
d) Assigning Values to individual elements of an arrays.
2. Run time initialization
a) Initializing all elements of an array:
In this type of array initialization, initialize all the elements of specified memory size.
Mrs. Bhavya P S.,CSE dept. Page 5
Module3
Example : int a[2][2]={2,4,5,6};
This can also be written as
int a[2][2]={{2,4},{5,6}};
2 4
a[0][0] a[0][1]
5 6
a[1][0] a[1][1]
b) Partial array initialization:
If the number of values to be initialized is less than the size of array then it is called as partial
array initialization.
The remaining elements will be initialized to zero automatically by the compiler.
Example : int a[2][2]={{2,4}};
2 4
a[0][0] a[0][1]
0 0
a[1][0] a[1][1]
c) Initialization without size:
In two dimensional array the first dimension can be omitted.
Example: int a[ ][2]={2,4,5,6};
In the above example the first dimension is set to ‘2’ by the compiler.
2 4
a[0][0] a[0][1]
5 6
a[1][0] a[1][1]
d) Assigning Values to arrays
Using assignment operators, we can assign values to individual elements of arrays.
For example:
int a[2][2];
a[0][0] = 2
a[0][1] = 4
a[1][0] = 5
a[1][1] = 6
2 4
a[0][0] a[0][1]
5 6
a[1][0] a[1][1]
2. Run time initialization
An array can be filled by inputting values from the keyboard.
Mrs. Bhavya P S.,CSE dept. Page 6
Module3
Example: initializing using for
loop for(i=0;i<5;i++)
for(j=0;j<5;j++)
scanf(“%d”,&a[i][j]);
Example: Write a C program to read and display two dimensional matrix
#include<stdio.h>
int main()
{
int a[10][10],m,n,i,j;
printf("enter the order of matrix
A"); scanf("%d%d",&m,&n);
printf("Enter the elements of the matrix\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &a[i][j]);
printf("The given matrix A is \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf(" %d\t", a[i][j]);
}
printf("\n");
}
Mrs. Bhavya P S.,CSE dept. Page 7