You are on page 1of 7

Practical - 1

Aim :- Write, test, debug and execute minimum five programs with
array operations like insertion, searching, merging, sorting and
deletion.
1.1 Insertion operation in array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5], size = 5, i;
clrscr();

for(i=0; i < size; i++)


{
printf( "Enter the new element value:");
scanf( “ %d ”, & a[i]);
}

printf("Entered elements are : ");

for(i=0; i < size; i++)


{
printf( “ %d ” , a[i] ) ;
}

getch();
}

Output:
Enter the new element value: 10

Enter the new element value: 20

Enter the new element value: 30

Enter the new element value: 40

Enter the new element value: 50

Entered elements are: 10 20 30 40 50


1.2 Searching operation in array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={10,20,30,40,50}, size=5, value, i;
clrscr();

printf("Enter the element value to be searched:" );


scanf(“%d”, &value);

for(i=0; i < size; i++)


{
if (a[i] == value)
{
printf( "Element found at index: %d", i );
}
}

getch();
}

Output:

Enter the element value to be searched: 40


Element found at index: 3
1.3 Merging of two array

#include<stdio.h>
#include<conio.h>
void main()
{
int a1[2] = {10,20}, a2[3] = {30,40,50}, a[5] , i, i1, i2;
int a1_size=2, a2_size=3, total;
clrscr();

total = a1_size + a2_size;

i1=0, i2=0;
for(i=0; i<total; i++)
{
if (i < a1_size)
{
a[i] = a1[i1];
i1++;
}
else
{
a[i] = a2[i2];
i2++;
}
}

printf("Merged array (a1 + a2) elements:");


for(i=0; i <total; i++)
{
printf(“%d ”, a[i] );
}
getch();
}

Output:
Merged array (a1 + a2) elements: 10 20 30 40 50
1.4 Sorting operation in array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={30,20,50,40,10}, size = 5, i, j, temp;
clrscr();

for(i=0; i < size; i++)


{
for(j=i+1; j < size; j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
Printf("Array after sorting:");
for(i=0; i < size; i++)
{
printf(“%d ”, a[i] );
}
getch();
}

Output: Array after sorting: 10 20 30 40 50


1.5 Deletion operation in array

#include<stdio.h>
#include<conio.h>
int main()
{
int a[5]={10,20,30,40,50};
int size=5, delete_index,i;
clrscr();

printf("Enter the index value of element to be deleted:");


scanf(“%d”, &delete_index);

for(i=delete_index; i < size; i++)


{
a[i] = a[i+1];
}

size= size - 1;

printf("Array elements after deletion:");


for(i=0; i < size; i++)
{
printf(“%d ”, a[i] );
}
getch();
}

Output:
Enter the index value of element to be deleted: 2
Array elements after deletion: 10 20 40 50

You might also like