Array Shifting C++

This is a discussion on Array Shifting C++ within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm having a bit of trouble shifting my arrays. I need to move everything in an array to the ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    45

    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. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    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.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    45

    (Reply)

    Thanks for the reply!
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    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. #6
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    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;
    }
    Last edited by Drogin; 04-11-2009 at 12:29 PM.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Please don't write complete code for people. They don't learn much this way.

    It's even worse if it's homework.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    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. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    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*
    Last edited by cyberfish; 04-11-2009 at 12:43 PM.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    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. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #12
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Quote Originally Posted by cyberfish View Post
    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. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    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. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    std::copy().... interesting. It's a bit hard for me to wrap my mind around that though.

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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;
    }
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Replies: 4
    Last Post: 05-13-2003, 04:54 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21