+ 2
Insert a number in an array(Insertion operation)
6 Answers
+ 3
You could actually change your loop to fix it, too. I really wanted to point out that, before you put val in, the current element needed to move up one.
a[0]=11; a[1]=13; a[2]=?;
move a[1] to a[2]
store 12 into a[1]
a[0]=11; a[1]=12; a[2]=13;
+ 2
I understood it thank you for ur help sir.
+ 2
Line 16 could be changed to:
for(k=n-1;k>=pos-1;k--)
to avoid adding the extra move outside of the loop by doing one more pass.
+ 1
You forgot to ask a question or make any statement about why you posted this. As such, it is spam. Please edit your question to add, "why doesn't this work?" so it won't be deleted.
Add:
a[pos]=a[pos-1];
immediately after for loop, before storing val in a[pos-1] to fix your bug.
+ 1
Sorry sir I am not saying that ur solution is wrong but why we have to do that as I am giving position-1 directly to val (variable) and so it will directly decrease a position by 1 and give it to val and at last both things will be same . By the way thanks for your solution.
+ 1
yes indeed it was a blunder once again thank you sir . U helped a lot thank you once again.