-3

I have a C++ program where I have to rotate the array clockwise according to the given number of elements to be rotated (x). For example if the input array is

[1,2,3,4,5]

given that 2 elements (denoted as x) must to rotated.

The output array should be

[3,4,5,1,2]

Code:

#include <iostream>
#include<algorithm>
using namespace std;

int main()
{ 
   int t;
   cin>>t;
   while(t--){
       int n,x;
       cin>>n>>x;
       int a[n],b[x];
       for(int i=0;i<n;i++){
           cin>>a[i];
       }
      copy(a,a+x,b);
      copy(b,b+x,a+n);
      n=n+x;
      for(int i=x;i<n;i++){
          cout<<a[i]<<" ";
      }
   }
    return 0;
}

What I'm doing here is that I copy the given number of elements to a new array. Later copy them back to the original array starting from 'n'. So my array will look like [1,2,3,4,5,1,2].

Later I'm printing out the array starting from the index 'x'. So that my array will look like [3,4,5,1,2].

I'm able to compile the program and I'm getting the output. But while submitting the code in a website called GeekforGeeks its complier is throwing out a Segmentation Fault (SIGSEGV).

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Did run your code in a **debugger** to see where that error occurs, then run it again with a breakpoint near that failure so you can step carefully ahead and watch what happens leading up to that point? – tadman Mar 30 '20 at 17:25
  • Isn't `copy(b,b+x,a+n)` asking to copy data after the *end* of the `a` array? – tadman Mar 30 '20 at 17:27
  • yes it is tadman – Sai Darshan Mar 30 '20 at 17:32
  • Hint: What do you think happens when you write outside of array bounds? – tadman Mar 30 '20 at 17:33
  • You need a compiler environment that has one, like Visual Studio Express, Xcode, or even Visual Studio Code + GCC or Clang. There's tons of options out there, those are just common examples. – tadman Mar 30 '20 at 17:34
  • wat if i first increase the array size and then the copy the elements? – Sai Darshan Mar 30 '20 at 17:37
  • Now you're on the right track. Also, as idclev points out consider using `std::vector` as we do in C++, since then you have a lot of easy resizing options not available if you're using C arrays. – tadman Mar 30 '20 at 17:37
  • @SaiDarshan No it's got nothing to do with array size. You approach is wrong, the copy statements don't execute a rotate. – john Mar 30 '20 at 17:38
  • @john You can rotate by duplicating, then taking a slice in the middle. it's a valid approach. – tadman Mar 30 '20 at 17:38
  • @tadman OK but inefficient, and I see no evidence that is what the OP is attempting. – john Mar 30 '20 at 17:39
  • @john if memory is of no concern then it is actually rather efficient – 463035818_is_not_an_ai Mar 30 '20 at 18:15

3 Answers3

3

This

int a[n],b[x];

is not standard c++. See here for details: Why aren't variable-length arrays part of the C++ standard? . Use std::vector for dynamic arrays.

Then here:

copy(a,a+x,b);

you use x but the size you used for a was n not x. Depending on your input this may acces the array out-of-bounds.

Next, here:

copy(b,b+x,a+n);

you try to copy to a+n but already a+n is beyond the last element of a. Arrays have fixed size, also n=n+x; wont help to change that.

To rotate elements of an array (or std::vector) you can use std::rotate.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Yes i used size for a as n ..but in copy i need only starting 2 elements of array a.That is y im using a+x ..x contains the number of elements to rotate.If it is 2.I will only copy starting 2 elements of array "a" – Sai Darshan Mar 30 '20 at 18:21
  • @SaiDarshan not sure if I understand what you mean. In case you refer to what i wrote about `copy(a,a+x,b);` then I wrote this **may** acces the array out-of-bounds, doesnt mean that it does fail always. Still I would never write code like that, because it looks innocent but can easily fail with dramatic consequences (undefined behavior). Anyhow, `copy(b,b+x,a+n);` is definitely accessing out-of-bounds, `a` has `n` elements, not more – 463035818_is_not_an_ai Mar 30 '20 at 18:24
  • Yes i agree with the copy(b,b+x,a+n).What if i first increase the array size and then do this statement ? – Sai Darshan Mar 30 '20 at 18:28
  • @SaiDarshan arrays have fixed size, thats why I suggest to use `std::vector` – 463035818_is_not_an_ai Mar 30 '20 at 18:29
  • ok i m new to C++ programming can u link me a demo program how to use a vector please – Sai Darshan Mar 30 '20 at 18:37
  • @SaiDarshan here you can find some examples: https://en.cppreference.com/w/cpp/container/vector/operator_at – 463035818_is_not_an_ai Mar 30 '20 at 18:47
0

Problem 1

The line

  copy(a,a+x,b);

does not do what you want to do. It copies the first x elements of a to b, without an offset. If x is 2, that is equivalent to:

 b[0] = a[0];
 b[1] = b[1];

You need something that will do:

 b[0+x] = a[0];
 b[1+x] = a[1];

 ...

 b[n] = a[n-x-1];

To achieve that, you need to use:

 std::copy(a, a+(n-x), b+x);

Problem 2

The line

copy(b,b+x,a+n);

is notright.

  1. You want to copy from a to b, not from b to a.

  2. Using a+n immediately results in accessing the array using out-of-bounds indices, which causes undefined behavior.

  3. The offsets used in that call don't make sense at all.

After the first line of std::copy is executed, you need something what will do the following:

 b[0] = a[n-x];
 b[1] = a[n-x+1];

 ...

 b[x-1] = a[n-1];

To achieve that, you need to use:

 std::copy(a+(n-x), a+n, b);
R Sahu
  • 204,454
  • 14
  • 159
  • 270
-1

Arrays must have determined size some compilers accept your code but other don't so try to use pointers instead