Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
96 views

C Program To Insert An Element in An Array - Programming Simplified

This document provides a C program code example to insert an element into an array at a specified position. The code first takes user input for the number of elements, values of existing elements, position to insert, and value to insert. It then shifts existing elements from the insertion point down by one index to make space. Finally, it prints the resultant array with the inserted element at the specified position.

Uploaded by

Sachin Puri
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

C Program To Insert An Element in An Array - Programming Simplified

This document provides a C program code example to insert an element into an array at a specified position. The code first takes user input for the number of elements, values of existing elements, position to insert, and value to insert. It then shifts existing elements from the insertion point down by one index to make space. Finally, it prints the resultant array with the inserted element at the specified position.

Uploaded by

Sachin Puri
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

c program to insert an element in an array | Programming Simplified

http://www.programmingsimplified.com/c/source-code/c-program-inse...

Home C programming C programming examples c program to insert an element in an array

c program to insert an element in an array


This code will insert an element into an array, For example consider an array a[10] having three elements in it initially and a[0] = 1, a[1] = 2 and a[2] = 3 and you want to insert a number 45 at location 1 i.e. a[0] = 45, so we have to move elements one step below so after insertion a[1] = 1 which was a[0] initially, and a[2] = 2 and a[3] = 3. Array insertion does not mean increasing its size i.e array will not be containing 11 elements.

C programming code
#include <stdio.h> int main() { int array[100], position, c, n, value; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the location where you wish to insert an element\n"); scanf("%d", &position); printf("Enter the value to insert\n"); scanf("%d", &value); for (c = n - 1; c >= position - 1; c--) array[c+1] = array[c]; array[position-1] = value; printf("Resultant array is\n"); for (c = 0; c <= n; c++) printf("%d\n", array[c]); return 0; }

Array codes
Delete element from array Reverse array Linear search
C programming: C programming examples

1 of 1

12/24/2012 5:26 PM

You might also like