module3 _arrays
module3 _arrays
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
An array which has only one subscript is known as one dimensional array.
Syntax
datatype array_name[size];
where,
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
In this type of array initialization, initialize all the elements of specified memory size.
Example : int a[5]={2,4,34,3,4};
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]
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].
10 11 12 13 14
a[0] a[1] a[2] a[3] a[4]
Here, length= 4-0+1=5.
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
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):
}
Syntax
datatype array_name[row_size][column_sze];
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
. . . . .
. . . . ………. .
. . . . .
m-1 a[m-1][0] a[m-1][1] a[m-1][2] ………. a[m-1][n-1]
Initialization of 2D array:
2 4
a[0][0] a[0][1]
5 6
a[1][0] a[1][1]
2 4
a[0][0] a[0][1]
0 0
a[1][0] a[1][1]
2 4
a[0][0] a[0][1]
5 6
a[1][0] a[1][1]
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]