inserting into an array

Jun 21, 2012 at 9:11pm
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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 Jun 21, 2012 at 9:12pm
Jun 21, 2012 at 10:36pm
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.

you can just add your number to an array place

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.



Jun 21, 2012 at 11:57pm
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.
Jun 22, 2012 at 12:09am
@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.
Jun 22, 2012 at 12:15am
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?
Jun 22, 2012 at 12:20am
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.
Jun 22, 2012 at 12:22am
ouch got that one... at this point I was kind of just throwing stuff at my screen hoping something stuck. thanks
Jun 22, 2012 at 12:26am
That's usually the best time to turn off the computer and pick up a pencil and sheet of paper.
Jun 22, 2012 at 12:40am
1
2
3
4
5
6
7
8
9
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 Jun 22, 2012 at 12:41am
Jun 22, 2012 at 2:51am
yup exactly what I thought I was WAY over thinking it. thank you all for your help!
Jun 22, 2012 at 8:27am
wouldn't a stack or queue suite your purposes better? or is this an assignment?
Jun 23, 2012 at 12:03am
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.