this code works completely fine in my pc while i compile it with codeblocks 10.05. the code doesn’t give an error or warning whatsoever! But while submitting it to an online judge community of coding they reply me with an compilation error!
Here is my code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int j=1;
while(j=1){
int x,y,i,j,num,count=0,p,k;
for(;;){
printf("enter two integers. they must not be equal and must be between 1 and 100000\n");
scanf("%d%d",&i,&j);
if(i>=1 && i<100000 && j>=1 && j<100000 && i!=j){
break;
}
else{
printf("try the whole process again\n");
}
}
if(i>j){
x=i;
y=j;
}
else{
x=j;
y=i;
}//making x always greater than y
int *cyclelength=(int *)malloc(5000*sizeof(int));
if (NULL==cyclelength){
printf("process aborted");
return 0;
}
else{
/*solution part for the range of number. and solution for each number put into cyclelength.*/
num=y;
while(num<=x){
p=1;
k=num;
while(k!=1){
if(k%2==0)
k=k/2;
else
k=3*k+1;
p+=1;
}
cyclelength[count]=p;
num+=1;
count+=1;
}
int c=0;
int max=cyclelength[c];
for(c=0;c<x-y-1;c+=1){
if(max<cyclelength[c+1]){
max=cyclelength[c+1];
}
}
free(cyclelength);
cyclelength = NULL;
printf("%d,%d,%d\n",i,j,max);
}
}
}
and when i try to submit it in an online community under ANSI C 4.1.2 – GNU C Compiler with options: -lm -lcrypt -O2 -pipe -ansi -DONLINE_JUDGE this format. they send me a compilation error message!
Our compiler was not able to properly proccess your submitted code. This is the error returned:
code.c: In function 'main':
code.c:25:10: error: expected expression before '/' token
code.c:27:19: error: 'cyclelength' undeclared (first use in this function)
code.c:27:19: note: each undeclared identifier is reported only once for each function it appears in
code.c:10:18: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result
why am i getting this?? where is my fault?? Is the coding format not matching?? I am a beginner!
with the -ansi option the errors are ANSI compliance failures:
should be
In the line 26; you can’t declare cyclelength here; it needs to be at the top of the function (at line 5)
The scanf problem is a warning because not checking the return value of scanf assumes that it has succeeded; so you should do something like:
of course you’ll need to declare
number_readat the top of the function