I have to write code to shift all the values in the array to the right of position P, one place to the left. I can make up my own array and position P. would this code work?

for (j=6; j>1;j--)
{
x[j-1]=x[j];
}

Recommended Answers

All 3 Replies

Although inefficient I will suggest you a way.

void rot(int *a, int sz)
{
	int * t = new int[sz];

	t[0] = a[sz-1];

	for(int i = 1; i < sz; i++)
		t[i] = a[i-1];

	for(int i = 0; i < sz; i++)
		a[i] = t[i];

	delete [] t;
}

I am only in programming 2 for C++ i have no idea what all that means...so can you make it more simple...?

hmm ok lets see.

void rot(int a[], int sz)
{
   const int MY_MAX = 100; // assumes the array passed size is < 100
int  temp[MY_MAX];

	temp[0] = a[sz-1]; //copy last element from the array thats passed to the first element

	for(int i = 1; i < sz; i++ )  //now copy every element except the last 1 
		temp[i] = a[i-1];

	for(int i = 0; i < sz; i++) //now temp contains the rotated array so copy it back to the array
		a[i] = temp[i];
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.