1. Array Shifting C++

Hello,
I'm having a bit of trouble shifting my arrays.
I need to move everything in an array to the right one. The last part of an array doesn't need to be carried over to the front... it can be chopped off.

A) {a, b, c, d, e, f}
B) I need to move b (and everything after that) to the right one-
So it looks like this {a, (blank), b, c, d, e} (notice f was chopped off)

Code:
```const int width = 800;
double points[width][5];

int ss1,se1,ss2,se2;

for(int xl=testL+2; xl<width-2; xl++)
{
ss1 = points[xl-1][1];se1 = points[xl-1][2];
ss2 = points[xl][1];se2 = points[xl][2];
points[xl] = ?}```
I'm kind of stuck here....

I am looking for example code,
Any help would be appreciated!

2. Think about how you would do it by hand, then write it as C++ statements (no loop), then try to find a pattern into those statements, and turn them into a loop.

This actually looks incredibly simple now...

So basically you do this:

(Sorry if there are errors, I wrote this in lua and converted it to C++)
Code:
```char temp = {'a','b','c','d','e','f'}
for(int i=sizeof(temp)/sizeof(char); i>1; i--)
{
temp[i] = temp[i-1]
}```
It's really this simple?

4. You have the right idea, but need to get the indices right and fix the syntax error. Note that sizeof(char) is always 1, so it is a little redundant here.

5. Almost.

BTW, it's usually not a good idea to use sizeof to determine array length, since it won't work for dynamically allocated arrays or arrays passed into functions (you will just get the size of a pointer).

The only way to keep array lengths is to... make another variable hold the length.

Or, you can use std::vector since this is C++ (may want to wait till you are comfortable with the very basics, though)

6. I wrote this in a haste, so it might not be correct...
Code:
```// Function shiftArray
// arg1 int *: pointer to the array. can be pointer to integer, char, whatever you want
// arg2 int  : last index to be pushed downwards
// arg3 int  : first index to be pushed downwards
//
// This function starts at the bottom, and assings the bottom value to the value 1 step
// further up.

void shiftArray(int *array, int stopShiftingIndex, int startShiftingIndex) {
for(int i=stopShiftingIndex; i > (startShiftingIndex-1); i--) {
array[i+1] = array[i];
}

// Decide what value to give to the first index that were moved down,. What should the empty "gap" be?
array[startShiftingIndex] = 0;
}```

7. Please don't write complete code for people. They don't learn much this way.

It's even worse if it's homework.

8. Thanks for all the help!

So would this be correct (I know it works, but is there anything I could do better?):

Code:
```const int length = 6;
char temp[length] = {'a','b','c','d','e','f'};
for(int i=length-1; i>=1; i--)
{
temp[i] = temp[i-1];
}
temp[1] = ' ';```
(This isn't homework, though I wish it was, because that would mean I was in a C++ class)

9. Does it actually work? doesn't look like what you described in the first post to me.

*edit*
nvm I see it does. You can save one iteration by making it
Code:
`for(int i=length-1; i > 1; i--)`
though, since temp[1] is going to be overwritten anyways.
*edit*

10. Well the actual code that I put into the program I'm working on looks like this:

Code:
```const int width = 800;
double points[width][5]; //Active; Start Pos; End Pos; Y Pos1; Y Pos2
double midpoint = width/2;

int vstart1, vend1, vstart2, vend2;
int complexity = 8;

int testL = 0;

int ss1,se1,ss2,se2;

...

for(int xl=width-1; xl>=testL+2; xl--)
{
points[xl][0] = points[xl-1][0];
points[xl][1] = points[xl-1][1];
points[xl][2] = points[xl-1][2];
points[xl][3] = points[xl-1][3];
points[xl][4] = points[xl-1][4];
}```
I've never taken a C++ class (or any other programming class for that matter) and I'm trying to create a 2D random terrain generator... Maybe I'm pushing my luck. But if it works I'm going to be very happy.

Edit: Ah, I didn't think about that (i > 1)... Cool.

11. If it's not homework, I'd just use the std::copy() algorithm and then chop off the last element, or better yet, use an std::vector...

12. Originally Posted by cyberfish
Please don't write complete code for people. They don't learn much this way.

It's even worse if it's homework.
I disagree. When you're at the basics of programming, seeing some complete working code can give you the ideas and understanding needed to make your own code.
I learned C++ almost entirely by first watching complete code, read it, understand it, put it away, and then write my own.

13. I disagree. When you're at the basics of programming, seeing some complete working code can give you the ideas and understanding needed to make your own code.
I learned C++ almost entirely by first watching complete code, read it, understand it, put it away, and then write my own.
Problem with that is, you won't know how to come up with the solution in the first place when you see a new problem, which is usually the hardest part. Not the implementation of an algorithm already given to you (whether in words or in code).

14. std::copy().... interesting. It's a bit hard for me to wrap my mind around that though.

15. std::copy() is pretty simple. Here's an example:
Code:
```#include <algorithm>
#include <iostream>

using namespace std;

int main()
{
int nums[5] = { 1, 2, 3, 4, 5 };

cout << "Original set:" << endl;
for ( int i = 0; i < 5; ++i )
{
cout << nums[i] << endl;
}

// Just give it the first and last elements you want to copy, as well as the location where to copy them to.
copy( &nums[1], &nums[5], &nums[0] );
nums[4] = 0;

cout << endl << "New set:" << endl;
for ( int i = 0; i < 5; ++i )
{
cout << nums[i] << endl;
}

return 0;
}```