0% found this document useful (0 votes)
18 views

4.3 Inserting Element in Array

Uploaded by

buvanesh sk
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)
18 views

4.3 Inserting Element in Array

Uploaded by

buvanesh sk
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
You are on page 1/ 1

New Chat Default (GPT-3.

5)

Today
Insert an element in a array in c with program and physudo
Clean Ink Stain Pants

Yesterday

Certainly! Here's a C program that inserts an element into an array at a specified position
Uses for Hard Corn
using a function. I'll also provide a pseudo code for better understanding:
Previous 7 Days

C Program:
Developing an E-Commerce Si

c Copy code
Previous 30 Days

The Last Ship Clarification #include <stdio.h>

Kill ants with water. // Function to insert an element at a specified position


void insertElement(int arr[], int size, int position, int element) {
Dry Cough Home Remedies
if (position < 0 || position >= size) {
printf("Invalid position\n");
August
return;
Eye Itching Remedies }

Fiber for Regular BMs for (int i = size - 1; i > position; i--) {
arr[i] = arr[i - 1];
Food Staying on Tongue
}

Class Object Addition Impleme


arr[position] = element;

July size++;
}
New chat

int main() {
Share Bot & Channel
int arr[100], size, position, element;

June
printf("Enter the size of the array: ");
Unary Operators in CPP scanf("%d", &size);

While vs Do-While in C++ printf("Enter the elements of the array:\n");


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

printf("Enter the position where you want to insert the element: ");
scanf("%d", &position);

printf("Enter the element to insert: ");


scanf("%d", &element);

insertElement(arr, size, position, element);

printf("Array after insertion:\n");


for (int i = 0; i < size + 1; i++) {
printf("%d ", arr[i]);
}

return 0;
}

Pseudo Code:

arduino Copy code

function insertElement(arr, size, position, element)


if position < 0 or position >= size
print "Invalid position"
return
end if

for i from size - 1 down to position + 1


arr[i] = arr[i - 1]
end for

arr[position] = element
size = size + 1
end function

main()
declare arr[100], size, position, element

print "Enter the size of the array: "


input size

print "Enter the elements of the array:"


for i from 0 to size - 1
input arr[i]
end for

print "Enter the position where you want to insert the element: "
input position

print "Enter the element to insert: "


input element

call insertElement(arr, size, position, element)

print "Array after insertion:"


for i from 0 to size
print arr[i]
end for
end main

This program reads the size of the array, its elements, the position where you want to insert
the element, and the element itself. It then inserts the element at the specified position and
displays the updated array.

Upgrade to Plus NEW


Send a message
buvanesh sk Free Research Preview. ChatGPT may produce inaccurate information about people, places, or facts. ChatGPT August 3 Version ?

You might also like