Run time error in C for prime generator - c

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;
}

Related

lexical analyser recognise numbers

i'm trying to make a small lexical analyser that recognizes numbers as it is in the following regular expression:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
char C[500];
bool temp;
bool Q0(char k[]);
bool Q1(char k[]);
bool Q2(char k[]);
bool Q3(char k[]);
void substr (char C[]);
char nextchar(char C[]);
int main()
{
printf("enter a string: ");
scanf("%s",C);
temp=Q0(C);
if (temp==true)
{
printf("it's number");
}
else
{
printf("false");
}
return 0;
}
bool Q0(char k[])
{
printf("%s\n",k);
printf("%c\n",k[0]);
if (k[0]=='+'||k[0]=='-')
{
return Q1(nextchar(k));
}
else if (k[0]=='0'||k[0]=='1'||k[0]=='2'||k[0]=='3'||k[0]=='4'||k[0]=='5'||k[0]=='6'||k[0]=='7'||k[0]=='8'||k[0]=='9')
{
return Q1(nextchar(k));
}
else
{
return false;
}
}
bool Q1(char k[])
{
if (k[0]=='0'||k[0]=='1'||k[0]=='2'||k[0]=='3'||k[0]=='4'||k[0]=='5'||k[0]=='6'||k[0]=='7'||k[0]=='8'||k[0]=='9')
{
return Q1(nextchar(k));
}
else if (k[0]=='.')
{
return Q2(nextchar(k));
}
else if (k[0]=='\0')
{
return true;
}
else
{
return false;
}
}
bool Q2(char k[])
{
if (k[0]=='0'||k[0]=='1'||k[0]=='2'||k[0]=='3'||k[0]=='4'||k[0]=='5'||k[0]=='6'||k[0]=='7'||k[0]=='8'||k[0]=='9')
{
return Q2(nextchar(k));
}
else if (k[0]=='e'||k[0]=='E')
{
return Q3(nextchar(k));
}
else
{
return false;
}
}
bool Q3(char k[])
{
if (k[0]=='+'||k[0]=='-')
{
return Q3(nextchar(k));
}
else if (k[0]=='0'||k[0]=='1'||k[0]=='2'||k[0]=='3'||k[0]=='4'||k[0]=='5'||k[0]=='6'||k[0]=='7'||k[0]=='8'||k[0]=='9')
{
return Q3(nextchar(k));
}
else if (k[0]=='\0')
{
return true;
}
else
{
return false;
}
}
void substr (char *C)
{
(C)++;
}
char nextchar(char *C){
substr(C);
return C[0];
}
why the code execute else section in the Q0 function ?
in the result k[0] equals 5 and it should execute the else if(k[0] == ...'5'...)
and go to the next state Q1
also i got lot of warnings about type casting and passing parameters but i can't understand why
PS: i printed the whole string and the first letter of it in order to make sure that there isn't a problem with the array

Why is my code giving right output in codeBlocks but wrong output in HackerRank?

