Shifting Array in C: Solving Last Element Problem

  • Thread starter Thread starter James889
  • Start date Start date
  • Tags Tags
    Array
AI Thread Summary
The discussion focuses on writing a function in C to shift an array by a specified number of steps, particularly addressing how to handle the last element during the shift. It highlights the importance of iterating in the opposite direction when moving elements within the same array to avoid overwriting data. The use of the modulus operator is suggested for determining new positions of elements after shifting. While swapping elements is a valid approach, it is noted that this method results in more memory operations compared to using a move function like memmove, which can handle overlapping memory correctly. The conversation emphasizes understanding both methods for a comprehensive solution to the problem.
James889
Messages
190
Reaction score
1
Hi,

i would like some help with this task.

My task is to write a function that shifts an array arbitrary number of steps.

So for example the array { 0,1,2,3,4,5,6,7,8,9 }

would look like { 9,0,1,2,3,4,5,6,7,8 } after the function was called with the steps arguments set to 1.

The actual shifting isn't a problem, but rather how i deal with the last element in the list.

For example if i shift all elements using a for loop
Code:
int temp;
for(int i=0; i<length_of_list; i++){
temp = array[i+1];
array[i+1] = array[i];
array[i] = temp;
}

On the last run i am left with temp, containing the last element in the list. How do i put it first?
 
Physics news on Phys.org
The array in your example has indexes that range form 0 through 9. If you were to shift all elements, say, three places higher in the array, where would each array element go? In particular, where would arr[7], arr[8], and arr[9] go? Can you think of an operator that might be helpful to you?
 
You may want to see if the modulus operator % can help you.

Also, when moving stuff within the same array you have to be careful to iterate the "correct way" - as a rule of thumb you have to iterate the "opposite" way of the move. Your code above doesn't.
 
Filip Larsen said:
Also, when moving stuff within the same array you have to be careful to iterate the "correct way" - as a rule of thumb you have to iterate the "opposite" way of the move. Your code above doesn't.

Huh?, backwards?, why?
 
I can see that I in haste probably was confusing you more than helping with that comment.

Normally when you move elements in an array, you move it by copying from one place to the other (that is, you don't swap the elements). In this case you must iterate the opposite way as I mentioned when moving within the same or overlapping array, or you will overwrite the array with a repeating pattern of the first elements you start moving (you can easily make a hand simulation to see this). In practice you will probably want to use the memmove function [1] or similar to move array contents and it handles the overlap issue just fine.

However, in your case you have chosen to swap each element and then the direction of iteration does not matter. Using swap instead of move is indeed one way to solve the "direction problem", but you should know that it comes of the price of doing twice as many memory operations as is required when you do it with a move.

In other words, while swapping logically is a perfectly valid solution I suspect that your instructor may still be interested to see you are able to handle the case of moving array elements.

[1] http://www.cplusplus.com/reference/clibrary/cstring/memmove/
 
Last edited by a moderator:

Similar threads

Replies
21
Views
3K
Replies
3
Views
1K
Replies
4
Views
1K
Replies
5
Views
2K
Replies
1
Views
1K
Replies
4
Views
2K
Replies
4
Views
5K
Back
Top