0% found this document useful (0 votes)
7 views6 pages

Inserting Element in the Array

Uploaded by

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

Inserting Element in the Array

Uploaded by

priyanka singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Inserting Element in the Array

// C Program to Insert an element

// at a specific position in an Array

#include <stdio.h>

int main()

int arr[100] = { 0 };

int i, x, pos, n = 10;

// initial array of size 10

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

arr[i] = i + 1;

// print the original array

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

printf("%d ", arr[i]);

printf("\n");

// element to be inserted

x = 50;

// position at which element

// is to be inserted

pos = 5;

// increase the size by 1

n++;

// shift elements forward

for (i = n - 1; i >= pos; i--)


arr[i] = arr[i - 1];

// insert x at pos

arr[pos - 1] = x;

// print the updated array

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

printf("%d ", arr[i]);

printf("\n");

return 0;

}
DELETING AN ELEMENT IN AN ARRAY
/*
* C program to delete an element from array at specified position
*/

#include <stdio.h>
#define MAX_SIZE 100

int main()
{
int arr[MAX_SIZE];
int i, size, pos;

/* Input size and element in array */


printf("Enter size of the array : ");
scanf("%d", &size);
printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

/* Input element position to delete */


printf("Enter the element position to delete : ");
scanf("%d", &pos);

/* Invalid delete position */


if(pos < 0 || pos > size)
{
printf("Invalid position! Please enter position between 1 to %d", size);
}
else
{
/* Copy next element value to current element */
for(i=pos-1; i<size-1; i++)
{
arr[i] = arr[i + 1];
}

/* Decrement array size by 1 */


size--;

/* Print array after deletion */


printf("\nElements of array after delete are : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}
}

return 0;
}
SORTING IN ARRAY
#include <stdio.h>

void bubbleSort(int arr[], int size) {

int step = 0;

while(step < size - 1) {

int i = 0;

while(i < size - step - 1) {

if (arr[i] > arr[i + 1]) {

int temp = arr[i];

arr[i] = arr[i + 1];

arr[i + 1] = temp;

++i;

++step;

void printArray(int arr[], int size) {

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

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[10000];

int n;

printf("Enter size of the array: ");

scanf("%d", &n);

printf("Enter the array elements: ");


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

scanf("%d", &arr[i]);

bubbleSort(arr, n);

printf("After sorting, the array in ascending order is: ");

printArray(arr, n);

OUTPUT-

Enter size of the array: 4


Enter the array elements: 23 12 65 45
After sorting, the array in ascending order is: 12 23 45 65
BINARY SEARCHING IN ARRAY-

You might also like