Raised This Month: $12 Target: $400
 3% 

Rotating an Array?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Marcus101RR
Veteran Member
Join Date: Aug 2009
Location: Tampa, FL
Old 09-13-2016 , 12:38   Rotating an Array?
Reply With Quote #1

Is there a better way to do this?

Code:
int arrayNums[] = { 1, 2, 3};
int first = arrayNums[0];

for (int i = 0; i < 2; i++)
{
  arrayNums[i] = arrayNums[i+1]
}

arrayNums[2] = first
__________________

Last edited by Marcus101RR; 09-13-2016 at 12:42.
Marcus101RR is offline
Send a message via AIM to Marcus101RR Send a message via Skype™ to Marcus101RR
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 09-13-2016 , 13:23   Re: Rotating an Array?
Reply With Quote #2

What do you exactly mean by "rotate"? Show an (or a few) example input and output, don't explain with code.
klippy is offline
Grey83
Veteran Member
Join Date: Dec 2014
Location: Ukraine
Old 09-13-2016 , 14:56   Re: Rotating an Array?
Reply With Quote #3

somthing like that
PHP Code:
#define MAX_ARRAY_SIZE 3
int arrayNums[] = {123};

public 
void OnPluginStart()
{
    
RegConsoleCmd("sm_reverse"Cmd_Reverse);
}

public 
Action Cmd_Reverse(int clientint args)
{
    
char buffer[256];
    for (
int i 0MAX_ARRAY_SIZEi++)
    {
        
Format(buffersizeof(buffer), "%s, %i"bufferarrayNums[i]);
    }
    
ReplyToCommand(client"Input: %s"buffer);

    
int array2[MAX_ARRAY_SIZE];
    for (
int i 0MAX_ARRAY_SIZEi++)
    {
        
array2[i] = arrayNums[MAX_ARRAY_SIZE i];
    }
    
arrayNums array2;

    for (
int i 0MAX_ARRAY_SIZEi++)
    {
        
Format(buffersizeof(buffer), "%s, %i"bufferarrayNums[i]);
    }
    
ReplyToCommand(client"Result: %s"buffer);

    return 
Plugin_Handled;

__________________
Grey83 is offline
Marcus101RR
Veteran Member
Join Date: Aug 2009
Location: Tampa, FL
Old 09-13-2016 , 17:02   Re: Rotating an Array?
Reply With Quote #4

Quote:
Originally Posted by KliPPy View Post
What do you exactly mean by "rotate"? Show an (or a few) example input and output, don't explain with code.
Its a common "google" search actually and Rotate an Array is an acceptable definition, here is what it does in layman's terms.

It rotates the items in an array, so that the first item in the list "0" becomes the last, and all other items are moved down by one to the lesser index.
__________________
Marcus101RR is offline
Send a message via AIM to Marcus101RR Send a message via Skype™ to Marcus101RR
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 09-13-2016 , 21:20   Re: Rotating an Array?
Reply With Quote #5

PHP Code:
stock bool RotateArrayany[] iArrayint iSizeint iRotations 1bool bReverse false )
{
    if ( 
iSize <= )
        return 
false;

    
int iRotationCountiRotatedValueiElement;
    
int iLast iSize 1;

    if ( !
bReverse // Default rotates the first element to the last position
    
{
        for ( 
iRotationCount 0iRotationCount iRotationsiRotationCount++ )
        {
            
iRotatedValue iArray];

            for ( 
iElement 0iElement iLastiElement++ )
            {
                
iArrayiElement ] = iArrayiElement ];
            }

            
iArrayiLast ] = iRotatedValue;
        }
    }

    else 
// bReverse rotates the last element to the first position
    
{
        for ( 
iRotationCount 0iRotationCount iRotationsiRotationCount++ )
        {
            
iRotatedValue iArrayiLast ];

            for ( 
iElement iLastiElement 0iElement-- )
            {
                
iArrayiElement ] = iArrayiElement ];
            }

            
