Thread: snippet of for loop code problem

1. snippet of for loop code problem

hey, I have part of a program that has an array containing 100 items. In a secion of the array I need to shift everything up 1 spot and then inster a number at the intial shift spot, let me give example:

Code:
```int data [100];
size_t I;```
I need to shift data[50]......data[98] up one spot to locations
data[51]....data[99]. After the shift has occured I then want to insert the number 42 in location data[50]

I know this is a simple for loop for most, will someone show me and explain how the computer will read the for loop so I can use something similar in my program

2. Use a for loop, and count backwards, from 98 to 50. Have another variables that starts at 99 and counts down to 51. Assign the value at the first number to the location of the second, decrement your variables, and move on. When the for loop has finished executing, just assign 42 to data[50].

Don't forget that the first element of a 50 element array is array[0] and the last is array[49].

3. ok let me think that out and write it down and ill come back with what I have and hopefully it will be right, thanks sean

4. Code:
```for(i=98;i>=50;i--)
for(j=99;i>=51;i--)```
would that be the loops i use? I am not sure how to link them together so to speak, how do i assign everything that was in i, now to j?

5. whoops, i meant for j to be second for loop not i :

Code:
```for(i=98;i>=50;i--)
for(j=99;j>=51;j--)```
would I then just simply set i = j ?

6. Code:
```for(int i = 98, j = 99; i >= 50, j >=51; i--, j--)
data[j] = data [i];```
edit: if your loop executes just one line of code, you don't need the curly braces {}. Ass long as you keep assignment, comparisons, and increments separated by a semicolon ; you can work on multiple variables by separating them with a comma, as I have done. Remember that the problem will change quite a bit if you're using pointers, which may be necessary depending on where you place this code in your program.

7. Random Q: If you try to decrement a size_t integer below zero.. will the size_t variable just contain a zero..??

8. No, double for loop will be wrong because you need both i and j to decrease at the same time, whereas like this j will go down to 51, then i decreases 1, then j goes down to 51 again, etc.

Heres how I'd do it in pseudocode (target is the index you want to stop at):
Code:
```place value you want to place at target into array[current size+1]
for (i=new array size; i>target; i--)
swap array[i] with array[i-1]```
This will work as long as you don't make the array bigger than you gave it space for.

9. Code:
```for(i=98;i>=50;i--)
for(j=99;j>=51;j--)```
FYI- That's a nested loop. Very handy, but not for this problem. You do NOT need two indexes (i & j).
All you need is i and i+1. (Or i and i-1, depending on how you write your program.

This is NOT a program fragment... just some random C++ statements for you to think about!
Code:
```data[99] = data[98];  // Copy value from data[98] to data[99]
// Both now have the same value

data[98] = data[97];  // Copy value from data[97] to data[98]
data[97+1] = data[97];   // Same as above

data[i+1] = data[i];    // Copy value from data[i] to data[i+1]```

When your loop is done shifting, data[50] and data[51] will have the same value. Then, you can overwrite data[50] with the new value.

Popular pages Recent additions