### inserting into an array

ok so my dilemma is that Im writing a function that inserts a number into an integer array that is supposed to bump the existing numbers after it up in position.

such as, the array looks like this, 00480 and I want to put a 1 in the 0 reference so it prints out like this 10048.

I have written myself more than one algorithm trying to sort it out and at best I only rewrite the values so that they look like this 10480 which I can simply overwrite the existing data in array. I suppose it would be somewhat adding a value in a sorted array.

the code I have so far far in the function looks like this.

with the passed down values being the Offset they want to insert it at, the number I'm inserting, the array, and the array size.

I am sure I am just way over thinking this and it is way easier than I am making it.

 ``12345678910111213141516171819`` ``````void arraySwap(int offNum, int theNum, int numArray[], int size){ int prka = 0; int prkb; for(int i = offNum+1; i < (size - offNum); i++){ prka = numArray[i]; prkb = numArray[i+1]; numArray[i] = numArray[i-1]; numArray[i] = prka; } numArray[offNum] = theNum; }``````
Last edited on
HI Ruffneck

I am not sure if I fully understand what you would like to do but it seems like you only want to overwrite one place in the array. You don't need two arrays to do this.

secondly unless your numArray is global you won't be able to see your changes outside the arraySwap function.
if it is a global then you don't need to put it in the function anyway.

yes the array is global, what I am trying to do is shuffle the values to the left as I add one to the right if that makes sense? I wish I was just adding it! I wish it was easier to explain.

picture my array printed out was 12456 and I want to put the 3 in its proper place and the 6 can drop off so the size stays the same, does that make more sense? sorry if I'm being difficult.
@jorz: arrays are passed by reference, so the actual array will be changed.

I think I have a better way. Start from the end of the array and keep pushing the values back until you get to the offNum (or whatever math is needed).

At the end of that loop, you should be right where you want to put theNum.
 `for(int i = offNum+1; i < (size - offNum); i++)`

Look at the end condition of your loop. If size = 10 and offNum = 6, what does (size - offNum) equal?
In your loop, you're basically not doing anything. I'm not sure what prkb is supposed to be used for, but look at prka and numArray[i].

For example, let's say that your array is {0, 1, 2, 3, 4} and that i = 2 right now. What will your loop do?

prka = 2
prkb = 3
numArray[i] = 1
numArray[i] = prka

You end up with numArray[i] being back at 2!

Also, there's something wrong with how you set up the loop, but I don't know how you're going to change the code inside, so I don't know how you should fix it.
ouch got that one... at this point I was kind of just throwing stuff at my screen hoping something stuck. thanks
That's usually the best time to turn off the computer and pick up a pencil and sheet of paper.
 ``123456789`` ``````void arraySwap(int offNum, int theNum, int numArray[], int size) { while (size > offNum) { numArray[size] = numArray[size - 1]; size--; } numArray[offNum] = theNum; }``````

Last edited on
yup exactly what I thought I was WAY over thinking it. thank you all for your help!
wouldn't a stack or queue suite your purposes better? or is this an assignment?
yeah it was how they wanted it in an assignment... I had just stared at it for too many hours and my brain had shut off yesterday afternoon sometime! Again thanks for your help folks
Topic archived. No new replies allowed.