Getting SIGSEGV error in FCTRL2 problem

Here is my source code :

#include <iostream
#include <stdio.h>
using namespace std;

int main(void) {

int testCases,i,j,k,n,num,digits,carry=0,temp;
scanf("%d",&testCases);
int *arr[testCases];
int digitsArr[testCases];


for(i=0;i<testCases;i++){
    scanf("%d",&n);
    num=n;
    arr[i] = new int[160];
    if(n == 0 || n==1){
        *arr[i]=1;
        digits=1;
    }
    else{
        
        for(j=10,k=0;n!= 0;j*10,k++){
            *(arr[i]+k) = n%j;
            n=n/j;
        }
        digits=k;
        for(j=1;j<num;j++){
            for(k=0;k<digits;k++){
                temp = (*(arr[i]+k)) * j + carry;
                
                if(temp>10){
                    *(arr[i]+k) = temp%10;
                    carry = temp/10;
                }
                else{
                    *(arr[i]+k) = temp;
                    carry=0;
                }
            }
	            if(carry > 10){
	                *(arr[i]+k)=carry%10;
	                k++;
	                *(arr[i]+k)=carry/10;
	                digits=k+1;
	                carry=0;
	            }
	            else if(carry >0){
	                *(arr[i]+k)=carry;
	                digits=k+1;
	                carry=0;
	            }     
            }
        }
         digitsArr[i]=digits-1;
    }


for(i=0;i<testCases;i++){
    for(j=digitsArr[i];j >= 0;j--){
    printf("%d",*(arr[i]+j));
    }
    printf("\n");
}
return 0;

}

https://www.codechef.com/wiki/status-codes

You may refer to these contents , your code almost looks fine

The code behaves differently, everytime i run it. It either shows SIGSEGV, SIGABRT error or executes successfully. But when i submit it, the wrong answer notification pops up. Though it shows correct answer for all the manual inputs i put in.

The online compiler of codechef refers to the NZEC, SIGSEV, SIGABRT runtime errors as WA only. So, there is a chance that these types are encountered. You can go through the link, it will help you. You can try out some cases that you encounter the errors and try debugging different parts of the code.

1 Like