jeudi 25 juin 2020

Identify the segmentation fault

problem 1 problem conti

Refer the images for the complete question. I get segmentation fault 50% of the time when i use dynamic array. Even this code i got a segmentation fault. I have typed the input you can copy and paste it. Input 2 5 1 0 5 1 1 7 1 0 3 2 1 0 2 1 1
output 7 3 explanation

#include <iostream>
using namespace std;
static int last_ans=0;
int main ()
{
int n,q;
cin>>n>>q;
int **s=new int*[n];
for(int i=0;i<q;i++)
{
        s[i]=new int[q];
}

int* j=new int[n];
for(int i=0;i<q;i++)
{

         int a,b,c;
         cin>>a>>b>>c;
         if(a==1)
         { 
 
               last_ans=(b^last_ans)%n;
               s[last_ans][j[last_ans]]=c;
               j[last_ans]++;
         }

        else
        {
               last_ans=s[(b^last_ans)%n][c];
               cout<<last_ans<<endl;
        }

    }

      return 0;
}

Aucun commentaire:

Enregistrer un commentaire