0% found this document useful (0 votes)
6 views5 pages

Array Program

Arrays and list are very important part of programming

Uploaded by

zinkazame39
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
6 views5 pages

Array Program

Arrays and list are very important part of programming

Uploaded by

zinkazame39
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

Insertion in an Array

#include<stdio.h>
int main()
{
int student[40],pos,i,size,value;
printf("enter no of elements in array of students:");
scanf("%d",&size);
printf("enter %d elements are:",size);
for(i=0;i<size;i++)
scanf("%d",&student[i]);
printf("enter the position where you want to insert the element:");
scanf("%d",&pos);
printf("enter the value into that position:");
scanf("%d",&value);
for(i=size-1;i>=pos-1;i--)
student[i+1]=student[i];
student[pos-1]= value;
printf("final array after inserting the value is");
for(i=0;i<=size;i++)
printf("%d",student[i]);
return 0;
}
Deletion in an Array
/* program to remove the specific elements from an array in C. */
#include <stdio.h>
#include <conio.h>
int main ()
{
// declaration of the int type variable
int arr[50];
int pos, i, num; // declare int type variable
printf (" \n Enter the number of elements in an array: \n ");
scanf (" %d", &num);
printf (" \n Enter %d elements in array: \n ", num);

// use for loop to insert elements one by one in array


for (i = 0; i < num; i++ )
{ printf (" arr[%d] = ", i);
scanf (" %d", &arr[i]);
}

// enter the position of the element to be deleted


printf( " Define the position of the array element where you want to delete: \n ");
scanf (" %d", &pos);

// check whether the deletion is possible or not


if (pos >= num+1)
{
printf (" \n Deletion is not possible in the array.");
}
else
{
// use for loop to delete the element and update the index
for (i = pos - 1; i < num -1; i++)
{
arr[i] = arr[i+1]; // assign arr[i+1] to arr[i]
}

printf (" \n The resultant array is: \n");

// display the final array


for (i = 0; i< num - 1; i++)
{
printf (" arr[%d] = ", i);
printf (" %d \n", arr[i]);
}
}
return 0;
}
Merging Two Arrays:
// C Program To Merge Two Arrays
#include <stdio.h>
int main()
{
int arr1size = 5, arr2size = 5, arr_resultsize, i, j;

// elements of first Array


int a[5] = { 1, 2, 3, 4, 5 };

// elements of Second Array


int b[5] = { 6, 7, 8, 9, 10 };

// resultant Array Size Declaration


arr_resultsize = arr1size + arr2size;
int c[arr_resultsize];

// copying array 1 elements into an array


for (i = 0; i < arr1size; i++) {
c[i] = a[i];
}

// copying array 2 elements into an array


for (i = 0, j = arr1size;
j < arr_resultsize && i < arr2size; i++, j++) {
c[j] = b[i];
}

// Array Elements After Merging


for (i = 0; i < arr_resultsize; i++) {
printf("%d ", c[i]);
}
return 0;
}
Searching in Array:
#include <stdio.h>
#include <conio.h>

int main()
{
int arr[10],i,n,ele;
printf("Enter array size: ");
scanf("%d", &n);
printf("Enter array elements: ");
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
printf("Enter element to search: ");
scanf("%d", &ele);

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


{
if(arr[i]==ele)
{
printf("%d found at position %d", ele, i+1);
return 0;
}

}
printf("Element not found");
}

You might also like