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.
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);
}
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);
}
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.
#include <iostream>
#include <cmath>
usingnamespace 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)
{
returnfalse;
break;
}
++c;
}
returntrue;
}
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.