-1

I'm solving a problem on array rotation. We take an array and the number of transitions to take place as input from the user. For example, Assume that the array A[]={1,2,3,4,5} and the number of transitions(d)=2. Then, the output will be : {3,4,5,1,2} , etc.

This is the code that I've written, but I'm getting a Segmentation fault. What should I do?

Code:

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

int main()
{
    int n,d;
    cout<<"Enter the number of elements:"<<endl;
    cin>>n;
    cout<<"Enter the elements:"<<endl;
    int a[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    cout<<"Enter the number of transitions:"<<endl;
    cin>>d;
    int temp[d];
    //To store the first d elements of the array
    for(int i=0;i<d;i++)
    {
        temp[i]=a[i];
    }
    //To move the elements by d positions
    for(int i=0;i<(n-d);i++)
    {
        a[i]=a[i+d];
    }
    //Fill the d elements to the end
    for(int i=(n-d);i<n;i++)
    {
        for(int i=0;i<d;i++)
        {
            cin>>temp[i];
        }
    }
    //Print the final array
    cout<<"The final array is: "<<endl;
    for(int i=0;i<n;i++)
    {
        cout<<setw(3)<<a[i];
    }
    return 0;
}
Absolute7
  • 3
  • 3
  • 5
    Unrelated: Please read [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – Ted Lyngmo Dec 25 '20 at 13:01

3 Answers3

0

Firstly, in this part

    //To move the elements by d positions
    for(int i=0;i<(n-d);i++)
    {
        a[i]=a[i+2];
    }

is is weird to use the fixed number 2 while the shift width d is configurable. It seems i+2 should be i+d.

Then, in C++ variable-length array (VLA) like int a[n]; and int temp[d]; are non-standard. It should be replaced with std::vector like std::vector<int> a(n); and std::vector<int> temp(d);. Add #include <vector> at the top of your code to use std::vector. Also this will prevent it from causing Segmation fault by stack overflow when trying to allocate large arrays.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

I'm getting a Segmentation fault. What should I do?

I'd start by looking for a solution in the standard library, and it happens to contain a function to do just what you need: std::rotate.

Example:

#include <algorithm>
#include <iterator>

// ...

std::rotate(std::begin(a), std::next(std::begin(a), d), std::end(a));

Please also read Why aren't variable-length arrays part of the C++ standard?. Your int a[n]; should be replaced by std::vector<int> a(n);

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

As other saide VLA (variable0length array) should not be adopted. Also rotating the array can be simplified by the ring buffer algorithm using modulo operator or if it's size is by next power of 2 with bitwise &.

ex.:

#include <cstring>
#include<iostream>
#include<iomanip>
#include <vector>
using namespace std;

int main()
{
    int arr[] = {1,2,3,4,5};
    std::vector <int> arr2;
    int n ;
    cin >> n;

    for(int i=n; i < n + 5; )
        arr2.push_back(arr[i++ % 5]);

    for(int i=0; i < 5; i++) {
        std::cout << "[" << arr2[i] << "]";
    }

    return 0;
}
Ilian Zapryanov
  • 1,132
  • 2
  • 16
  • 28