Basic Array Programs
Contents
Toggle1. Read and Display elements in an array
#include<stdio.h>
#define size 40
int main()
{
int a[size];
int n,i;
printf(“Enter the size of the array:”);
scanf(“%d”,&n);
printf(“Enter the elemnents of the array:”);
for(i=0;i<n;i++)
{
printf(“\na[%d]= “,i);
scanf(“%d”,&a[i]);
}
printf(“The array elements are : \n”);
for(i=0;i<n;i++)
{
printf(“a1[%d]= %d\n”,i,a[i]);
}
return 0;
}
OUTPUT –
Enter the size of the array:5
Enter the elements of the array:
a[0]= 20
a[1]= 12
a[2]= 98
a[3]= 28
a[4]= 68
The array elements are :
a1[0]= 20
a1[1]= 12
a1[2]= 98
a1[3]= 28
a1[4]= 68
2. Sum of n numbers in an array
#include<stdio.h>
#define size 40
int main()
{
int a[size];
int n,i,sum=0;
printf(“Enter the size of the array:”);
scanf(“%d”,&n);
printf(“Enter the elemnents of the array:”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
sum+=a[i];
}
printf(“\nThe sum of elements : %d “,sum);
}
OUTPUT –
Enter the size of the array: 5
Enter the elemnents of the array:10
20
30
40
50
The sum of elements : 150
3. Merging of two arrays into a third array
#include<stdio.h>
#define size 40
int main()
{
int a1[size],a2[size],a3[size];
int n1,n2,i,temp=0,m;
printf(“Enter the size of the 1st array:”);
scanf(“%d”,&n1);
printf(“Enter the elemnents of the 1st array:”);
for(i=0;i<n1;i++)
{
scanf(“%d”,&a1[i]);
}
printf(“Enter the size of the 2nd array:”);
scanf(“%d”,&n2);
printf(“Enter the elemnents of the 2nd array:”);
for(i=0;i<n2;i++)
{
scanf(“%d”,&a2[i]);
}
printf(“The 1st array elements are : \n”);
for(i=0;i<n1;i++)
{
printf(“a1[%d]= %d\n”,i,a1[i]);
}
printf(“The 2nd array elements are : \n”);
for(i=0;i<n2;i++)
{
printf(“a2[%d]= %d\n”,i,a2[i]);
}
m=n1+n2;
for(i=0;i<n1;i++)
{
a3[temp]=a1[i];
temp++;
}
for(i=0;i<n2;i++)
{
a3[temp]=a2[i];
temp++;
}
printf(“The merged array elements are : \n”);
for(i=0;i<m;i++)
{
printf(“a3[%d]= %d\n”,i,a3[i]);
}
}
OUTPUT –
Enter the size of the 1st array: 5
Enter the elemnents of the 1st array:10
20
30
40
50
Enter the size of the 2nd array:5
Enter the elemnents of the 2nd array: 60
70
80
90
100
The 1st array elements are :
a1[0]= 10
a1[1]= 20
a1[2]= 30
a1[3]= 40
a1[4]= 50
The 2nd array elements are :
a2[0]= 60
a2[1]= 70
a2[2]= 80
a2[3]= 90
a2[4]= 100
The merged array elements are :
a3[0]= 10
a3[1]= 20
a3[2]= 30
a3[3]= 40
a3[4]= 50
a3[5]= 60
a3[6]= 70
a3[7]= 80
a3[8]= 90
a3[9]= 100
4. Inserting an element at a given location in an array
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
int main()
{
int i, n ,num , pos , arr[20];
printf(“\nEnter the number of elements in the array : “);
scanf(“%d” , &n);
printf(“\nEnter the elements : “);
for(i=0;i<n;i++)
{
printf(“\n arr[%d] = “,i);
scanf(“%d”, &arr[i]);
}
printf(“\nEnter the number to be inserted : “);
scanf(“%d” , &num);
printf(“\nEnter the position at which the number has to be inserted : “);
scanf(“%d” , &pos);
for(i=n-1;i>=pos;i–)
{
arr[i+1]=arr[i];
}
arr[pos]=num;
n=n+1;
printf(“\nThe array after insertion of %d is : “,num);
for(i=0;i<n;i++)
{
printf(“\n arr[%d] = %d”,i,arr[i]);
}
return 0;
}
OUTPUT –
Enter the number of elements in the array : 5
Enter the elements :
arr[0] = 20
arr[1] = 30
arr[2] = 45
arr[3] = 56
arr[4] = 78
Enter the number to be inserted : 19
Enter the position at which the number has to be inserted : 0
The array after insertion of 19 is :
arr[0] = 19
arr[1] = 20
arr[2] = 30
arr[3] = 45
arr[4] = 56
arr[5] = 78
5. Inserting an element in an array that is already sorted in ascending order
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
int main()
{
int i, n ,num , j, arr[20];
printf(“\nEnter the number of elements in the array : “);
scanf(“%d” , &n);
printf(“\nEnter the elements : “);
for(i=0;i<n;i++)
{
printf(“\n arr[%d] = “,i);
scanf(“%d”, &arr[i]);
}
printf(“\nEnter the number to be inserted : “);
scanf(“%d” , &num);
for(i=0;i<n;i++)
{
if(arr[i]>num)
{
for(j=n-1;j>=1;j–)
{
arr[j+1]=arr[j];
}
arr[i]=num;
break;
}
}
n=n+1;
printf(“\nThe array after insertion of %d is : “,num);
for(i=0;i<n;i++)
{
printf(“\n arr[%d] = %d”,i,arr[i]);
}
return 0;
}
OUTPUT –
Enter the number of elements in the array : 5
Enter the elements :
arr[0] = 10
arr[1] = 30
arr[2] = 40
arr[3] = 50
arr[4] = 60
Enter the number to be inserted : 20
The array affter insertion of 20 is :
arr[0] = 10
arr[1] = 20
arr[2] = 30
arr[3] = 40
arr[4] = 50
arr[5] = 60