-1

why am I getting '1' as last output at index arr[2] after shifting the elements

#include <iostream>
using namespace std;

void shifting(int* arr)
{
    int i, j;
    for (i = 0; i < 3; i++)
    {
        arr[i] = arr[i + 1];
    }

    for (i = 0; i < 3; i++)
    {
        cout << arr[i] << endl;
    }
}

int main()
{
    int array[n] = { 5, 2, 3 };
    shifting(array);       //shifting the elements to left side 
    return 0;
}

output: 2 3 1

Topaco
  • 40,594
  • 4
  • 35
  • 62
krish
  • 1
  • 1
  • 5
    In this line: `arr[i] = arr[i + 1];` you have UB when `i` is 2 (in the last iteration). – wohlstad Jul 06 '22 at 13:39
  • 2
    Your code has undefined behavior. You read one element beyond the end of the array. The program should crash, but it can return the random value that is in that location. Also learn to use std::array or std::vector instead of refering to arrays by a pointer to the first element (that's a kind of "C" legacy). And do not use "using namespace std;" – Pepijn Kramer Jul 06 '22 at 13:40
  • 1
    @OP After shifting, what is the expected output for the last value? You never mentioned what you *should* be getting -- instead you only posted what you *are* getting. Right now, it isn't clear what your expected output should be.' – PaulMcKenzie Jul 06 '22 at 13:49
  • If your entire question is "why am I getting 1", the entire answer is that you are getting undefined behavior for reading past the end of an array. It is entirely undefined what you will get from this program. – Drew Dormann Jul 06 '22 at 13:59
  • 2
    What I get is: **:20:15: error: 'n' was not declared in this scope** https://godbolt.org/z/1xceWeaqG – Goswin von Brederlow Jul 06 '22 at 14:29
  • 1
    `shifting` should take a `std::span`, `std:array` or `std::vector`. And use `at` to get out-of-bounds exceptions. – Goswin von Brederlow Jul 06 '22 at 14:30

4 Answers4

0

The way you are shifting is wrong. The proper way to do it is to store the value in a temp element and then shift.

For eg.

temp=arr[0]
for (i = 0; i < 3; i++)
    {
        arr[i] = arr[i + 1];
    }

Here add arr[2]=temp

  • 3
    This is only a part of the answer. The code still has Undefined Behaviour in the last iteration and `temp` is unused. – Yksisarvinen Jul 06 '22 at 13:42
0

You're indexing an out of bounds array on line

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

when i=2 the right hand side evaluates to arr[3], which is out of bounds.

This generates undefined, buggy behavior. This has already been answered much better by people at this question.

FrontDeer
  • 28
  • 5
0

This is called undefined behavior in C++, the way you are shifting is indexing the array out of bounds, meaning that you are trying to access an element that doesn't exist in the array.

0

It depends on what you want the last value to be. If you want the array to output 2, 3, 5:

  • store the first element(arr[0]) in a temp variable
  • iterate through the array just like you did but stop at the (n-1)th element i.e., (i < n-1)
  • then store the temp value in arr[n-1].
mg4603
  • 1