Arrays
Array is a set of values of same data type stored under a
single variable name
General form of declaring an array
data-type array-name[size];
int mark[5]; (10bytes)
mark
2 bytes 2 bytes 2 bytes 2 bytes 2 bytes
50 65 89 88 98
mark[0] mark[1] mark[2] mark[3] mark[4]
2000 2002 2004 2006 2008
i
i+2
i+4
i+6
mark[0]=50
mark[1]=65
mark[2]=89
mark[3]=88
mark[4]=98
The number in the square bracket refers to array Index or
array subscript
Types of arrays
1. One dimensional array (or) linear array (or) single
dimensional array - array with one subscript is called
one dimensional array. a[5]
2. Two dimensional array array - array with two
subscripts is called as two dimensional array.a[2][2]
3. Three dimensional array - array with three subscripts
is called three dimensional array.a[2][2][2]
4. Multidimensional array - array with more than three
subscripts is called multidimensional array.
General form of declaring two dimensional array
data-type array-name[row][column];
int a[3][3];
1c 2c 3c
1st row 0,0 0,1 0,2
2nd row 1,0 1,1 1,2
3rd row 2,0 2,1 2,2
Array initialization can be done in two ways
1. During compile time
2. During run time
1. Compile time initialization
Assigning values to the array elements during
declaration is called compile time initialization.
General form for initializing array during compile
time
data-type array-name[size]={list of values};
int a[5] = {50,25,98,88,76};
a[0]=50
a[1]=25
a[2]=98
a[3]=88
a[4]=76
float b[3]={4.2,8.4,9.1};
b[0]=4.2
b[1]=8.4
b[2]=9.1
char ch[3]={‘h’,’a’,’i’};
ch[0]=’h’
ch[1]=’a’
ch[2]=’i’
If the size is not specified explicitly, then the
number of values is taken as the size
automatically by the compiler
int a[]={3,6,7};
a[0]=3
a[1]=6
a[2]=7
If the size of the array is greater than the values
then the remaining values are automatically
initialized to zero
int a[5]={3,6,7};
a[0]=3
a[1]=6
a[2]=7
a[3]=0
a[4]=0
If the size of the array is lesser than the number
of values then it is invalid and it will give error
int a[5]={3,6,7,4,8,9}
2. .
Run time initialization is done using the scanf()
statement when you run the program.
int a[10];
for(i=0;i<10;i++) 0-9=10times
{
scanf(“%d”,&a[i]);
sum=sum+a[i];
}
i=0 0<10 true
a[0]=12
sum=0+12=12
i=1 1<10 True
a[1]=20
sum=12+20=32
i=2 2<10 true
a[2]=23
sum=32+23=55
a[9]
10<10False
temp=a
a=b
b=temp
5 9 7 3 4
7
3rd location
The element is not present in the array
for(i=0;i<5;i++)
{
if (a[i]==s)
{
printf("\nThe number is present in the
location %d",i+1);
c=1;
}
}
if(c==0)
{
printf("\nThe number is not in the given
array");
}
45 35 76 35 23
s=35
i=0<5 True
A[0]
45=35 False
i=1<5 True
A[1]
35=35 True
The number is present in the location 2
c=1
i=2<5 True
A[2]
76=35 False
i=3<5 True
A[3]
35=35 True
The number is present in the location 4
c=1
i=4<5 true
A[4]
23=35 False
i=5<5 False
C program to Insert an element in an Array
An array is a collection of items stored at contiguous memory locations. In this article,
we will see how to insert an element in an array in C.
Given an array arr of size n, this article tells how to insert an element x in this array arr
at a specific position pos.
Steps :
1. First get the element to be inserted, say x
2. Then get the position at which this element is to be inserted, say pos
3. Then shift the array elements from this position to one position forward,
and do this for all the other elements next to pos.
4. Insert the element x now at the position pos, as this is now empty.
// C Program to Insert an element at a specific position in an
Array
#include <stdio.h>
void main()
{
int arr[15];
int i, x, pos,n=10;
printf(“Enter %d elements for the array : “,n);
for (i = 0; i < n; i++)
scanf(“%d”,&arr[i]);
printf(“\nEnter the element to be inserted :”);
scanf(“%d”,&x);
printf(“\nEnter the position to insert the element :”);
scanf(“%d”,&pos);
printf(“\nOriginal Array : “);
for (i = 0; i < n; i++)
printf("%d\t", arr[i]);
n++;
n=11-1=10>=5Yes
arr[10]=arr[9]
i=10-1=9>=5 Y
arr[9]=arr[8]
i=8>=5Y
arr[8]=arr[7]
i=7>=5Y
arr[7]=arr[6]
i=6>=5Y
arr[6]=arr[5]
i=5>=5 Y
arr[5]=arr[4]
i=4>=5F
for (i = n-1; i >= pos; i--)
{
arr[i] = arr[i - 1];
}
arr[pos - 1] = x;
printf(“\nArray after insertion :”);
for (i = 0; i < n; i++)
printf("%d\t", arr[i]);
}
arr
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
10 20 22 34 38 66 45 98 21 16 76
x= 38
pos=5
i = 11-1=10 >= 5 true
arr[10]=arr[9]
i=9 >= 5 true
arr[9]=arr[8]
i=8 >= 5 true
arr[8]=arr[7]
i=7 >= 5 true
arr[7]=arr[6]
i=6>=5 true
Arr[6]arr[5]
i=5>=5 true
arr[5]=arr[4]
i=4>=5 false
#include <stdio.h>
void main()
{
int arr[100], pos, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &arr[c]);
printf("Enter the location where you wish to delete element\
n");
scanf("%d", &pos);
if ( pos >= n+1 )
printf("Deletion not possible.\n");
else
{
for ( c = pos - 1 ; c < n - 1 ; c++ )
arr[c] = array[c+1];
printf("Resultant array is\n");
for( c = 0 ; c < n - 1 ; c++ )
printf("%d\n", arr[c]);
}
}
int a, b, x, sum=0;
float y, c;
1001 1201 800 5001 401 6001
a b x sum y c
60 80 76 0 89.67 13.7