iArray] = iRotatedValue;
        }
    }

    return 
true;

Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
Mitchell
~lick~
Join Date: Mar 2010
Old 09-13-2016 , 21:57   Re: Rotating an Array?
Reply With Quote #6

I would have been lazy and just used an ArrayList
Code:
ArrayList alTemp = new ArrayList();
alTemp.PushArray(arrayNums, 3);
//Take the last value and put it in the front
alTemp.Push(alTemp.Get(alTemp.Length-1));
//Erase the last value since we moved it to the front.
alTemp.Erase(alTemp.Length-1);
And I actually have no idea if this would even work.
Edit: That's not how ArrayLists work. Push adds it to the end of the array.

Last edited by Mitchell; 09-13-2016 at 23:57.
Mitchell is offline
Sharpie_LaBeouf
Member
Join Date: Apr 2016
Old 09-14-2016 , 00:25   Re: Rotating an Array?
Reply With Quote #7

what do you want to flip the array so the top is at the bottom and the 2nd top is at the 2nd bottom, and so on?
Sharpie_LaBeouf is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 09-14-2016 , 00:33   Re: Rotating an Array?
Reply With Quote #8

Quote:
Originally Posted by Sharpie_LaBeouf View Post
what do you want to flip the array so the top is at the bottom and the 2nd top is at the 2nd bottom, and so on?
I think he just wants to shift it.
Mitchell is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 09-14-2016 , 01:40   Re: Rotating an Array?
Reply With Quote #9

untested ArrayList rotating function

PHP Code:
//assuming that we already have a populated ArrayList

//passing a positive value to the second parameter causes items at the END of the array to be moved to the front of the array
//passing a negative value causes items at the beginning to be placed at the end
stock void ArrayList_Rotate(ArrayList list, int amount) {
    if (list == 
null || amount == 0) return;
    
    if (
amount 0) {
        for (
int iamounti++) {
            list.
Push(0);
            list.
SwapAt(0, list.Length-1);
            list.
Erase(0);
        }
    }
    else { 
//amount is positive
        
for (int iamounti++) {
            list.
ShiftUp(0);
            list.
SwapAt(0, list.Length-1);
            list.
Erase(list.Length-1);
        }
    }

__________________

Last edited by ddhoward; 09-14-2016 at 01:44.
ddhoward is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 09-14-2016 , 02:12   Re: Rotating an Array?
Reply With Quote #10

Depending on the size of the array, you might want to consider something like this to avoid mutation of the array at all.

PHP Code:
native void printnum(int n);
native void print(const char[] str);

public 
void main()
{
  
int arrayNums[] = { 123};
  
int arrayStart 0;
  
  
#define CircleIndex(%1) arrayNums[(arrayStart + %1) % sizeof(arrayNums)]
  #define ShiftArray(%1) arrayStart = (arrayStart + %1) % sizeof(arrayNums)
  
  
print("Starting with\n");
  for(
int i=0i<sizeof(arrayNums); i++)
    
printnum(CircleIndex(i));
  
  for (
int x=0x<5x++)
  {
    print(
"After shifting\n");
    
ShiftArray(1);
    for(
int i=0i<sizeof(arrayNums); i++)
      
printnum(CircleIndex(i));
  }

Your array doesn't start at index 0 anymore, but at index arrayNumsStart. You wrap the (arrayNumsStart + i) index around when reaching the array size to start at the beginning again.

Quote:
Originally Posted by Output
Starting with
1
2
3
After shifting
2
3
1
After shifting
3
1
2
After shifting
1
2
3
After shifting
2
3
1
After shifting
3
1
2
Edit: Use this to allow negative shifting as well.
PHP Code:
#define ShiftArray(%1) arrayStart = (arrayStart + %1 + sizeof(arrayNums)) % sizeof(arrayNums) 
__________________

Last edited by Peace-Maker; 09-14-2016 at 02:16. Reason: Consider ShiftArray(-1) as well
Peace-Maker is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 16:43.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode