This is the code for a problem on codechef.
#include<stdio.h>
inline int chkIsDiv(int n1, char* ptr)
{
int i=0, b=0;
while(ptr[i] != '\0')
{
b=b*10+(ptr[i]-48);
b%=n1;
i++;
}
if(b==0)
return 1;
return 0;
}
int main()
{
int t, a, b=0, i;
char c[252];
scanf("%d",&t);
while(t>0)
{
scanf("%d",&a);
i=0;
getchar();
while((c[i++]=getchar()) !='\n');
c[i-1]='\0';
if(a!=0 && chkIsDiv(a,c)) printf("%d",a);
else if(a==0) {
i=0;
while(c[i] !='\0') printf("%d",c[i++]-48);
}
else
{
for(i=a-1; i>=1; i--)
{
if(a%i==0) {
if(chkIsDiv(i,c)) {
printf("%d",i);
break;
}
}
}
}
printf("\n");
t--;
}
//getch();
return 0;
}
The problem is when I run the above code on ideone, it compiles the code successfully, but when I put input test cases, it gives segmentation fault (SIGSEGV) Runtime Error.
My submission link on ideone: http://ideone.com/qGclvK
Similarly, on codechef when I submit my problem it gives same error.(I guess since both uses the same compiler from SPOJ).
But when I run the same code on my machine it works fine with every input condition specified in the problem and also for corner cases. The code is running fine in both windows and linux. And also I believe the algorithm I used is correct.
I used Dev-C++ default compiler in windows and gcc in linux.
I know the error is occurring due to some invalid memory reference, but i'm not able to find where the problem is, which statement is causing problem, as it is running fine on my system.
Can anyone help me with this, I'm kind of beginner??
[SOLVED] #thank_to_MayankJain.
Got it solved. The problem was that I was assuming a '\n' at the end of every line, but in this case the last line will not contain any '\n' so I tested for EOF marker now, and works fine.
Here's the modified code http://ideone.com/qGclvK
Since feature requests to mark a comment as an answer remain declined, I copy the above solution here.
#MayankJain OK, solved now thanx. I was so dumb, didn't thought about it. The inputs are given from file and the last line will not contain any '\n'. Therefore I checked for EOF marker in last case. – abhishekkannojia
Related
I am new in C and tried checking the loop condition as to find on the internet, but I get this error I am not able to solve (no other questions/answers were helpful):
void main() {
char* insert = malloc(30);
printf("Insert a Molecular Formula:\n");
gets(insert);
if (insert) {
for (int i = 0; insert[i] != '\0'; i++) {
}
} }
I get the error 6011 in VS inside the for-loop when checking insert[i] != '\0'.
I haven't found a good fix, I have tried cheking return of malloc like if(!insert){ //above code here}
but this didn't help.
Thanks in advance.
Error C6011 is a warning, not an error, so your code will run, but it's not bad to handle these issues if Visual Studio is indicating them.
To get the warning to go away, fix your loop like so:
if (insert)
{
for (int i = 0; insert[i] != '\0'; i++) {
}
}
I am solving the following question in a hackerrank contest:
https://www.hackerrank.com/contests/moodys-analytics-2018-university-codesprint/challenges/meeting-profit-target
and my code is :
int main()
{
double need,ap,ep;
long nod;
int noq;
scanf("%d",&noq);
for(int i=0;i<noq;i++)
{
need=0;
scanf("%ld",&nod);
for(int j=0;j<nod;j++)
{
scanf("%lf",&ap);
scanf("%lf",&ep);
ep+=need;
if(ap-ep<0)
need+=ep-ap;
else need=0;
}
if(need==0)
printf("0\n");
else printf("1\n");
}
return 1;
}
18 out of 20 test cases are showing runtime error. Please help me out figuring out why this is happening.
main() is generally supposed to return 0 to indicate the program ran successfully. Returning any other value signals a failure, and Hackerrank apparently reports that as a runtime error.
I have my piece of code which is shown below
#include<cstdlib>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
int a;
printf("Please select a choice \n1.Enter New Artist\n2.List All Artist information\n3. Show sorted List of Artists\n4.Add Album to existing Artist\n5.Remove Album from existing Artist\n6.Update Artist info\n7.Search for Artist");
scanf("%d,&a");
if(a==1)
{
printf("no");
}
system("PAUSE");
return EXIT_SUCCESS;
}
On running the code, it displays the menu and if i input 1, it takes a few seconds before it crashes and displays the info
document1.exe has stopped working
How can I debug this problem. I am using dev c++ 4.9.9.2
Your scanf statement is wrong. You are not passing argument (pointer) to it.
Change
scanf("%d,&a");
to
scanf("%d",&a);
For a C solution - I am not certain
aside from previously mentioned
error in scanf formatting (which your compiler should have warned about),
which would have put the result.. well, god knows where on stack…
I'm not certain, but I suspect the compiled code would take the next location in ram as an address, and write there.
Borked stack == really, really, really hard to debug errors.
Since you are using a c++ compiler, if you can use c++, you may wish to consider evaluation of using some of the STL here.
I assume you will be terminating input with a CR
int main (int argv, char** argv]
{
int a;
std::string inputString;
std::cout <<"Please select a choice \n""1.Enter New Artist\n2.List All Artist information\n3. Show sorted List of Artists\n4.Add Album to existing Artist\n5.Remove Album from existing Artist\n6.Update Artist info\n7.Search for Artist";
std::getline(std::cin,inputString);
std::stringstream inputStream(inputString);
inputStream >> a; // could also have been parsed with std::stol, or strtol. - my preference due to error checking - what if your user entered 'byte me'?
if (a==1)
{
printf("no");
}
system("PAUSE");
return EXIT_SUCCESS;
}
In my code I define a step size "h" before a while loop. Somehow it seems to change by itself when I try to use it in the loop. If I define it inside the loop it seems to be ok, but the data I get doesn't seem to be right so I'm guessing the problem might be related.
Even when I print it at this location (see the printf in the code) the output is 3 values and I have no idea why. If you see anything else that's not related but seems wrong please tell me, as I said I'm getting unexpected values (it may just be my formulas).
int main()
{
FILE *f1;
f1 = fopen("Question2 part 3 solution.txt", "w");
double r0=0.05;
double dr0=-a*a*r0/sqrt(1+pow(a*a*r0,2)),h=0.01;
double k[4][3],x[]={dr0,z0,T0,r0},x1[]={0,0,0,0}, s=0;
int i,j;
while(s<=1)
{
//Runge-Kutta
for (j=0;j<4;j++)
{
for (i=0;i<4;i++)
{
if (j==0){k[i][0]=h*System(i,x[0],x[1],x[2],x[3]);}
if (j==1){k[i][1]=h*System(i,x[0]+k[0][0]/2.0,x[1]+k[1][0]/2.0,x[2]+k[2][0]/2.0,x[3]+k[3][0]/2.0);}
if (j==2){k[i][2]=h*System(i,x[0]+k[0][1]/2.0,x[1]+k[1][1]/2.0,x[2]+k[2][1]/2.0,x[3]+k[3][1]/2.0);}
if (j==3){k[i][3]=h*System(i,x[0]+k[0][2],x[1]+k[1][2],x[2]+k[2][2],x[3]+k[3][2]);}
}
}
for (i=0;i<4;i++)
{
x[i]=x[i]+(k[i][0]+2.0*k[i][1]+2.0*k[i][2]+k[i][3])/6.0;
}
printf("%8.3lf",h);
s+=h;
}
fclose(f1);
return(0);
}
double System(int i,double dr, double z, double T, double r)
{
//printf("%e\t%e\t%e\t%e\n",dr,z,T,r);
if (T==T0 && z==z0 && i==0) {return (-a*a*dr)*pow(1-dr*dr,3/2)/2.0;}
if (i==0 && T!=0){return (-a*a*r*(1-dr*dr)-dr*sqrt(1-dr*dr))/T;}
if (i==1){return (-sqrt(1-dr*dr));}
if (i==2){return (-a*a*r*dr+sqrt(1-dr*dr));}
if (i==3){return (dr);}
//if (i==3){return (-m2*l1*l2*B*theta1dt*theta2dt*sin(theta2-theta1)-l2*m2*g*B*sin(theta2));}
}
Thanks in advance!
See the declaration of k:
double k[4][3]
And then see this statement
k[i][3]=...
Here you write beyond the boundaries of the array, leading to undefined behavior.
You are overrunning the memory, variable k is defined as double k[4][3], but you are updating k[i][3]when j==3
I have tried so many things. Running from command line, running from cmd, running with /K, putting system("pause"); getchar(); getch(); before return 0 and I simply can't get it to run. I'm writing in Notepad++, compiling in Cygwin and the window appears blank for the split second it appears (according to my screenshot, it could have been taken too early). Basically I've tried anything I could Google myself to. So I figured it must be something wrong with my code that the debugger doesn't show.
#include <stdio.h>
int main()
{
float lt1, lt2, dmg, x;
lt1=10;
lt2=30;
while(lt2>dmg)
{
while(x>0 || lt2>dmg)
{
dmg=dmg+x*lt1;
x--;
return (dmg);
}
x=x+0.01;
return (x);
}
printf("Horde factor is: %f", x);
return 0;
}
I would appreciate any help I can get, and I hope you will bear over with my inexperience.
You have undefined behavior in your code.
When you declare a local variable without assigning anything to it, its value is indeterminate. Usage of this variable will be undefined behavior until you assign a value to it.
In this case it's the dmg and x variables that causes this problem.
Its because of these statement :
return (dmg); //this ends the code execution .. because you have returned something from main()
x=x+0.01;
return (x); // even this one is wrong
you are exiting the code there and never getting to the printf ..
there should only be one return in main() .. and at the end.
More problems with your code:
you don't initialise dmg and x , but you use them as parameters for while loop
float lt1, lt2, dmg, x; // dmg,x uninitialized
In the outer while loop .. its an infinite loop as you don't do anything to the parameters of that loop to get out of it.
Like I said above .. there should be only 1 return in main()
Maybe instead of returning you should look into break; ( i don't know if thats what you want or not as I don't understand your code )