+ 2

Insert a number in an array(Insertion operation)

https://code.sololearn.com/cOTrGv2u1M28/?ref=app

12th Feb 2018, 1:43 PM
Mithilesh Jethwa
Mithilesh Jethwa - avatar
6 odpowiedzi
+ 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;
13th Feb 2018, 3:50 PM
John Wells
John Wells - avatar
+ 2
I understood it thank you for ur help sir.
13th Feb 2018, 4:08 PM
Mithilesh Jethwa
Mithilesh Jethwa - avatar
+ 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.
13th Feb 2018, 4:11 PM
John Wells
John Wells - avatar
+ 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.
12th Feb 2018, 2:12 PM
John Wells
John Wells - avatar
+ 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.
13th Feb 2018, 12:01 PM
Mithilesh Jethwa
Mithilesh Jethwa - avatar
+ 1
yes indeed it was a blunder once again thank you sir . U helped a lot thank you once again.
13th Feb 2018, 4:13 PM
Mithilesh Jethwa
Mithilesh Jethwa - avatar