Can't find segmentation fault

I have been trying to code a program that will calculate the 1000 prime number. however when I compile my program I get a "segmentation fault(core dumped)" any advice about how to fix this problem would be greatly appreciated. Also as I am a programming novice I would appreciate any advice about ways I could improve my programming in general.

#include <iostream>

using namespace std;

int primestorage [999];

bool primecheck (int a, int b);

int main()
{
int numberbeingchecked, numberofprimes;
bool primeresult;
primestorage[0] = 2;
numberofprimes = 1;
numberbeingchecked = 3;
while (numberofprimes < 1000)
{
primeresult = primecheck(numberbeingchecked, numberofprimes);
if (primeresult == true)
{
primestorage[numberofprimes - 1] = numberbeingchecked;
numberofprimes++;
}
numberbeingchecked++;
}
cout << primestorage[999];
return 0;
}

bool primecheck (int a, int b)
{
bool result;
int c;
while(a % primestorage[c] != 0)
{
c++;
}
if(c == b)
{
result = true;
}
else
{
result = false;
}
return(result);
}

Thanks in advance.
In your primecheck(int, int) int c is created but not assigned a value and then you try to add a number to it. That is what is causing your error

Set it to 0 before doing anything with it
Last edited on
Thank you for the quick response. That fixed the segmentation fault but, now i am getting a floating point exception. I am very vague on what this means and how to fix it. So i would be very appreciative if you or anyone else could clarify what exactly this mean and how to fix. Here is the updated primecheck function

bool primecheck (int a, int b)
{
bool result;
int c = 0;
while(a % primestorage[c] != 0)
{
c++;
}
if(c == b)
{
result = true;
}
else
{
result = false;
}
return(result);
}

Again thank you so much.
Floating point exceptions occur when you are trying to divide by zero. It seems that this is excatly what you are doing. When an array is created, each element in the array is set to zero. When you try dividing numbers by each element in that array, you get floating point exceptions. You have to fill your prime storage with actual numbers.

E:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <cmath>

using namespace std;

int primestorage [999];

bool primecheck (int);

int main()
{
    int numberbeingchecked, numberofprimes;
    bool primeresult;
    numberofprimes = 1;
    
    primestorage[numberofprimes -1] = 2;
    ++numberofprimes;
    
    numberbeingchecked = 3;
    while (numberofprimes - 1 < 999)
//You said 1000 here, but you array can only take 998...the 999th value is the NULL terminating character '\0'
    {
        primeresult = primecheck(numberbeingchecked);
        if (primeresult)
        {
            primestorage[numberofprimes - 1] = numberbeingchecked;
            ++numberofprimes;
        }
        numberbeingchecked += 2;//Skip even numbers
    }
    for (int w = 0; w < 999; ++w)
        cout << primestorage[w] << " ";//use a for loop here
    cout <<endl;
    return 0;
}

bool primecheck (int a) //No need for b (int b))
{
    //bool result;
    int c = 0;
    int Faster = pow(a, 0.5);//Make the code faster by checking only up the sqrt of the number
    while(primestorage[c] <= Faster)
    {
        if (a % primestorage[c] == 0)
        {
            return false;
            break;
        }
        ++c;
    }
    
    return true;
}
Last edited on
I would suggest changing the names in primecheck to something meaningful. You don't make any attempt to ensure that c doesn't exceed the number of primes stored in primestorage (if it does, you'll be dividing by 0.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool primecheck (int candidate, int primesFound)
{
    int index = 0 ;
    bool stillCandidate = true ;

    while ( stillCandidate  &&  index < primesFound )
    {
        if ( (candidate % primestorage[index]) == 0 )
            stillCandidate = false ;
        ++index ;
    }

    return stillCandidate ;
}


Also, primestorage[numberofprimes - 1] = numberbeingchecked; is wrong. You want to add a number to the array, not change the last number stored.
Thank you very much for your help. My program now executes and the 1000 prime number is 7919 confirmed at http://primes.utm.edu/lists/small/1000.txt . Thanks again for so much help.
Topic archived. No new replies allowed.