Insertion using functions in C++

December 25, 2008 · Print This Article · Email It

This program inserts a given number into an integer array. The location is also taken from the user.
#include<iostream.h>
void insert_array(int, int, int,int []);
void main()
{
	int a[20],x,loc,n;
	cout<<"Enter the number of elements";
	cin>>n;
	for(int i = 0;i<n;i++)
		cin>>a[i];

	cout<<"Enter the number and location";
	cin>>x>>loc;
	insert_array(x,loc,n,a);
}
void insert_array(int x,int loc,int n,int a[])
{
	int b;
	loc--;
	if(n<19)
	{
		b=n;
		while(b<loc)
		{
			a[b]=a[b-1];
			b--;
		}
		a[loc]=x;
		for (int i=0;i<n;i++)
			cout<<a[i];
	}
}
blog comments powered by Disqus