-1

I have an array called int arr[10] = {1,2,3,4,5}

From my understanding the rest of the array is filled with 0's.

My questions is if its a fixed array length how can I put the first index behind the last index that is not a 0. For example

I believe the 0 is not shown in real printf but I am including it for illustration purposes

for (int i = 0 ; i < 10 ; i++)
{
    print("%i" , arr[i]);
}

The output

1 2 3 4 5 0 0 0 0 0

If i move the first index to the back of the 5 like so

for (int i = -1 ; i < 10 ; i++)
{
    arr[i] = arr[i + 1];
    print("%i" , arr[i]);
}

Will the output put the 1 behind the 5 or at the back of the whole array?

2 3 4 5 1 0 0 0 0 0

or because there is 0s then

2 3 4 5 0 0 0 0 0 1

If my question is unclear please tell me and I will try explain it.

  • 1
    Also, `arr[i + 1]` will fail when `i` reaches `9`. – vgru Sep 03 '19 at 12:34
  • Possible duplicate of [Arrays: Left Rotation in C](https://stackoverflow.com/questions/40774788/arrays-left-rotation-in-c) or [Rotate array left or right by a set number of positions with linear time complexity](https://stackoverflow.com/q/22078728/69809) or [How to rotate an array in Java](https://stackoverflow.com/q/31174840/69809). Basically, if you want to rotate a single element to the end, save it to a temp variable, then shift items from `1` to `len-1` one place to the left, and then assign temp to the last array item. – vgru Sep 03 '19 at 12:36
  • The fact that you only explicitly initialize the first 5 elements does not affect the indexes in any way - the array is still 10 elements long, 0 through 9. – 500 - Internal Server Error Sep 03 '19 at 12:37
  • 1
    What happened when you actually ran your code? – PhoenixBlue Sep 03 '19 at 12:37

2 Answers2

4

The output

1 2 3 4 5 0 0 0 0 0

No, the actual output is

1234500000

Your code has undefined behavior. The first iteration of the loop (with i = -1) tries to assign to arr[-1], which does not exist:

    arr[i] = arr[i + 1];

Similarly, the last iteration (with i = 9) tries to read from arr[10], which also does not exist.

I'm not sure why you think your code will move the first element back.

Community
  • 1
  • 1
melpomene
  • 84,125
  • 8
  • 85
  • 148
0

From my understanding the rest of the array is filled with 0's

You are right.:)

If i move the first index to the back of the 5 like so

for (int i = -1 ; i < 10 ; i++)
{
    arr[i] = arr[i + 1];
    print("%i" , arr[i]);
}

then you will get undefined behavior because the indices -1 and 10 are not valid indices.

It seems what you are trying to do is the following

#include <stdio.h>
#include <string.h>

int main(void) 
{
    enum { N = 10 };
    int a[N] = { 1, 2, 3, 4, 5 };

    size_t pos = 0;

    while ( pos < N && a[pos] != 0 ) ++pos;

    if ( pos != N && !( pos < 3 ) )
    {
        int tmp = a[0];
        pos -= 2;

        memmove( a, a + 1, pos * sizeof( int ) );

        a[pos] = tmp;
    }

    for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[i] );
    putchar( '\n' );

    return 0;
}

The program output is

2 3 4 1 5 0 0 0 0 0 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335