Shifting Array in C: Solving Last Element Problem

  • Thread starter James889
  • Start date
  • Tags
    Array
In summary, the task at hand is to write a function that shifts an array by a given number of steps. The shifting process is not the issue, but rather how to deal with the last element of the array. The use of the modulus operator may be helpful in this case. Additionally, when moving elements within the same array, it is important to iterate in the opposite direction to avoid overwriting the array. While swapping may be a valid solution, using a move function may be more efficient in terms of memory operations.
  • #1
James889
192
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
  • #2
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?
 
  • #3
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.
 
  • #4
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?
 
  • #5
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:

FAQ: Shifting Array in C: Solving Last Element Problem

How does shifting an array in C solve the last element problem?

Shifting an array in C involves moving all the elements of the array by one position to the left or right. This means that the last element of the array will now be in the position of the second to last element, effectively solving the last element problem.

What is the last element problem in C?

The last element problem in C refers to the issue of accessing or modifying the last element of an array. This is because the index of the last element is always one less than the size of the array, making it prone to errors or omissions.

Can shifting an array in C cause data loss?

No, shifting an array in C does not cause data loss. The values in the array are simply moved to different positions, but they are not removed or overwritten.

How do you shift an array in C?

To shift an array in C, you can use a for loop to iterate through each element and assign it to the next or previous position in the array. You can also use built-in functions such as memmove() or memcpy() to shift the elements.

Is shifting an array the only way to solve the last element problem in C?

No, there are other ways to solve the last element problem in C. One approach is to use a different data structure, such as a linked list, which does not have a fixed size and does not require shifting. Another way is to use a separate variable to keep track of the last element index, making it easier to access and modify the last element of the array.

Similar threads

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