0

Whenever I try to compile the following code, I can enter the initial value I ask for but then nothing happens. Also, there is no build error that being picked up so i don't know why this is happening. Please help and thank you in advance.

#include <stdio.h>
#include <stdbool.h>
int n;
int allDaPrimes[1000];
int counter = 0;
bool isPrime(int number);
int i;

int main()
{
    printf("Please enter a numeric value now: ");
    scanf("%d", &n);
    for (i = 2; i <n; i++){
        isPrime(i);
        if(isPrime(i)) {
            allDaPrimes[counter] = i;
            counter++;
        }
        }


    for(i= 0; i==counter; i++){
        printf("%d", allDaPrimes[i]);
    }
    return 0;

   }

bool isPrime(int number) {
    for (i= 2; i <= number; i++){
        if(number % i == 0 && number != i){
            return false;
        }

    }
    return true;
}
6
  • I suppose that's because your isPrime function has a terrible complexity, so it runs forever. Try limiting to int(sqrt(number)+0.5) you'll have the same results, much faster. note: you're calling it twice in your loop,; making it even worse. Commented Mar 22, 2017 at 17:21
  • 1
    If it runs to the point you can enter a value, it's not a compiling problem, because your code compiled, linked and executed. Now it's a debugging problem, and for that you use a debugger to step through the code. Commented Mar 22, 2017 at 17:21
  • consider using a sieve algorithm instead of that. (lookup sieve of erathostenes) Commented Mar 22, 2017 at 17:22
  • 1
    which value are you entering? Entering 1 million creates a 10**12 loop. Commented Mar 22, 2017 at 17:23
  • goodness guys thanks so much for responding so quickly! Ill look into your suggestions and update you as soon as I can. Commented Mar 22, 2017 at 17:23

2 Answers 2

3

This is a logic error in your code, which means your code is doing exactly what you told it to, but it's not what you had in mind in your head.

The problem is this statement

for(i= 0; i==counter; i++){
    printf("%d", allDaPrimes[i]);
}

It starts with i=0 then runs while i==counter, which is probably not what you had in mind. You probably meant: i<counter

Sign up to request clarification or add additional context in comments.

8 Comments

Wow I am dumb! I just started learning how to code so I guess theres some serious learning pains. Thanks so much!
So i fixed that problem and made it "i<=counter" and the problem persists :(
Try inserting the number 11 as input and see if it works, if you're inputting huge numbers it may just be hanging since it's taking so long given your algorithm
Probably should have started with this but, I need the prime numbers up until (not including the number) and for them to be printed all on one line. Also I dont get why none of the values are being stored in my array
@Simba thanks so much man for working with me here. I think I am going to try and implement a sieve algorithm to clean up some of complexity
|
1

All your variables are declared globally rather than locally in the functions where they are used. This means that you're getting conflicts between the different functions.

Take i for example. In the loop in main, you start off with it set to 2. But isPrime() changes it to be n+1 because that is the last value it'll be after the loop in there finishes. But you call that twice so i ends up being n+2, so the main loop only runs once. And if you end up with any values in the array, it'll be n+2 if n+1 was a prime.

3 Comments

Good work man! In addition, was also true what stated in the other answer.
just turned all my "i" into "i, j, k ...etc" and it works!!!!! thanks so much Chris!
@PeterLouis whilst that undoubtedly "works", the correct way would be to put int i; at the top of the block they're used in. There's no problem with you re-using i in the second main loop as you're initialising it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.