R4R
Right Place For Right Person TM
 
R4R CC TopicsC Data StructureInsert values in an Array
Previous

Home

Next

How can we insert values in an Array?

The values in an array can be inserted in the following two manner :

  1. Insertion at the end of the array.
  2. Insertion at the specified location in the array.

Insertion at the end of the Array can be carried out very easily. As value has to be inserted at the end so we just need to traverse the whole array till the end and then inserted our desired value at the end by setting the value pointed by the last element of the array to the new node which we desired to insert.

A program to insert value at a specified location in  the array :

To insert a value in an array  at a desired position firstly we will have to shift all the elements forward so as to accommodate the new element, then we  place the element at the required place.

#include <stdio.h>
#include<conio.h>
int i, len, num, num_pos; /* declared some global variables */
void main()
{
int arr[50];
clrscr();
add_element(arr, len, num, num_pos); /* calling the function to add an element in the array */
getch();
}
void add_element(int a[], len, num, num_pos)
{
printf("Enter the length of the array :");
scanf("\n%d", &len);
printf("\nEnter the array elements :");
scanf("\n%d",&a[i]);
printf("\nEnter the number you want to insert :");scanf("%d",&num);
printf("\nEnter the position of the number at you want to insert :");
scanf("%d", &num_pos);
--num_pos;
if(num_pos>len)/*checking whether insertion outside array*/ 
{
    printf("insertion outside the array");
}
else
{
     for(i=len; i>=num_pos; i--)
{
     a[i]=a[i+1];/* shifting all the elements backwards to create a space for new element */
}
a[num_pos]=num; len=len+1;
printf("\n After insertiion now the array is :");
for(i=0; i<len; i++)
{
     printf("%d", a[i]);/* printing the newly formed array */
}
}
}

Output : The output of the above program would something like this

Enter the length of the array :

5

Enter the array elements :

1

2

3

4

5

Enter the number you want to insert :

7

Enter the position of the number at you want to insert :

3

After insertion now the array is :

1

2

7

3

4

5
 

Previous

Home

Next

R4R CC TopicsC Data StructureInsert values in an Array
Tolal:0 Click:
Show All Comments

Post Your Comments

Your Name:

Your Email ID :
Comments :
URL
  =* Enter SUM

New Updates

10:New Updates
Tutorials,examples and Interview Questions with answers
Posted By: Shashi     Posted Date:02.10.14

9:C# Tutorials,C# examples and C# Interview Questions with answers
R4R provide C# Tutorials,C# examples and C# Interview Questions with answers. Through R4R you can develop a small application and small programs.We cover C# Tutorials,C# examples and C# Interview Questions and answers for freshers as well experienced programmer.
Posted By: Shashi     Posted Date:11.17.13

8:Java/J2EE(Servlet,JSP,JNDI,Struts,Spring,Hibernate,EJB,JSF,JMS,Web-Services)
In this section R4R covers Java/J2EE(Servlet,JSP, JNDI, Struts, Spring, Hibernate, EJB,JSF,JMS,Web-Services) Tutorials with Examples.R4R provide Java/J2EE (Servlet,JSP, JNDI,Struts, Spring, Hibernate, EJB, JSF, JMS, Web-Services) Interview Questions with answers study materials for freshers and experienced.
Posted By: Shashi     Posted Date:11.13.13

7:CoreJava Tutorials , CoreJava Examples and CoreJava Interview Questions with answers
In this section R4R covers CoreJava Tutorials with CoreJava Examples. R4R covers CoreJava programming concept in easy way. R4R provide CoreJava Interview Questions with answers study materials for freshers and experienced.
Posted By: Shashi     Posted Date:11.04.13

6:Microsoft.net Technology ASP.NET,c# ,ADO.NET,WCF,WPF,Silverlight ,VB.net
R4R provides Microsoft Technologies(like ASP.NET,c# ,ADO.NET,WCF,WPF, Silverlight and VB.ne)Tutorials with Examples,programming concept and Interview Questions with answers study materials for freshers and experienced.
Posted By: Shashi     Posted Date:11.04.13

5:J2me Tutorials concept with Examples with Netbean IDE and J2ME Application Examples.
R4R cover basic J2me Tutorials concept with Examples and provide a way to develop J2me programming concept in easy way. R4R provide J2me Interview Questions with answers.R4R provide J2me Languages study materials with examples uing Netbean IDE and J2ME Application Examples.
Posted By: Shashi     Posted Date:10.30.13

4:J2me Tutorials concept with Examples ith netbean IDE and J2ME Application Examples.
R4R cover basic J2me Tutorials concept with Examples and provide a way to develop J2me programming concept in easy way. R4R provide J2me Interview Questions with answers.R4R provide J2me Languages study materials with examples uing Netbean IDE and J2ME Application Examples.
Posted By: Shashi     Posted Date:10.30.13

3:Java ebooks , Java tutorials , Java examples , Java interview questions and answers
R4R provides study materials for Java/j2EE technologies. R4R covers core java, advanced java, servlet, JSP, Struts, Spring, Hibernate, EJB, JSF, J2ME, ANT, JUnit and Some APIS .R4R also provides ebooks,tutorials,examples, interview questions and answers.
Posted By: Shashi     Posted Date:10.30.13

2:Learn basic C++ Tutorials with Examples , C++ Interview Questions with answers
R4R is a free E-learning website.You can learn basic C++ Tutorials with Examples , C++ Interview Questions with answers and also some hot topics.
Posted By: Shashi     Posted Date:10.30.13

1:C Tutorials with Example C subjective and objective interview questions and answers
R4R provide C Tutorials with Example and also provides C subjective and objective interview questions and answers.You can learn C in easy way.We cover basics of C here and give some examples.
Posted By: Shashi     Posted Date:10.30.13

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R