I am trying to solve a problem of HackerRank. The problem wants me to find out if a string can be changed into a palindrome string or not with a certain number of changing characters of that string. That certain number will be given by input. I am giving the link of full description of the problem below.
In my code, I first checked if the string is already palindrome or not. Then I checked some cases under the first checking for the given number of changes to find out if that string can be changed into palindrome after changing the characters.
But I am getting different output for my code. Output that I found from code blocks is not the same as the output which I found in HackerRank.
Here is the link of the full problem statement
my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print(char str[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%c",str[i]);
}
}
void cpy(char qtr[],char str[],int n)
{
int i;
for(i=0;i<n;i++)
{
qtr[i]=str[i];
}
}
void rev(char str[],int n)
{
int i;
char qtr[100000];
for(i=0;i<n;i++)
{
qtr[i]=str[n-1-i];
}
for(i=0;i<n;i++)
{
str[i]=qtr[i];
}
}
int main()
{
char str[100],str2[100000],qtr[100000],c;
int n,k,i;
scanf("%d",&n);
scanf("%d",&k);
fflush(stdin);
gets(str);
cpy(qtr,str,n);
rev(str,n);
cpy(str2,str,n);
int same=0;
if(strcmp(str2,qtr)==0)
{
same=1;
}
if(k==n)
{
c=qtr[i]+1;
for(i=0;i<k;i++)
{
if(qtr[i]==c)
{
c=c+1;
i=-1;
continue;
}
}
for(i=0;i<k;i++){qtr[i]=c;}
print(qtr,n);
return 0;
}
if(same==1)
{
if(n%2!=0)
{
if(qtr[n/2]!='9'){qtr[n/2]='9';}
else
{qtr[n/2]='1';}
k--;
for(i=0;i<n;i++)
{
if(k<0){printf("-1");break;}
if(k==0){printf(qtr,n);break;}
if(qtr[i]=='9')
{
qtr[i]='1';
qtr[n-1-i]='1';
k=k-2;
}
else
{
qtr[i]='9';
qtr[n-1-i]='9';
k=k-2;
}
}
}
else
{
for(i=0;i<n;i++)
{
if(k<0){printf("-1");break;}
if(k==0){print(qtr,n);break;}
if(qtr[i]=='9')
{
qtr[i]='1';
qtr[n-1-i]='1';
k=k-2;
}
else
{
qtr[i]='9';
qtr[n-1-i]='9';
k=k-2;
}
}
}
}
else
{
if(n%2==0)
{
int arr[n];
for(i=0;i<n;i++){arr[i]=0;}
while(k>=0)
{
int g=0;
for(i=0;i<n/2;i++)
{
if(qtr[i]!=qtr[n-i-1])
{
arr[i]=1;
arr[n-i-1]=1;
qtr[i]=qtr[n-i-1];
k--;
g=1;
}
}
if(k==0){print(qtr,n);return 0;}
if(k%2!=0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==1)
{
arr[i]=2;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k--;g=1;
break;
}
}
}
if(k==0){print(qtr,n);return 0;}
if(k%2==0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==0)
{
arr[i]=3;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k=k-2;g=1;
break;
}
}
}
if(k==0){print(qtr,n);return 0;}
if(g==1){printf("-1");return 0;}
}
printf("-1");return 0;
}
else
{
int arr[n];
for(i=0;i<n;i++){arr[i]=0;}
while(k>=0)
{
int g=0;
for(i=0;i<n/2;i++)
{
if(qtr[i]!=qtr[n-i-1])
{
arr[i]=1;
arr[n-i-1]=1;
qtr[i]=qtr[n-i-1];
k--;
g=1;
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(k%2!=0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==1)
{
arr[i]=2;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k--;g=1;
break;
}
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(k%2==0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==0)
{
arr[i]=3;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k=k-2;g=1;
break;
}
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(g==1){printf("-1");return 0;}
}
printf("-1");return 0;
}
}
}

I need help to convert from infix to postfix in C

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()); } }

Next Palindrome SPOJ (PALIN)

While solving the NEXT PALINDROME question on SPOJ. I have considered for every possible case and wrote the code. But it is showing WA(Wrong Answer). I am not getting my mistake or any possible case where my code won't work.
Below is my code.
Please help me out.
Thank You.
#include<stdio.h>
#include<string.h>
char K[10000000];
int a[10000000];
int main()
{
int t,length,i,count,j,rem,carry;
scanf("%d",&t);
while(t--)
{
scanf("%s",K);
length=strlen(K);
count=0;
for(i=0;i<length;i++)
{
a[i]=K[i]-'0';
if(a[i]==9)
count++;
}
if(length==2 && count!=length)
{
if(a[0]<=a[1])
{
a[1]=++a[0];
}
else
{
a[1]=a[0];
}
for(i=0;i<length;i++)
{
printf("%d",a[i]);
}
printf("\n");
continue;
}
if(count==length)
{
if(length==1)
{
printf("11\n");
continue;
}
memset(a,0,sizeof(a));
a[0]=10;
a[length-1]=1;
for(i=0;i<length;i++)
{
printf("%d",a[i]);
}
printf("\n");
continue;
}
i=0;
j=length-1;
count=0;
while(j-i!=1 && j-i!=0)
{
if(a[i]>a[j])
{
a[j--]=a[i++];
count++;
}
else
{
if(a[i]<a[j])
count--;
a[j--]=a[i++];
}
}
if(count==0)
{
rem=a[i]+1;
a[i]=rem%10;
a[j++]=a[i--];
carry=rem/10;
while(carry!=0)
{
rem=a[i]+1;
a[i]=rem%10;
a[j++]=a[i--];
carry=rem/10;
if(i<0&&j>length-1)
break;
}
}
else
{
if(a[i]<a[j] || count<0)
{
rem=a[i]+1;
a[i]=rem%10;
a[j++]=a[i--];
carry=rem/10;
while(carry!=0)
{
rem=a[i]+1;
a[i]=rem%10;
a[j++]=a[i--];
carry=rem/10;
if(i<0&&j>length-1)
break;
}
}
else
a[j]=a[i];
}
for(i=0;i<length;i++)
{
printf("%d",a[i]);
}
printf("\n");
}
return 0;
}

code solves 9*9 sudoku.works for medium level but gives segmentation fault for hard ones due to excessive recursion..where's the problem

