Reputation: 1
My code runs fine in code blocks but, when I submit it shows runtime sigabrt
error, what does this error mean?
When I initialize the size of pointer array as 200 it works fine but I want to know what is this error?
link for question is: http://www.spoj.com/problems/FCTRL2/
Here is the code:
#include <iostream>
using namespace std;
void multiply(int x,int *nums,int &len){
int carry=0;
int prod;
for (int j=0;j<len;j++){
prod=nums[j]*x+carry;
carry=prod/10;
nums[j]=prod%10;
}
while (carry){
nums[len]=carry%10;
carry=carry/10;
len++;
}
}
int main()
{
int t,n;
cin>>t;
int arr[100];
for (int i=0;i<t;i++){
cin>>arr[i];
}
for (int i=0;i<t;i++){
n=arr[i];
if (n==0){cout<<0<<endl;}
else{
int *nums= new int[0];
nums[0]=1;
int len=1;
for (int i=2;i<=n;i++){
multiply(i,nums,len);
}
for (int i=len-1;i>=0;i--){
cout<<nums[i];
}
cout<<endl;}
}
return 0;
}
Upvotes: 0
Views: 240
Reputation: 1952
Change int *nums = new int[0];
to int *nums = new int[x]
, where x
is the maximum size of the array (depending on the number of digits you're expecting in the factorial). According to the constraints (1 <= n <= 100
) A suitable value of x
would be 158
.
int *nums = new int[n]
allocates memory for an n
element array.
Upvotes: 2