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;
}
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.
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.
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.
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