Arrays
Arrays
Arrays
An array is a collection of variables of the homogeneous data type that are referred to through a
common identifier and are stored contiguously in memory. An Individual element in an array is
accessed by an index value. The base address corresponds to the first element and the highest
address to the last element of an array. Arrays may have from one to several dimensions.
Irrespective of their dimensions, the arrays are stored linearly in the memory.
The size of an array is predefined and cannot be changed during run time. It can prove to be a big
disadvantage when working with large size of data elements. Under utilization of elements cause
huge amount of unused memory that is eventually wasted.
Declaration of an array
Like any other variable of built-in data type, array must be explicitly declared to enable the compiler
to allocate space for it in memory. How much memory is to be allocated depends on the size and type
of the array.
Syntax,
type name[size];
For example,
int array[10]; // an array with 10 elements
Memory allocated to array depends on the size or number of elements of an array and the
base type of the array.
Let us consider the following memory allocation to an integer array of 5 elements in
memory
For example,
int array[10]; // an array with 10 elements
integer takes 2 bytes in memory, so an array of 10 elements will take 20 bytes in memory.
An array of any data type can be initialized when it is declared. Not initializing an array
results in garbage value in all the elements of the array.
For example,
For example,
2
An element of the array is accessed by indexing the array name. Each element of an array is uniquely
identified by its index. By indexing we mean to place the index of the element within square brackets
after the name of the array. The value or index given in square brackets is also known as subscript
value. The smallest index starts from 0 and the largest index value is size-1.
For example,
#include<conio.h>
#include<stdio.h>
void main()
#include<conio.h>
3
#include<stdio.h>
void main()
{
int array[10], loc, count;
{
printf("Enter element %d of the array :- ", count);
scanf("%d",&array[count]);
loc =0; // let the consider the first element to be the smallest element for (count
= 1; count < 10; count ++)
{
if (array[loc] > array[count])
loc = count;
}
printf("Location of the smallest element %d is :- %d",array[loc],loc);
getch();
Multidimensional arrays
For example,
int arr[3][3];
The last statement is used to create an array with 3 rows and 3 columns. Following is the logical
representation of array in memory.
4
column
row
#include<conio.h>
#include<stdio.h>
main( )
int array[3][3], i, j ;
{
for (j = 0; j < 3; j++)
{
printf("Enter element [%d][%d] of array :- ",i, j);
scanf("%d",&array[i][j]);
//matrix display
printf(”Original
matrix:\n\n"); for (i =
0; i < 3; i++)
{
printf(“\n”);
for (j = 0; j < 3; j++)
{
printf("%d\t",array[i][j]);
5
// diagonal elements of
array
printf(”\n\nDiagonal
elements”); for(i=0,j=0;
i<3; i++, j++)
{
printf("\n%d",array[i][j]);
}
getch( );