Rotating array

Hey I was trying to rotate the elements in an array to the right by using this function

int rotateRight(string a[], int n, int pos);
Eliminate the item at position pos by copying all elements before it one place to the right. Put the item that was thus eliminated into the first position of the array. Return the original position of the item that was moved to the beginning. Here's an example:

string humans[5] = { "padme", "aladdin", "palpatine", "pocahontas", "quigon" };
int p = rotateRight(humans, 5, 2); // returns 2
// humans now contains: "palpatine", "padme", "aladdin", "pocahontas", "quigon"

And i have at the moment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int rotateRight(string a[], int n, int pos)
{
	for(int k = 0; k<n;k++)
	{
		if (k == pos)
		{
			string pos_string = a[pos];
			int j = k-1;
			while(j>=0)
			{
				a[k]=a[j];
				k--;
				j--;
			}
			a[0]=pos_string;
		}
	}
	cerr <<a[0] << endl;
	cerr <<a[1] << endl;
	cerr << a[2] << endl;
	cerr << a[3] << endl;
	cerr << a[4] << endl;
	return pos;
}

I feel like it should work but when i call it nothing is printed on my screen, anyone know what it wrong?
Line 3 - incrementing k.
Line 12 - decrementing k.
Hence your for loop is looping forever, from the time k and pos are equal.

Remove the k-- and check from there.
Topic archived. No new replies allowed.