Moving elements in my array

im trying to write a program that will shift the values in my array one space to the left. im not sure if im going down the right path though. any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void shiftright (int myarray[], int size)
{
  for (int i=0; i<size; i++)
  {
    myarray[i] ;
  }
}



main (void)
{
int myarray [5]= {1, 2, 3, 4, 5};
  shiftright ( myarray, 5);

	for ( int i=0; i<5; i++) 
    {
	cout << myarray [i];
    }

}



the if the values i enter are {1,3,5,7,9} it needs to output {9,1,3,5,7}
Last edited on
Typeless main :(

For shifting, I don't think there is an in place way to do it, so you would have to make a copy of the array with each element moved over once.
It's more a rotate than shift. You need to save the last value when you start (9 in this case), shuffle all the elements into the next slot up starting with the second to last. Then set the saved value into position zero.

Your main's logic is correct, but syntactically incorrect. main should always return an int.
For one thing, your shiftright() function isn't even doing anything they way you have it defined. It's just iterating through the array. Also, why did you say you wanted to shift everything to the left, but you called your function shiftright() ??

You didn't say what you wanted to do with the last value, so like kdw, I'm assuming you want to throw the value of the first element onto the end.

Try something like this..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void shiftright (int myarray[], int size);

int main (void)
{
int myarray []= {1, 2, 3, 4, 5};

shiftright( myarray, 5);

for ( int i=0; i<5; i++) 
{
	cout << myarray[i] << ' ';
}

return(0);

}

void shiftright (int myarray[], int size)
{
  
int temp = myarray[0];

for (int i=0; i<(size - 1); i++)
{
    myarray[i] = myarray[i+1] ;
}
myarray[size - 1] = temp;

}


Last edited on
If I were you I'd go above and beyond and make the function more generic. Write it so that the user can specify how many places to rotate or follow the example of std::rotate where you specify where you want a particular position moved to.
Have a look at memmove(p1,p2,n), which copies a block of memory from p2 to p1. Where n is the number of elements you want to be affected
memmove is ok provided that the function only operates on arrays of POD types.
@jsmith....could you explain POD please.
Try with the following code, it will give you the correct result what you expected.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void shiftright (int myarray[], int size);

int main (void)
{
int myarray []= {1, 3, 5, 7, 9};

shiftright( myarray, 5);

for ( int i=0; i<5; i++) 
{
	cout << myarray[i] << ' ';
}

return(0);

}

void shiftright (int myarray[], int size)
{
  
int temp;
int temp1;

for (int i=0; i<(size -1); i++)
{
	temp = 	myarray[size-1];
	myarray[size-1] = myarray[i];
	myarray[i] = temp;
}


}
Most complete code! I hope it helps...

#include<iostream.h>
#include<conio.h>
void shiftright(int myarray[],int size);

int main(void)
{
clrscr();
int myarray[] = {1, 3, 5, 7, 9};
shiftright(myarray,5);
for(int i=0;i<5;i++)
{
cout<<myarray[i]<<' ';
}
getch();
return(0);
}

void shiftright(int myarray[],int size)
{
int temp;
int temp1;
for (int i=0; i<(size -1); i++)
{
temp = myarray[size-1];
myarray[size-1] = myarray[i];
myarray[i] = temp;
}
}

Topic archived. No new replies allowed.