array
array
An array is a collection of elements of same type that share a common name. The elements of
the array are stored in consecutive memory locations and are referred by an index (also
known as subscript).
Array types:
1. One Dimensional Arrays
2. Multi Dimensional Arrays
Initialization of arrays:
After an array is declared, its elements must be initialized. Otherwise, they will contain
garbage value.
Arrays can be initialized as follows:
Syntax:
Datatype array_name[size]={elements separated by commas};
Ex:
int arr[5]={5,7,3,9,1};
5 7 3 9 1
arr[0] arr[1] arr[2] arr[3] arr[5]
1
UNIT-III
1. TRAVERSAL :
Traversing the array elements means accessing each and every element of the array for a
specific purpose. If arr_name is an array name then, traversing the data elements of an array
includes:
1. Printing every element in the array.
2. Counting the total number of elements in the array.
3. Performing any processing on elements of the array.
Example: write a program to accept elements into the array and print them
#include <stdio.h>
#include<conio.h>
main() {
int arr[10];
int size, i;
2
UNIT-III
clrscr(); Output:
printf(" Enter the size of an array");
Enter the size of an array 5
scanf(" %d",&size);
printf(" Enter %d elements into array \n",size); Enter 5 elements into array
for(i=0;i<size;i++)
{ arr[0] = 1
printf("\n arr[%d] =",i); arr[1] = 2
scanf("%d",&arr[i]); arr[2] = 3
} arr[3] = 4
printf(" \n The array elements are \n");
arr[4] = 5
for(i=0;i<size;i++)
{ The array elements are
printf(" arr[%d] = %d",i,arr[i]); arr[0] = 1
} arr[1] = 2
getch(); arr[2] = 3
} arr[3] = 4
arr[4] = 5
2. INSERTION:
Inserting an element in the array means adding a new data element in an already existing
array.
Example:
#include<stdio.h>
#include<conio.h> Output:
main() Enter the size of an array 5
{ Enter the elements for an array 1
int arr[10];
2
int pos,size,i,num;
printf(" Enter the size of an array "); 3
scanf("%d",&size); 4
printf(" \n Enter the elements for an array "); 5
for(i=0;i<size;i++)
{ Enter the number to be inserted :8
scanf("%d",&arr[i]);
Enter the position to insert the number :2
}
printf("\n Enter the number to be inserted :"); Elements after insertion are 1 2 8 3 4 5
scanf("%d",&num);
printf("\n Enter the position to insert the number :");
scanf("%d",&pos);
for(i=size;i>=pos;i--)
{
arr[i+1]=arr[i];
}
arr[pos]=num;
3
UNIT-III
3. DELETION
Deleting an element from the array means removing a data element from an already existing
array.
// program to delete an element from a given location in an array
#include<stdio.h>
#include<conio.h>
main()
{
int arr[10],i,n,pos;
Output:
printf("\n Enter the size of an array ");
scanf("%d",&n); Enter the size of an array 5
printf("\n Enter the elements of the array "); Enter the elements of the array 1
for(i=0;i<n;i++) 2
{ 3
scanf("%d",&arr[i]); 4
} 5
printf("\n Enter the position to delete ");
scanf("%d",&pos);
for(i=pos;i<n;i++) Enter the position to delete 2
arr[i]=arr[i+1];
n--; The array after deletion is :
printf(" \n The array after deletion is :"); a[0] = 1
for(i=0;i<n;i++) a[1] = 2
printf("\n a[%d] = %d",i,arr[i]); a[2] = 4
getch();
a[3] = 5
}
4. MERGING
Merging two arrays in a third array means first copying the contents of the first array into the
third array and then copying the contents of the second array into the third array. Hence, the
merged array contains contents of the first array followed by the contents of the second array.
Program to merge two unsorted arrays
#include<stdio.h>
#include<conio.h>
main()
Output:
{
int arr1[10],arr2[10],arr3[20]; Enter the size of array1: 3
int n1,n2,index=0,i,m; Enter the elements of array1: 1
printf(" \n Enter the size of array1: "); 2
scanf("%d",&n1); 3
printf(" \n Enter the elements of array1: "); Enter the size of array2: 3
for(i=0;i<n1;i++) Enter the elements of array2: 4
scanf("%d",&arr1[i]);
5
printf("\n Enter the size of array2: ");
scanf("%d",&n2); 6
printf("\n Enter the elements of array2: "); The merged array is
for(i=0;i<n2;i++) arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 4
scanf("%d",&arr2[i]); arr[4] = 5 arr[5] = 6
4
UNIT-III
m=n1+n2;
for(i=0;i<n1;i++)
{
arr3[index]=arr1[i];
index++;
}
for(i=0;i<n2;i++)
{
arr3[index]=arr2[i];
index++;
}
printf(" \n The merged array is \n");
for(i=0;i<m;i++)
{
printf("arr[%d] = %d ",i,arr3[i]);
}
}
5. SEARCHING
Searching means to find whether a particular value is present in the array or not. There are
two popular methods for searching the array elements. One is linear search and the second is
binary search.
Linear search: We can call this as sequential search. In this method we are going to search
the element in sequence order.
flag=1;
break;
}
}
5
UNIT-III
if(flag==1)
Binary search: Binary search method is fast and efficient. Sort the list.
In this method, to search an element we compare it with the element present at the centre of
the list. It matches then the search is successful. Otherwise, the list is divided into two halves.
One from 0th element to the centre element and another from centre element to the last
element.
Write a program to implement binary search
#include<stdio.h>
#include<conio.h>
main()
{
int arr[10],num,i,n,pos=-1,beg,end,mid,found=0;
printf("\n Enter the number of elements in the array : ");
scanf("%d",&n);
printf("\n Enter the elements :");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("\n Enter the number that has to be searched : ");
scanf("%d",&num);
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(arr[mid]==num)
{
printf("\n %d is present in the array at position= %d",num,mid);
found=1;
break;
}
if(arr[mid]>num)
end=mid-1;
else if(arr[mid]<num)
beg=mid+1;
}
if(beg>end && found==0)
printf("\n %d does not exist in the array :",num);
getch();
}
6
UNIT-III
6. SORTING
Sorting means arranging a set of data in an order, either in ascending or in descending order.
Example:
#include<stdio.h>
#include<conio.h>
main()
{
int arr[10],i,j,n;
clrscr();
printf("\n Enter the array size ");
Output:
scanf("%d",&n);
Enter the array size 6
printf("\n Enter the elements :");
for(i=0;i<n;i++) Enter the elements :4
{ 8
scanf("%d",&arr[i]); 1
} 3
printf(" \n Elements after sorting are : \n "); 7
for(i=0;i<n;i++) 2
{
for(j=i+1;j<n;j++)
{ Elements after sorting are :
if(arr[i]>arr[j]) 1 2 3 4 7 8
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
printf("%3d",arr[i]);
}
getch();
}
7
UNIT-III
read_array(num,n);
dis_array(num,n);
getch();
}
void read_array(int arr[10],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\n array[%d]= ",i);
scanf("%d",&arr[i]);
}
}
void dis_array(int arr[10],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\n array[%d]=%d ",i,arr[i]);
}
}
TWO-DIMENSIONAL ARRAY
When an array uses only two subscripts then it is called “Two dimensional array”. It can be
viewed as table of elements, which contains rows and columns. A Two dimensional array is
useful for matrix operations.
0 1 2
0
1
Two rows and three columns marks[2][3].
If we omit values to an array default it is going to take a value 0.
8
UNIT-III
Write a program to accept array elements and print them in matrix form
#include<stdio.h>
#include<conio.h>
main() Output:
{
int a[3][3],i,j; Enter the elements for Matrix A 1
clrscr(); 2
printf(" \n Enter the elements for Matrix A "); 3
for(i=0;i<3;i++) 4
{ 5
for(j=0;j<3;j++) 6
{ 7
scanf("%d",&a[i][j]); 8
} 9
} The elements of Matrx A are :
printf("\n The elements of Matrx A are :\n"); 1 2 3
for(i=0;i<3;i++)
{ 4 5 6
for(j=0;j<3;j++)
{ 7 8 9
printf("%3d",a[i][j]);
}
printf("\n");
}
getch();
}
printf("%3d",a[i][j]);
}
printf("\n");
}
Write a program to sum two matrices Enter the Elements for the Matrix A :1
#include<stdio.h> 2
#include<conio.h> 3
main() 4
{ 5
int a[3][3],b[3][3],c[3][3],i,j; 6
clrscr(); 7
printf("\n Enter the Elements for the Matrix A :"); 8
for(i=0;i<3;i++) 9
{ Enter the Elements for the Matrix B :1
for(j=0;j<3;j++) 2
{ 3
scanf("%d",&a[i][j]); 4
} 5
6
} 7
printf("\n Enter the Elements for the Matrix B :"); 8
for(i=0;i<3;i++) 9
{ Elements of Matix A are:
for(j=0;j<3;j++) 1 2 3
{ 4 5 6
scanf("%d",&b[i][j]); 7 8 9
}
Elements of Matix B are:
}
printf("\n Elements of Matix A are: \n "); 1 2 3
4 5 6
for(i=0;i<3;i++) 7 8 9
{
for(j=0;j<3;j++) Elements after the sum of two matrices
{ are:
printf("%3d",a[i][j]);
} 2 4 6
printf("\n"); 8 10 12
14 16 18
10
UNIT-III
}
printf("\n Elements of Matix B are: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%3d",b[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\n Elements after the sum of two matrices are: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%3d",c[i][j]);
}
printf("\n");
}
getch();
}
}
printf("\n Enter the Elements for the Matrix B :");
for(i=0;i<3;i++)
{
11
UNIT-III
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n Elements of Matix A are: \n ");
Output:
for(i=0;i<3;i++)
{ Enter the Elements for the Matrix
for(j=0;j<3;j++) A :4
{ 5
printf("%3d",a[i][j]); 6
} 7
printf("\n"); 8
} 9
printf("\n Elements of Matix B are: \n"); 10
11
for(i=0;i<3;i++) 12
{
for(j=0;j<3;j++) Enter the Elements for the Matrix
{ B :1
printf("%3d",b[i][j]); 2
} 3
printf("\n"); 4
} 5
for(i=0;i<3;i++) 6
{ 7
for(j=0;j<3;j++) 8
{ 9
c[i][j]=a[i][j]-b[i][j];
} Elements of Matix A are:
} 4 5 6
printf("\n Elements after the subtraction of two matrices7are:
8 \n");
9
10 11 12
for(i=0;i<3;i++)
{ Elements of Matix B are:
for(j=0;j<3;j++) 1 2 3
{ 4 5 6
printf("%3d",c[i][j]); 7 8 9
}
printf("\n");
Elements after the subtraction of two
}
matrices are:
getch();
3 3 3
}
3 3 3
3 3 3
12