0% found this document useful (0 votes)
5 views2 pages

Array Manipulation in C Programming

The document contains C code snippets for various array operations including inserting an element at the end of an array, inserting an element at a specific position, deleting an element from a specific position, deleting the last element, and merging two arrays. Each code snippet includes user input for the number of elements and the elements themselves, followed by the respective operation. The document provides a practical guide for manipulating arrays in C programming.

Uploaded by

kavanronald
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Array Manipulation in C Programming

The document contains C code snippets for various array operations including inserting an element at the end of an array, inserting an element at a specific position, deleting an element from a specific position, deleting the last element, and merging two arrays. Each code snippet includes user input for the number of elements and the elements themselves, followed by the respective operation. The document provides a practical guide for manipulating arrays in C programming.

Uploaded by

kavanronald
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

insert an element at the end of the array

#include<stdio.h>
void main()
{
int i,n,num,A[10];int insertposi;
printf("enter the number of elements n ");
scanf("%d",&n);
printf("enter the elements ony by one");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
printf("number to be inserted\n");
scanf("%d",&num);

insertposi=n;
A[insertposi]=num;
printf("after inserting an element at the end of the array \n");
for(i=0; i<=n; i++)
printf("%d\t%d\n",i,A[i]);
}

insert an element at particular position of an array


#include<stdio.h>
void main()
{
int i,n,num,pos,A[10];
printf("enter the number of elements n ");
scanf("%d",&n);
printf("enter the elements ony by one");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
printf("number to be inserted\n");
scanf("%d",&num);
printf("position which the element to be inserted\n");
scanf("%d",&pos);
for(i=n-1; i>=pos; i--)
A[i+1]=A[i];
A[pos]=num;
n=n+1;
printf("after inserting an element the array is\n");
for(i=0; i<n; i++)
printf("%d\t%d\n",i,A[i]);
}

Delete an element from the particular position of an array


#include<stdio.h>
void main()
{
int i,n,num, pos,A[10];
printf("enter the number of elements n ");
scanf("%d",&n);
printf("enter the elements ony by one\n");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
printf("in which positon the element to be deleted\n");
scanf("%d",&pos);
for(i=pos; i<n; i++)
A[i]=A[i+1];
n=n-1;
printf("after deleting an element from the particular positin the
array is\n");
for(i=0; i<n; i++)
printf("%d\t%d\n",i,A[i]);
}

Delete an element in last position of an array


/*#include<stdio.h>
void main()
{
int i,n,num, pos,A[10];
printf("enter the number of elements n ");
scanf("%d",&n);
printf("enter the elements ony by one");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
n=n-1;
printf("after deleting an element at the end of the array \n");
for(i=0; i<n; i++)
printf("%d\t%d\n",i,A[i]);
}

Merging of Two array


#include<stdio.h>
void main()
{
int i, j,k,temp, n1,n2, ArrayA[20], ArrayB[20], ArrayC[40];
printf("Enter the value of N1 for ArrayA\n");
scanf("%d", &n1);
printf("Enter the array elements of ArrayA \n");
for (i = 0; i < n1; i++)
scanf("%d", &ArrayA[i]);
printf("Enter the value of N2 for ArrayB\n");
scanf("%d", &n2);
printf("Enter the array elements of ArrayB \n");
for (i = 0; i < n2; i++)
scanf("%d", &ArrayB[i]);
for(i=0;i<n1;i++) // assign ArrayA elements to ArrayC
ArrayC[i]=ArrayA[i];
k=n1;
for(i=0;i<n2;i++) // assign ArrayB elements to ArrayC
{
ArrayC[k]=ArrayB[i];
k++;
}
printf(" the array elements of ArrayC is \n");
for (i = 0; i < (n1+n2); i++)
printf("%d\t",ArrayC[i]);
}*/

You might also like