Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#define size 20000000
int prim[size];
int i, zahl, zaehler, erg;
int sieve(int zahl, int prim[], int zaehler) {
if(zahl == 2000000)
return 1;
for(i=0; i<=zaehler; i++) {
erg = zahl%prim[i];
if(erg==0) {
zahl++;
return sieve(zahl, prim, zaehler);
}
}
zaehler++;
prim[zaehler]=zahl;
zahl++;
printf("%d\n", prim[zaehler]);
return sieve(zahl, prim, zaehler);
}
int main(){
zaehler = 0;
zahl = 2;
for(i=0;i<size;i++)
prim[i]=2;
sieve(zahl, prim, zaehler);
}
When trying to calculate prime numbers, when i run this code, it always crushes at the number 64901.
What might be the problem?
Ironically, this is literally a stack overflow due to recursion. You can make your stack large (which will only delay the issue), or change from a recursive solution to an iterative one.
(and for what it's worth, some debuggers won't be able to help you in this situation. And it's very difficult to beginners in C to understand what is going wrong until the first time they hit this problem. So congrats! You're leveling up in C)
A cheap way to verify it's indeed a stack overflow is to create extra memory on your stack in the recursive function and see if the number it crashes on changes from 64901. My guess is if you put like char dummy[2048] in there, it will crash much sooner.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm creating a code where someone enters the amount of people eating a cut from and from there I can figure out how many pieces I can have in one pizza.I'm having trouble and don't know how to fix my error.
#include <stdio.h>
int Cuts(int n)
{
int max = n*2;
return m;
}
int main()
{
int m;
m = Cuts();
printf("P1:%d\n" , m);
}
Your CutYourPizza function is written to require one integer argument (called n), but when you invoked that function on the line max = CutYourPizza(); you did not supply any argument.
For example, if you wanted to supply the number 10 as an argument, then you could have written max = CutYourPizza(10); with the argument 10 inside the parentheses.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am not finding the correct output of this program.It giving run time error.
#include <stdio.h>
int main()
{
int c = 5, no = 10;
do {
no /= c;
} while(c--);
printf ("%d\n", no);
return 0;
}
It's division by zero. Since you are using a post-decrement in your loop counter c, it is becoming 0 in the last iteration.
Now that you know the reason for the run time error from the answer by #EugeneSh, here's how you can fix it.
do {
no /= c;
} while(--c); // Use pre-increment instead of post-increment.
In addition the all these answer above I just want to say it's better to check whether a number is zero before division -
#include <stdio.h>
int main()
{
int c = 5, no = 10;
do {
if(c!=0){
no /= c;
}
} while(c--);
printf ("%d\n", no);
return 0;
}
This will prevent these kind of runtime error.
Hope it will helps.
Thanks a lot.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
i cant get the following code to run, ie it dosent give out any output
the objective is to find the sum of all primes below 2 million,
#include <stdio.h>
#include <math.h>
int is_prime(long long int i)
{
long long int n;
if(i==2)
return 1;
for(n=2;n<=sqrt(i);n++)
if(i%n==0)
return 0;
return 1;
}
int main()
{
long long int s=0,i=2;
for(i<2000000;i++;)
{
if(is_prime(i))
s=s+i;
}
printf("sum: %lli",s);
return 0;
}
You're using the for loop wrong. A for loop looks like this:
for(initialization; test expression; update)
But you wrote
for(i<2000000;i++;)
which should be
for(;i<2000000;i++)
ie, skip initialization, on each iteration test for i<2000000 and increment.
Your problem is that you are essentially factorising every integer up to two million for this. It's not that your code isn't working, it just is running very very slowly. If you attach a debugger to it you will most likely be at line
if(i%n==0)
most of the time.
Implement the Sieve of Eratostenes. Will be the most effective here.
Also your loop for(i<2000000;i++;) will run for 2^64 cycles.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
i wrote the following code for the euler's project problem 10. Although am getting the correct numbers of prime numbers(as checked by the code given in a similar question) but the sum output is coming incorrect
the code is
#include<stdio.h>
#include<stdbool.h>
#define upper 2000000
int main(void)
{
bool prime[upper];
long long sum=0, i,k;
for(i=0;i<upper;i++)
{
prime[i]=true;
}
for(i=2;i<upper;i++)
{
if(prime[i])
{
for(k=2;i*k<upper;k++)
{
prime[k*i]=false;
}
}
}
long count=0;
for(i=2;i<upper;i++)
{
if(prime[i])
{
count++;
sum +=i;
}
}
printf("%d %d",count,sum);
return 0;
}
printf("%d %d",count,sum);
is your problem. The sum gets calculated correctly but you use the wrong format string. Check the printf format, especially what you need to output a long long.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
This is related to the "Project Euler Problem 14"
As I think, my logic and the code is good enough,but it may take few seconds to give the answer.
But when i run this code, the program stops when "starting_number" is around 103152(i can't remember the number exactly).
Can anybody please have a look at this code and, tell me where and what's wrong with this code.
Here's the code :
#include<stdio.h>
int starting_number;
int number_of_terms;
int j=0,k=0;
int term;
int main(){
for(starting_number=2;starting_number<1000000;starting_number++){
term = starting_number;
number_of_terms = 1;
while(1){
{
if(term%2==0){
term = term/2;
number_of_terms++;
} else if(term%2!=0){
term = 3*term + 1;
number_of_terms++;
}
}
if(term == 1) break;
}
if(j>=number_of_terms) //finding which chain is longer
j=j;
else if(j< number_of_terms) {
j= number_of_terms;
k=starting_number;
}
printf("\n%d",starting_number);
}
printf("\n%d(%d)\n",k,j);
return 0;
}
This one's mildly tricky, but your problem is here:
if(term == 1) break;
If the variable term becomes very large (as it can easily do) then it can overflow the int datatype.
When this happens term becomes negative. The C language modulus of a negative odd number is itself negative. Therefore, the end condition for your loop is never met.
Solve this problem by using a larger data type such as unsigned long long.
A less convoluted version of your code would appear as follows. Note that I have eliminated the global variables (those outside of your main function) because global variables are evil. I've replaced your infinite while-loop with a loop that uses an end condition. I've reduced duplication of code within the while-loop. I've eliminated the j=j case. Since printf is a slow function to run, I've commented out the prinft you had in the for loop, which improves the run-time significantly.
#include <stdio.h>
int main(){
int number_of_terms;
unsigned long long term;
int j=0,k=0;
for(int starting_number=2;starting_number<1000000;++starting_number){
term = starting_number;
number_of_terms = 1;
while(term!=1){
if(term%2==0)
term /= 2;
else
term = 3*term + 1;
number_of_terms++;
}
if(j<number_of_terms){ //finding which chain is longer
j = number_of_terms;
k = starting_number;
}
//printf("\n%d",starting_number);
}
printf("\n%d(%d)\n",k,j);
return 0;
}
And, indeed, using unsigned long long solves the problem.