I am solving a problem https://www.codechef.com/problems/DCGAME/
Inside this, I am not able to find the reason behind getting Runtime Error(SIGSEGV). On my PC, outputs are fine. I know the meaning of this error but am still not able to find where I am making the mistake. My code is below:
#include <stdio.h>
void calc(long long int arr1[],int n,long long int arr2[][n],long long int game[],int m)
{
//printf("Pre process %d %d",n,m);
int i,j;
for(i=0;i<n;i++){game[arr1[i]]++;}
for(i=1;i<n;i++)
{
for(j=0;j<=n-i-1;j++)
{
if(arr2[i-1][j]>arr1[i+j])
arr2[i][j]=arr2[i-1][j];
else
arr2[i][j]=arr1[i+j];
game[arr2[i][j]]++;
}
}
//for(i=0;i<=m;i++){printf(" %d ",game[i]);}
}
int main(){
int n,m,i,count=0,k;
char c,p;
scanf("%d %d",&n,&m);
long long int a[n],b[n][n],max=0;
for(i=0;i<n;i++)
{
scanf("%lld",&a[i]);
if(a[i]>max)max=a[i];
b[0][i]=a[i];
}
long long int game[max+1];
for(i=0;i<=max;i++)
game[i]=0;
calc(a,n,b,game,max);
while(m--)
{
// printf("WHILE-M");
c=getchar();
//scanf("%c",&c);
scanf("%c %d %c",&c,&k,&p);
switch(c)
{//printf("SWITCH");
case '<':
//printf("CASE <");
if(k>max){if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}break;}
for(i=1;i<k;i++)
{if(game[i]>0)
count+=game[i];
}
if(count%2==0)
{
if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}
}
else
{
if(p=='D')
{
printf("D");count=0;
}
else
{
printf("C");count=0;
}
}
break;
case '>':
//printf("CASE >");
if(k>max){if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}break;}
for(i=k+1;i<=max;i++)
{if(game[i]>0)
count+=game[i];
}
if(count%2==0)
{
if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}
}
else
{
if(p=='D')
{
printf("D");count=0;
}
else
{
printf("C");count=0;
}
}
break;
case '=':
//printf("CASE =");
if(k>max){if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}break;}
count+=game[k];
if(count%2==0)
{
if(p=='D')
{
printf("C");count=0;
}
else
{
printf("D");count=0;
}
}
else
{
if(p=='D')
{
printf("D");count=0;
}
else
{
printf("C");count=0;
}
}
break;
}
}
return 0;
}
If your algorithm is otherwise correct (I didn't check) but you are running out of stack space, then a short fix is to change long long int a[n],b[n][n],max=0; to:
long long int *a, (*b)[n], max = 0;
a = malloc(n * sizeof *a);
b = malloc(n * sizeof *b);
and also change long long int game[max+1]; for(i=0;i<=max;i++) game[i]=0; to:
long long int *game = calloc(max+1, sizeof *game);
Also you should check for failure, e.g. if ( !a || !b ) exit(EXIT_FAILURE);. The rest of the code can remain unchanged.
Related
I've written a very simple program which calculate the factorial of a number, the problem is that from 30 to 60 approximately, it returns a negative number and from 70 it returns 0.
I don't what I've done wrong. Could this problem depend on the computing power of my computer?
Here's the code:
#include <stdio.h>
int main(){
int x, i;
long long int f = 1;
printf("Insert a number:");
scanf("%d", &x);
if (x == 0){
printf("0! = 1");
}
else {
for (i = 1; i <= x; i++){
f *= i;
}
printf("%d! รจ = %lli", x, f);
}
return 0;
}
Here is my code:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void Carry(int bit[],int pos)
{
int i,carray=0;
for(i=0;i<=pos;i++)
{
bit[i]+=carray;
if(bit[i]<=9)
{
carray=0;
}
else if(bit[i]>9&&i<pos)
{
carray=bit[i]/10;
bit[i]%=10;
}
else if(bit[i]>9&&i>=pos)
{
while(bit[i]>9)
{
carray=bit[i]/10;
bit[i]%=10;
i++;
bit[i]=carray;
}
}
}
}
int main()
{
int num,pos,digit,i,j,m,n;
double sum=0;
int *fact;
printf("input want to calculute factorial num:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
sum+=log10(i);
}
digit=(int)sum+1;
if(!(fact=(int *)malloc((digit+1)*sizeof(int))))
{
printf("malloc failed\n");
return 0;
}
for(i=0;i<=digit;i++)
{
fact[i]=0;
}
fact[0]=1;
for(i=2;i<=num;i++)
{
for(j=digit;j>=0;j--)
{
if(fact[j]!=0)
{
pos=j;
break;
}
}
for(j=0;j<=pos;j++)
{
fact[j]*=i;
}
Carry(fact,pos);
}
for(j=digit;j>=0;j--)
{
if(fact[j]!=0)
{
pos=j;
break;
}
}
m=0;
n=0;
for(i=pos;i>=0;i--)
{
printf("%d",fact[i]);
m++;
if(m%4==0)
{
printf(" ");
}
if(m==40)
{
printf("\n");
m=0;
n++;
if(n==10)
{
printf("\n");
n=0;
}
}
}
printf("\n\n");
return 0;
}
I was practising some data structures problems that I did previously but this time I don't know what is going wrong in my code. I looked over a long time but I did not found the mistake. When I'm printing I'm just getting the first character and it looks like e is not being updated. But I've written e++.
#include<stdio.h>
#include "ctype.h"
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int priorityof(char x)
{
if(x=='(')
return 3;
else if(x=='+'|| x=='-')
return 1;
else if(x=='*'|| x=='/')
return 2;
}
int main()
{
char exp[20];
char *e;
e=exp;char x;
scanf("%c",exp);
while(*e!='\0')
{
if(isalnum(*e))
{
printf("%c", *e);
}
else if(*e=='(')
{
push(*e);
}
else if(*e==')')
{
while((x=pop())!='(')
printf("%c",x);
}
else {
while (priorityof(stack[top]) >= priorityof(*e)) {
printf("%c", pop());
push(*e);
}
}
e++;
}
while(top!=-1)
{
printf("%c",pop());
}
}
%c is for single character and reading your question it seems like you are giving more than one character so its a string, use %s.
#include<stdio.h>
#include "ctype.h"
int stack[20]; int top = -1;
void push(int x) {
stack[++top] = x;
}
int pop() { return stack[top--]; }
int priorityof(char x) {
if(x=='(') return 3;
else if(x=='+'|| x=='-') return 1;
else if(x=='*'|| x=='/') return 2;
}
int main() {
char exp[20];
char *e;
e=exp;char x;
scanf("%s",exp);
while(*e!='\0') { if(isalnum(*e)) { printf("%c", *e); } else if(*e=='(') { push(*e); } else if(*e==')') { while((x=pop())!='(') printf("%c",x); } else { while (priorityof(stack[top]) >= priorityof(*e)) { printf("%c", pop()); push(*e); } } e++; } while(top!=-1) { printf("%c",pop()); } }
I am trying to create program to add two matrixes. After typing input like [12 4] program crashes when function strcat starts.
I have no idea what is wrong. func.h consists of stdio.h, _mingw.h,stdlib.h and string.h
#include"func.h"
void string_to_table(double **matrix, char inpt[])/*problematic function*/
{
printf("convertion start");
int i=strlen(inpt);
printf("%i",i);
int j=1;
int k=0,l=0;
char num[128];
double converted=0;
printf("breakpoint1");
while(j<(i-1))
{
if(inpt[j]==' ')
{
printf("first if");
converted=atof(num);
num[0]='\0';
matrix[k][l]=converted;
++l;
printf("breakpoint2");
}
else if(inpt[j]==';')
{
printf("second if");
converted=atof(num);
num[0]='\0';
matrix[k][l]=converted;
++k;
l=0;
}
else
{
printf("third if");
strcat(num,inpt[j]);/*place when everything crashes*/
}
++j;
}
printf("convert0 end");
}
void add_matrix(double **matrix1, double **matrix2,int i,int j)
{
int k=0;
int l=0;
while(k<i)
{
while(l<j)
{
matrix1[k][l]+=matrix2[k][l];
++l;
}
l=0;
++k;
}
int matrixproccesing(int *i,int *j, char m[])/*sprawdzanie poprawnosci wejscia*/
{
printf("macro start");
int columnnum=0,rownum=0,x=0,piv=0,check=0;
int textsize=strlen(m);
printf("%i",i);
printf("loop start");
printf("%i",textsize);
while(x<(textsize-1))
{
printf("%i",x);
printf("\n");
if(x==0)/*czy poczatek to [*/
{
if(m[x]!='[')
return 0;
}
else if(x==(textsize-2))/*czy koniec to]*/
{
printf("kohec");
if(m[x]==']')
break;
return 0;
}
else if((m[x]>47&&m[x]<58)||(m[x]==' ')||(m[x]=='.')||(m[x]==';')||(m[x]=='-'))/*czy liczba*/
{
if(m[x]==';')/*czy ilosc liczb w rzedzie taka sama*/
{
if(check==0)
{
check=columnnum;
}
else if(check!=columnnum)
{
return 0;
}
printf("colnum");
columnnum=0;
rownum++;
}
else if(m[x]==' ')/*czy nowa liczba/kolumna */
{
columnnum++;
}
}
++x;
}
*i=(check+1);
*j=(columnnum+1);
printf("macro end");
return 1;
}
int is_same_size(int a, int b,int c ,int d)/*test rozmiaru*/
{
if((a==c)&(b==d))
return 1;
return 0;
}
void print_da_matrix(double **matrix, int i, int j)
{
int k=0,l=0;
printf("[ ");
while(k<i)
{
while(l<j)
{
printf("%f",matrix[k][l]);
printf(" ");
}
printf(";");
l=0;
if(k<(i-1))
++k;
}
printf("]");
}
void release_the_memory(double **matrix, int i)
{
int k=0;
while(k<i)
{
free(matrix[k]);
++k;
}
free(matrix);
matrix=NULL;
}
}
int main()
{
int i=0,j=0,m1=0,m2=0,tabcr=0;
char matrix[512];
fgets(&matrix,511,stdin);
double **matrix1;
double **matrix2;
if(!matrixproccesing(&i,&j,matrix))
{
printf("zle wejscie");
return 0;
}
matrix1=(double**)malloc(i*sizeof(double *));
while(tabcr<j)
{
matrix1[tabcr]=(double*)malloc(j*sizeof(double));
++tabcr;
}
string_to_table(matrix1,matrix);
printf("\n");
printf("podaj druga macierz");
fgets(&matrix,511,stdin);
if(!matrixproccesing(&m1,&m2,matrix))
{
printf("zle wejscie");
return 0;
}
tabcr=0;
if(!is_same_size(i,j,m1,m2))
{
printf("matrixes have different size.");
return 0;
}
matrix2=(double**)malloc(i*sizeof(double *));
while(tabcr<j)
{
matrix2[tabcr]=(double*)malloc(j*sizeof(double));
++tabcr;
}
string_to_table(matrix2,matrix);
add_matrix(matrix1,matrix2,i,j);
/* print_da_matrix(matrix1,i,j);
release_the_memory(matrix1,i);
release_the_memory(matrix2,i);*/
return 0;
}
I tried to solve a prime number generator problem in spoj.com. I solved it and it runs perfectly on my machine. But after submitting the solution to online, it shows run time error. Please help me!
Could anyone take a look into my code and tell where the problem is?
#include <stdio.h>
int primeFactor[350],w=0,z,i;
main()
{
int m[9],n[9],t=0,tMax,j;
scanf("%d",&tMax);
while(t<tMax)
{
scanf("%d%d",&m[t],&n[t]);
primeFactors(n[t]);
t++;
}
for(j=0;j<tMax;j++)
{
for(z=m[j];z<=n[j];z++)
{
if(z>1)
primeCalc(z);
}
printf("\n\n");
}
}
primeFactors(int a)
{
int remainder;
for(z=2;z<=sqrt(a);z++)
{
if(z==2) {primeFactor[w]=z; w++;}
else
{
for(i=2;i<z;i++)
{
if(z%i==0)
{
remainder=z%i;
break;
}
else {remainder=z%i;}
}
if(remainder!=0)
{
primeFactor[w]=z;
w++;
}
}
}
return 0;
}
primeCalc(int x)
{
int remainder;
if(x==2)
{
printf("%d\n",x);
}
else
{
for(i=0;i<w;i++)
{
if(primeFactor[i]>=x)
break;
else if(x%primeFactor[i]==0)
{
remainder=x%primeFactor[i];
break;
}
else
{remainder=x%primeFactor[i];}
}
if(remainder!=0)
printf("%d\n",x);
}
return 0;
}
Your main function may be returning some indeterminate value because it doesn't have any return statement and the judge may be treating it as Runtime Error.
You will have to return 0 from the main function.
Also, you shouldn't omit the return type of each functions.
Try this:
#include <stdio.h>
int primeFactor[350],w=0,z,i;
int main(void)
{
int m[9],n[9],t=0,tMax,j;
scanf("%d",&tMax);
while(t<tMax)
{
scanf("%d%d",&m[t],&n[t]);
primeFactors(n[t]);
t++;
}
for(j=0;j<tMax;j++)
{
for(z=m[j];z<=n[j];z++)
{
if(z>1)
primeCalc(z);
}
printf("\n\n");
}
return 0;
}
int primeFactors(int a)
{
int remainder;
for(z=2;z<=sqrt(a);z++)
{
if(z==2) {primeFactor[w]=z; w++;}
else
{
for(i=2;i<z;i++)
{
if(z%i==0)
{
remainder=z%i;
break;
}
else {remainder=z%i;}
}
if(remainder!=0)
{
primeFactor[w]=z;
w++;
}
}
}
return 0;
}
int primeCalc(int x)
{
int remainder;
if(x==2)
{
printf("%d\n",x);
}
else
{
for(i=0;i<w;i++)
{
if(primeFactor[i]>=x)
break;
else if(x%primeFactor[i]==0)
{
remainder=x%primeFactor[i];
break;
}
else
{remainder=x%primeFactor[i];}
}
if(remainder!=0)
printf("%d\n",x);
}
return 0;
}
i am pursuing the course on programming and data structures in nptel's MOOC.
A programming assignment in the course requires us to calculate sum of selected coefficients.
Now the program i wrote calculated the answer , but i am experiencing a runtime error.
Now i had this inclination of using gets() instead of scanf() so as to speed up my input.
how do i do that?
the code is as follows:
#include<stdio.h>
int combi(int ,int);
long fact(int);
int main()
{
int r,i,v[20],p[20],t,l=1,b=0,sum=0,a=0,flag=0;
char ch='a';
scanf("%d",&r);
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&v[0]);
if(getchar()==' ')
{
if(r<v[0])
{
while(ch!='\n')
{
scanf("%d",&v[l]);
l++;
if(getchar()=='\n')
{
p[a]=-1;
a++;
l=1;
break;
}
}
}
else
{
while(ch!='\n')
{
scanf("%d",&v[l]);
if(v[l]>v[0])
{
flag=1;
}
l++;
if(getchar()=='\n')
{
if(flag>0)
{
p[a]=-1;
a++;
l=1;
sum=0;
flag=0;
break;
}
else
{
for(b=1;b<l;b++)
{
sum=sum+combi(v[0],v[b]);
}
p[a]=sum;
a++;
sum=0;
l=1;
break;
}
}
}
}
}
}
for(i=0;i<t;i++)
{
printf("%d\t ",p[i]);
}
printf("\n");
}
int combi(int x,int y)
{
int a=fact(x)/(fact(x-y)*fact(y));
return a;
}
long fact(int z)
{
int i=1;
long f=1;
while(i<=z)
{
f=f*i;
i++;
}
return f;
}