Insertion into 2d array

I want to create a program to insert an element into a 2d array. User will wnter the array then element to be inserted its both row and column position.
We have to shift elements of the array after position given and discard the last element.
U can't convert it into 1d array.
Size of array is flexible
Example;
suppose size of array is 3,3
then
array input
0 1 2
3 4 6
7 8 9

element to be inserted is 5 at 2,3

array after insertion
0 1 2
3 4 5
6 7 8

I think the shifting will be done using pointers. I made a program but i got stuck. Can anyone help me??

Here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream.h>
#include<conio.h>
main()
{
int ele,p1,p2,a[20][20],i,j,n;
cout<<"\nEnter size of array:";
cin>>n;
cout<<"Enter array:";
for(i=0;i<n;i++)
{
                for(j=0;j<n;j++)
                cin>>a[i][j];
}
cout<<"Array:\n";
for(i=0;i<n;i++)
{
                for(j=0;j<n;j++)
                cout<<a[i][j]<<"\t";
                cout<<"\n";
}
cout<<"Enter element to be inserted :";
cin>>ele;
cout<<"Enter postion(Row,column):";
cin>>p1>>p2;
p1--;
p2--;
int *p=&a[n][n];
for(i=n*n;i>p1*p2;i++)
{
                                *p=*p-1;
                                p--;                
}
a[p1][p2]=ele;
for(i=0;i<n;i++)
{
                for(j=0;j<n;j++)
                cout<<a[i][j]<<"\t";
                cout<<"\n";
}
getch();
}
You already failed the first part of the assignment "size of array is flexible". Try working on that first.
Topic archived. No new replies allowed.