Arrays Reference
Arrays Reference
C COLLEGE OF
ENGINEERING.
Arrays
An array is a collection of elements of similar type, those sharing a common name and
occupies contiguous locations.
DECLARATION OF ARRAYS
Array may be of one dimensional or multi dimensional. Declarations of the one
dimensional arrays, two dimensional arrays and three dimensional arrays are as follows.
Here data_type refers to the data type of the elements in the array and it can be a
primitive data type. arrayname1, arrayname2…..etc refers to the identifiers which
represent the array name. Size is an integer expression representing total number of
elements in the array.
Example
int Num[10];
It is the declaration of a one dimensional array which defines an integer array name Num
of size 10 and that represents a block of 10 consecutive storage locations. Here each
element in the array is represented by Num[0], Num[1],….,Num[9], where 0, 1, 2, …, 9
represents subscripts or indices of the array.
1024 1026 1028 1030 1032 1034 1036 1038 1040 1042
Addresses
1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.
Let us consider the above example Num it self represent the base address of the
entire array. &Num [0] also represents the base address. &Num [5] represents the
address of the 6th element in the array Num, and it is calculated using the formulae
The scale factor is the number of byes allocated for each element of the array.
The address of array Num [5] is calculated as
&Num [5] = 1024 + 5 * 2 = 1034.
Here &Num [5] is also represented as (num+5).
Here data_type refers to the data type of the elements in the array and it can be a
primitive data type. array_name refers to the identifiers which represent the array name.
Size1 is an integer expression representing the row size in the array and Size2 is also
an integer expression representing the column size in the array. In a two dimensional
array two subscripts are used in two pairs of square brackets.
Example
int Num[2][2];
It is the declaration of a two dimensional array which defines an integer array name Num
of row size 2 with column size 2 and that represents a block of 4 consecutive storage
1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.
In general Num[ i ][ j ] refers to the element of the i th row and jth column in the
array. The representation of a two dimensional array in the memory is as follows.
Let us consider the above example Num it self represent the base address of the
entire array. &Num [0][0] also represents the base address. &Num [1][0] represents the
address of the 2nd row 1st column element in that array Num, and it is calculated using
the formulae
Address of the [ i, j]th element = base address + i * total no of columns * scale factor + j * scale factor
The scale factor is the number of byes allocated for each element of the array.
The address of array Num [1][1] is calculated as
&Num [1][1] = 1024 + 1 * 2 *2 + 1*2= 1030.
Write a program to read a two dimensional array and print that array.
#include<stdio.h>
main( )
{
int a[10][10], i, j, m, n ;
printf (“ Enter the number of rows and columns: “);
scanf (“ %d %d“, &m, &n );
for( i=0; i<m; i++ )
{
for( j=0; j<n; j++ )
{
printf ( “ Enter the value of a[%d][%d] : “ , i, j );
scanf (“ %d “, &a[ i ][ j ]);
}
}
printf (“ The array elements are ; “);
for( i=0; i<m; i++ )
for( j=0; j<n; j++)
printf (“ \t %d “, a[ i ][ j ]);
}
1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.
In the three dimensional array 3 subscripts are placed as three pairs of square brackets,
Where as size1 represents the number of pages, size2 represents the number of rows
and size3 represents the number of columns. The total number of elements in a three
dimensional array is calculated as No of pages * No of rows * No of columns.
Example
int a[2][2][2];
The value in the kth page, ith row and jth column is referred to by a[ k ][ i ][ j ]. The memory
representation of a three dimensional array is as follows.
Write a program to read p pages, m rows and n columns of elements and print them.
#include<stdio.h>
main()
{
int a[3][3][3], i, j, k, p, m, n;
printf ( “ Enter how many page numbers, rows and columns you want\n “);
scanf ( “ %d %d %d “, &p, &m, &n);
for( k=0; k<p; k++)
{
for( i=0; i<m; i++)
{
for( j=0; j<n; j++)
{
printf ( “ Enter element for a[%d][%d][%d] : “,k, i, j);
scanf ( “ %d “, &a[ k ][ i ][ j ]);
1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.
}
}
}
INITIALISATION OF ARRAYS
The elements of an array may be assigned with the values using initialization
instead of reading them by the I/O functions. An array can be initialized in its declaration
only. The lists of values are enclosed in braces. The values are separated by commas
and they must be constants or constant expressions.
If the number of values initialised for an array is less than the size mentioned, the
missing elements are assigned to zero.
Example
int a[3][4]={ {1,2},{3,4,5} };
In the above example the elements a[0][0], a[0][1] of the 0 th row and a[1][0], a[1]
[1] and a[1][2] of the 1 st row are initialised with the values 1,2,3,4 and 5 respectively. All
the other elements are initialised to zero. If the number of initial values exceeds the size
of the array, it is an error.
Example
int a[3][4]={1,2,3,4,5};
In the above example the values are assigned from left end to a[0][0], a[0][1], a[0]
[2], a[0][3] and a[1][0] according to the order of the storage representation.
1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.
The size of a one dimensional array need not be mentioned in its initialization. In
this case, the compiler will count the values assigned and take it as the size of that
array. In multi dimensional arrays the leftmost subscript may be omitted and all others
must be specified.
Example
int x[ ]={1,2,3,4,5};
The initialization makes the array x having 5 elements
Example
int a[ ][2]={1,2,3,4};
The initialization makes the array a having 2 rows and 2 columns.
Example
float x[ ][3]={{1},{2},{3}};
The declaration represents a three by three array with only the first element in each row
is initialised.
Example
float x[ ][3]={ {1,2,3} };
The above declaration represents a one by three array. Hence, the internal braces are
important in fixing the size of the dimension when leftmost dimension is omitted. The
missing elements of a initialised array are assigned the value zero.
à write a program to find maximum and minimum elements in the given array.
#include<stdio.h>
main()
{
int a[20],i, n, max, min;
printf ( "Enter value of n : ");
scanf ( "%d" , &n);
for( i=0; i<n; i++)
{
printf ( "Enter element for a[%d] : ",i );
scanf ( "%d", &a[ i ]);
}
max=min=a[0];
for( i=1; i<n; i++)
{
if(max < a[ i ])
max=a[ i ];
if(min > a[ i ])
min=a[ i ];
}
1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.
OUTPUT:
Enter value of n : 5
Enter element for a[0] : 2
Enter element for a[1] : 5
Enter element for a[2] : 6
Enter element for a[3] : 3
Enter element for a[4] : 8
Maximum element =8
Minimum element=2
à Write a program to find no.of +ve and –ve elements in the given array.
main()
{
int a[10],p=0,n=0,i,x;
printf("enter the size of the array \n");
scanf("%d",&x);
printf("enter the elements in the array\n");
for(i=0;i<x;i++)
scanf("%d",&a[i]);
for(i=0;i<x;i++)
{
if(a[i]>0)
p++;
else
n++;
}
printf("no.of +ve numbers are %d",p);
printf("no of -ve numbers are %d",n);
}
OUTPUT:
enter the size of the array
5
enter the elements in the array
2
-8
5
-9
1
no.of +ve numbers are 3no of -ve numbers are 2
1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.
OUTPUT:
1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.
main()
{
int a[10],b[10],n,i;
printf("enter the size of the array\n");
scanf("%d",&n);
printf("enter the elements in to the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("the array elements in A are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
for(i=0;i<n;i++)
b[i]=a[i];
printf("\n the array elements in B are\n");
for(i=0;i<n;i++)
{
printf("%d\t",b[i]);
}
}
OUTPUT:
main()
{
int a[10],n,i;
printf("enter the size of the array\n");
scanf("%d",&n);
printf("enter the elements in to the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("the array elements in A are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\n the reverse elements in A are\n");
1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.
for(i=n-1;i>=0;i--)
{
printf("%d\t",a[i]);
}
}
OUTPUT:
main()
{
int a[10],n,i,size;
printf("enter the number\n");
scanf("%d",&n);
i=0;
while(n>0)
{
a[i]=n%10;
n=n/10;
i++;
}
size=i;
for(i=size-1;i>=0;i--)
{
switch(a[i])
{
case 0:printf("zero\t");break;
case 1:printf("one\t");break;
case 2:printf("two\t");break;
case 3:printf("three\t");break;
case 4:printf("four\t");break;
case 5:printf("five\t");break;
case 6:printf("six\t");break;
1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.
case 7:printf("seven\t");break;
case 8:printf("eight\t");break;
case 9:printf("nine\t");break;
}
}
}
OUTPUT:
enter the number
2345
two three four five
à write a program to find the binary equivalent of the given number
main()
{
int a[10],n,i,size;
printf("enter the number\n");
scanf("%d",&n);
i=0;
while(n>0)
{
a[i]=n%2;
n=n/2;
i++;
}
size=i;
for(i=size-1;i>=0;i--)
{
printf("%d\t",a[i]);
}
}
OUTPUT:
enter the number
12
1 1 0 0
1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.
a[j-1]=a[k-1];
a[k-1]=temp;
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
OUTPUT:
enter the size of the array
5
enter the elements in to the array
1
2
3
4
5
the array elements are
1 2 3 4 5
enter the positions to be reversed
3
4
1 2 4 3 5
à Write a program to replace an element in the array with the given element.
main()
{
int a[10],n,i,old,new,c=0;
printf("enter the size of the array\n");
scanf("%d",&n);
printf("enter the elements in to the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("the array elements are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\nenter the element to be replaced\n");
scanf("%d",&old);
printf("enter the replacing elment\n");
scanf("%d",&new);
for(i=0;i<n;i++)
{
if(a[i]==old)
{
a[i]=new;
c++;
}
}
if(c==0)
printf("the element is not found\n");
else
1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.
{
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
}
OUTPUT:
enter the size of the array
5
enter the elements in to the array
1
2
3
4
5
the array elements are
1 2 3 4 5
enter the element to be replaced
4
enter the replacing elment
9
1 2 3 9 5
1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.
main()
{
int a[10],i,n,sum=0;
printf("enter the array size\n");
scanf("%d",&n);
printf("enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
printf("the sum of the array elements is %d",sum);
}
OUTPUT:
enter the array size
5
enter the array elements
1
2
3
4
5
the sum of the array elements is 15
à Write a program to find sum of even no’s of sum of odd no’s in the given array.
#include<stdio.h>
main()
{
int a[10],i,n,evensum=0,oddsum=0;
printf("enter the array size\n");
scanf("%d",&n);
printf("enter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("the array elements are\n");
for(i=0;i<n;i++)
printf("%d",a[i]);
for(i=0;i<n;i++)
{
if(a[i]%2==0)
evensum=evensum+a[i];
else
oddsum=oddsum+a[i];
}
printf("the sum of the even numbers are %d\n",evensum);
printf("the sum of odd numbers are %d\n",oddsum);
}
OUTPUT:
enter the array size
5
1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.
à Write a program to read elements from the keyboard and display the even number
array and odd number array separately.
#include<stdio.h>
main()
{
int a[10],i,n,e[10],o[10],oi=0,ei=0;
printf("enter the array size\n");
scanf("%d",&n);
printf("enter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("the array elements are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
for(i=0;i<n;i++)
{
if(a[i]%2==0)
e[++ei]=a[i];
else
o[++oi]=a[i];
}
printf("\nthe even numbers array is\n");
for(i=1;i<=ei;i++)
printf("%d\t",e[i]);
OUTPUT:
enter the array size
6
enter the array elements
1
2
1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.
3
4
5
6
the array elements are
1 2 3 4 5 6
the even numbers array is
2 4 6
the odd numbers array is
1 3 5
0 1 1 2 3 5 8 13 21 34
if(a[i]!=x)
printf("%d elememt is not found\n",x);
else
printf("%d element is found\n",x);
}
OUTPUT:
1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.
enter a number
9
enter the array elements
1
2
3
4
5
5
6
7
8
9
the array elements are
1 2 3 4 5 5 6 7 8 9
9 element is found