posted 7 years ago
I have added a print statement in your loop.
This is the result I got:
Enter no. of Array elements
10
ENter elements :
1
2
3
4
5
6
7
8
9
10
Enter position of insertion
0
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0]
[1, 1, 3, 4, 5, 6, 7, 8, 9, 10, 0]
[1, 1, 1, 4, 5, 6, 7, 8, 9, 10, 0]
[1, 1, 1, 1, 5, 6, 7, 8, 9, 10, 0]
[1, 1, 1, 1, 1, 6, 7, 8, 9, 10, 0]
[1, 1, 1, 1, 1, 1, 7, 8, 9, 10, 0]
[1, 1, 1, 1, 1, 1, 1, 8, 9, 10, 0]
[1, 1, 1, 1, 1, 1, 1, 1, 9, 10, 0]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 10, 0]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]
Enter element to be inserted
999
Array elements after insertion
[999, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Can you see what is happening?
This value 1 was copied from index:
0 to 1
1 to 2
2 to 3
3 to 4 and so on...
Also, you never set a value on index n.