What is causing segmentation fault/buss error in this?

its a test code , i am learning use of stoi()

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include<string>

using namespace std;

int main()
{ 
    int N;

string S,T;

int i,j,A[N],B[N];

cout<<"enter size ";
cin>>N;

cout<<"enter strings ";
cin>>S;

char temp1;
for(i=0;i<N;++i)
{
   if(i==0)
   {
       A[i]=stoi(S,0,2);
       
   }
   else
   {
       temp1=S[i];
       S[i]=S[i-1];
       S[i-1]=temp1;
       
       A[i] = stoi(S,0,2);
       
   }
   
}
cout<<"all formations are ";
for(i=0;i<N;++i)
{
    cout<<A[i]<<endl;
}
    

    return 0;
}

You’re defining A and B to be arrays of N elements when you haven’t initialised N yet.

1 Like

oh …thanks bro

1 Like

one question i have ,is it compusory to define modulo in question where it is mention modulo is 1000000007 or program will run without it

You should define and use it as the question states, otherwise you’ll get the wrong answer :slight_smile:

like for smaller value you don’t need modulo.

If it’s guaranteed that the correct answer is never \ge the modulus, then you can leave it out, if you want.

2 Likes

ok.thank you again

1 Like

if you use modulo for smaller values it wont affect the answer, but for bigger values you have to use.
So it is good to put modulo if mentioned for all the answers.

1 Like

so nice of you bro .i have some doubts which i will ask once the challenge is over