I am writing a c++ program in which I have filled a large array of size 100,000 with sequential values from 1 to 100,000. Now I am trying to make this array filled with prime numbers only. I have a variable defined "mult" which starts at 2 and goes through the array and deletes every 2nd location (multiples of 2), and I want loop again to do the same for 3, 5, 7, etc. However I get an exception thrown and my pointer p is a very large number. I would appreciate any clues as to why this is happening. Here is my code:
// Fill up the array with values 1 - 100,000.
void generateBigArray(int *p, int size)
{
int count = 1;
for (int i = 0; i < size; i++)
{
*p = count++;
p = p + 1; // bump to the next location.
}
}
void deleteMults(int *p, int size)
{
int mult = 1;
while (mult <= size)
{
for (int i = mult; i < size; i = i + mult)
{
p = p + 1; // starting at 2.
mult = mult + 1;
while (*p != 0)
{
*p = 0;
p = p + mult;
}
mult = mult + 1;
}
}
}
int main()
{
// an array to hold prime numbers from 1 - 100,000
const int BIGSIZE = 10000;
int bigAry[BIGSIZE];
// a pointer for the prime number array.
int *bigptr;
bigptr = bigAry;
generateBigArray(bigptr, BIGSIZE);
deleteMults(bigptr, BIGSIZE);
return 0;
}