Hello! i got this code. However i'm really not familiar with memcpy function. Can anybody can show me the other way to write that code without using memory function?

void rightRotate (int k, int n)
{
char    temp [2*MAX], *saveptr;
saveptr = num+k; //
cout << "k" << k << "\n";
memcpy (temp , saveptr, n);
memcpy (temp + n , saveptr, n); //
memcpy (saveptr, temp+n - 1, n); //
}

i'm familiar with this code:

void rightRotate(int *y, int n)
{
int temp ;
for (int i = 1; i<= n, i++)
{
temp = y[i];
y[i] = y[n-i+1];
y[n-i+1] = temp;
}
}

is it similar?

2
Contributors
1
2
Views
9 Years
Discussion Span
Last Post by StuXYZ

void rightRotate(int *y, int n)
{
int temp ;
for (int i = 1; i<= n, i++)
{
temp = y[i];
y[i] = y[n-i+1];
y[n-i+1] = temp;
}
}

I am 100% certain you have absolutely no idea what this does.

First is does not compile, you need a ; after the i<=n not a comma.

The loop runs from 1->n BUT arrays are arranged 0 to n-1 in c++/c.
Yes you can call it like this

int x[10];
rightRotate(x-1, 10);

Finally the function does absolutely nothing. Since you go past the mid-point and undo your swap!!

So TEST YOUR CODE. Then you will begin to understand. That means figure out what you code SHOULD do and write a test and compare.

So to answer your question the best way to write the code is as follows: ;)

void rightRotate(int*,int)
{
}

I would suggest that you test the second code fragment. Get it to do something and then let us know what you were trying to achieve.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.