Shifting Array in C: Solving Last Element Problem

  • Thread starter Thread starter James889
  • Start date Start date
  • Tags Tags
    Array
Click For Summary

Discussion Overview

The discussion revolves around implementing a function in C that shifts an array by a specified number of steps. Participants explore the mechanics of shifting elements, particularly focusing on how to handle the last element of the array during the shift operation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes a function that shifts an array and seeks help on how to correctly place the last element at the beginning after the shift.
  • Another participant questions the destination of specific array elements after a shift, suggesting the use of an operator to clarify the movement of elements.
  • A suggestion is made to use the modulus operator to assist with the shifting logic.
  • Concerns are raised about the direction of iteration when moving elements within the same array, emphasizing that it should be done in the opposite direction of the intended move.
  • Another participant clarifies that when moving elements, copying rather than swapping is typically preferred to avoid overwriting data, but acknowledges that swapping can be a valid approach in this context.
  • It is noted that while swapping is a valid solution, it may not be the most efficient method compared to using functions designed to handle overlapping memory regions, such as memmove.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to shifting elements in an array, particularly regarding the use of swapping versus moving elements. There is no consensus on a single method, and the discussion remains unresolved regarding the optimal implementation.

Contextual Notes

Participants highlight potential pitfalls in their approaches, such as the risk of overwriting data when elements are moved within the same array. There is an acknowledgment of the complexity involved in handling overlapping memory regions.

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 18 ·
Replies
18
Views
3K
Replies
43
Views
5K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K