-1
int main()
{
    int newposition, shiftSteps;
    int numbers[10], numberscopy[10];
    cin >> shiftSteps;
    for (int i = 0; i < 10; i++)
        cin >> numbers[i];

    for (int i = 0; i < 10; i++)
        numberscopy[i] = numbers[i];

    //------------------------------------

    for (int i = 9; i >= 0; i--)
    {
        if (i - shiftSteps < 10 && i - shiftSteps >= 0)
            newposition = i - shiftSteps;
        else
            newposition = i - shiftSteps + 10;
        numbers[newposition] = numberscopy[i];
    }
    for (int i = 0; i < 10; i++)
        cout << numbers[i] << " ";
}

I want to rotate 10 numbers to left and "shiftSteps" is number of moves to the left. but I have a problem, the code I wrote so far for some numbers it works properly like {0 1 2 3 4 5 6 7 8 9} and shiftSteps = 3 output is 3 4 5 6 7 8 9 0 1 2. but if inputs are 0 1 2 3 4 5 6 7 8 9 and shiftSteps = 15, the output is 5 6 7 8 9 5 6 7 8 9 and 0 Disappears, True answer for shiftSteps = 15 is 5 6 7 8 9 0 1 2 3 4.

Jayco
  • 45
  • 1
  • 6
  • 1
    In your example `shiftSteps` is uninitializated. – Gary Strivin' Jan 21 '21 at 22:41
  • 2
    [Why using "namespace std;" is considered a bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Gary Strivin' Jan 21 '21 at 22:41
  • 1
    `std::rotate` will do what you want. – sweenish Jan 21 '21 at 22:42
  • 3
    There is [`std::rotate`](https://en.cppreference.com/w/cpp/algorithm/rotate) – Jarod42 Jan 21 '21 at 22:42
  • 1
    @GaryNLOL: There is no `using namespace std;` in OP code. It might be `using std::cin;` :) – Jarod42 Jan 21 '21 at 22:44
  • 1
    @Jarod42 `using std::cin;` in the global namespace isn't exactly ideal either. But at least it's not nearly as bad as `using namespace std;`. – eerorika Jan 21 '21 at 22:49
  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – R Sahu Jan 21 '21 at 22:52
  • Where is your code that reads `shiftSteps `? It appears that you have some indexing errors. I suggest adding a line `shiftSteps = shiftSteps % 10;` after you read that number to fix indexing errors. – R Sahu Jan 21 '21 at 22:56
  • You could look at ready implementations like the one @Jarod42 pointed out and try to make use of that ([example](https://godbolt.org/z/T66vha)). Analyze what it's doing and perhaps you'll get some good ideas for your own implementation. – Ted Lyngmo Jan 22 '21 at 00:34

1 Answers1

0

The problem is that newposition = i - shiftSteps + 10; results in a negative value for shiftSteps == 15 and i < 5. This results in an out-of-bounds access.

You need to ensure that the rotation amount is below the number of elements of the array, which can be achieved with a modulus operator.

    shiftSteps = shiftSteps % 10;

    for (int i = 9; i >= 0; i--)
    {
        newposition = i - shiftSteps;
        if (newposition < 0)
            newposition += 10;
        numbers[newposition] = numberscopy[i];
    }

This will work for non-negative values of shiftSteps. If you also need to handle negatives, you should adjust the condition in the loop accordingly.

PS: Also, note that in your code shiftSteps is left uninitialized.

PPS: You could also use std::rotate algorithm.

Andrey Semashev
  • 10,046
  • 1
  • 17
  • 27