# include <stdio.h>
int check(int a,int b);
int check1(int a,int b,int c,int d);
void recursive(int x,int pos[82]);
void scaledown(int pos[82]);
int pos[82];
int q=1;
long c=0;
int ch[10]={0,1,2,3,4,5,6,7,8,9};
int ar[10][10]= {{0,0,0,0,0,0,0,0,0,0},
{0,8,6,0,0,2,0,0,0,0},
{0,0,0,0,7,0,0,0,5,9},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,6,0,8,0,0},
{0,0,4,0,0,0,0,0,0,0},
{0,0,0,5,3,0,0,0,0,7},
{0,0,0,0,0,0,0,0,0,0},
{0,0,2,0,0,0,0,6,0,0},
{0,0,0,7,5,0,9,0,0,0}};
int size;
void main()
{
int i,j,k=1,a;
int pos[82];
printf("WELCOME TO THE ULTIMATE SUDOKU SOLVER");
printf("\n\n\n");
for(i=1;i<=9;i++)
{
for(j=1;j<=9;j++)
{
if(ar[i][j]==0)
{
pos[k]=(10*i)+j;
k+=1;
}
printf("%d",ar[i][j]);
printf(" ");
}
printf("\n");
}
size=k-1;
printf("\n");
scaledown(pos);
k=1;
for(i=1;i<=9;i++)
{
for(j=1;j<=9;j++)
{
if(ar[i][j]==0)
{
pos[k]=(10*i)+j;
k+=1;
}
}
}
size=k-1;
recursive(q,pos);
for(i=1;i<=9;i++)
{
for(j=1;j<=9;j++)
{
printf("%d",ar[i][j]);
printf(" ");
}
printf("\n");
}
printf("%d",c);
}
void recursive(int x,int p[82])
{
c++;
printf("%d",c);
printf("\n");
ar[p[x]/10][p[x]%10]+=1;
if(ar[p[x]/10][p[x]%10]>9&&q<=size)
{
ar[p[x]/10][p[x]%10]=0;
q--;
recursive(q,p);
}
if(check(p[x]/10,p[x]%10)==1&&q<size)
{
q++;
recursive(q,p);
}
if(check(p[x]/10,p[x]%10)==0&&ar[p[x]/10][p[x]%10]<9&&q<=size)
{
recursive(q,p);
}
if(ar[p[x]/10][p[x]%10]==9&&check(p[x]/10,p[x]%10)==0&&q<=size)
{
ar[p[x]/10][p[x]%10]=0;
q--;
recursive(q,p);
}
if(q==size&&check(p[x]/10,p[x]%10)==1){}
}
int check1(int a,int b,int c,int d)
{
int i,j;
for(i=c;i<=(c+2);i++)
{
for(j=d;j<=(d+2);j++)
{
if(i==a&&j==b){}
else
{
if(ar[i][j]==ar[a][b])
{
return 0;
}
}
}
}
return 1;
}
int check(int a,int b)
{
int i,j;
for(i=1;i<=9;i++)
{
if(i!=b)
{
if(ar[a][i]==ar[a][b])
{
return 0;
}
}
if(i!=a)
{
if(ar[i][b]==ar[a][b])
{
return 0;
}
}
}
if(a<4&&b<4)
{
if(check1(a,b,1,1)==0)
{
return 0;
}
}
if(a<4&&b>3&&b<7)
{
if(check1(a,b,1,4)==0)
{
return 0;
}
}
if(a<4&&b>6)
{
if(check1(a,b,1,7)==0)
{
return 0;
}
}
if(a>3&&a<7&&b<4)
{
if(check1(a,b,4,1)==0)
{
return 0;
}
}
if(a>3&&a<7&&b>3&&b<7)
{
if(check1(a,b,4,4)==0)
{
return 0;
}
}
if(a>3&&a<7&&b>6)
{
if(check1(a,b,4,7)==0)
{
return 0;
}
}
if(a>6&&b<4)
{
if(check1(a,b,7,1)==0)
{
return 0;
}
}
if(a>6&&b>3&&b<7)
{
if(check1(a,b,7,4)==0)
{
return 0;
}
}
if(a>6&&b>6)
{
if(check1(a,b,7,7)==0)
{
return 0;
}
}
return 1;
}
void scaledown(int p[82])
{
int i,j,w,count=0;
for(i=1;i<=size;i++)
{
for(j=1;j<=9;j++)
{
ar[p[i]/10][p[i]%10]=ch[j];
if(check(p[i]/10,p[i]%10)==0)
{
ch[j]=0;
count+=1;
}
}
if(count==8)
{
for(w=1;w<=9;w++)
{
if(ch[w]!=0)
{
ar[p[i]/10][p[i]%10]=ch[w];
}
}
}
else
{
ar[p[i]/10][p[i]%10]=0;
}
for(w=1;w<=9;w++)
{
ch[w]=w;
}
count=0;
}
}
probably, max recursively call.
You most likely busted the stack. Just a couple unrelated notes: You need to make your code simpler by separating the work into individual functions. You may have been able to keep track of everything in this small program, but if you continue to write code you will have to think about code layout/design/usability.
To solve sudoku puzzles without overflowing the stack, I suggest looking into the solution using constraint solvers. Here's something to get you started.

Resources