# I can't see why my Sieve wont work. Everything looks right.

I know it's probably something small.  I"ve been toying with it for a while now.

I keep getting a stack overflow exception

#include <iostream>
#include <math.h>      //to use the sqrt and floor functions

int main()//start of main
{
double n = 50;
int arrayCount = 0;
int j;
int Array[100000];            //array to store data to be examined
int ArrayB[100000];            //array to house data of prime numbers
int ArrayC[100000];            //array to house data of all prime numbers that end with 3

double largest =0;
double test;

for(int p = 2; p <= n; p++)
{
Array[p];
std::cout<<"A"<<std::endl;

}//end of for loop
/*this part of the code populates the code with values of the type int from 2 to n
*/

double x = sqrt(n);
int y = (int)floor(x);
std::cout<<"B"<<std::endl;

for(int p=2; p<=y; p++)
{
std::cout<<"C"<<std::endl;
if(Array[p] !=0)      //searches through the array to perform on all values that are not zero
{
j=p*p;            //starts with 4 (2&3 are prime) and then runs through the while loop.
while(j<=n)
{
Array[j] = 0;      //assigns the value zero to all the numbers passed through the while loop
j = j+p;
std::cout<<"D"<<std::endl;
}//end of while
}//end of if
}//end of for loop

int i = 0;
for (int p =0; p<=n;p++)
{
if(Array[p] !=0)
{
ArrayB[i] = Array[p];            //assigns value of Array that are not zero to ArrayB
arrayCount ++;
}//end of if

}//end of for loop

//used to print out the prime numbers
std::cout <<"These are the prime numbers: "<<std::endl;
for(int count =0; count <= arrayCount -2;count++)
{
std::cout << ArrayB[count];
}

int number;// integer used to compare
for(int count =0; count <= arrayCount -2; count++)
{
number = ArrayB[count];

if ((number % 10) == 3)
{
ArrayC[number];

}//end of if

}//end of for loop

//used to print all the prime numbers that end with 3
std::cout <<"These are the prime numbers that end with 3: "<<std::endl;
for(int count =0; count <= arrayCount-2;count++)
{
std::cout << ArrayC[count];
}//end of if

//used to print the largest prime number
std::cout<<"This is the largest prime number"<<std::endl;
for(int count =0; count <= arrayCount -2; count++)
{
test = ArrayB[count];
if(test > largest)
{
largest = test;

}//end of if

}//end of for loop

}//end of main
LVL 1
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
If you all are wondering why I have the A, B, C, D, etc... I was trying to see how far in the loop I was going.
0
Commented:
Replace arrays allocated on stack with dynamic arrays:

int* Array = new int[100000];          //array to store data to be examined
int* ArrayB = new int[100000];          //array to house data of prime numbers
int* ArrayC = new int[100000];          //array to house data of all prime numbers that end with 3

Release them in the end of the program. Process has more memory on the heap than on the stack.
0
Author Commented:
I've never worked with dynamic arrays.  Do you know of a quick tutorial that will teach me what I need to know to get it to work.
0
Commented:
In your case you need to replace array lines in the beginning of main function as written in my firts post, and add the following lines before end of main function:

delete[] Array;
delete[] ArrayB;
delete[] ArrayC;
0
Commented:
0
Commented:
0
Author Commented:
After I run, it shoots out a memory location, basically the same memory location over and over again.  I would cut and paste, but I'm unable to.

0
Author Commented:
From what I read in the first link, I should be able to use the dynamic array the same way as a static.  Did I misread that?
0
Commented:
int array[10];
int* array = new int[10];

In both cases array has int* type and can be handled by the same way in other program parts. The difference is that dynamic array must be released in the end. Dynamic array is more flexible: staatic array must have constant size known at compile time, and dynamic array can have variable size calculated at run time.
0
Commented:
Hi,

Since this program seems familiar, I think you have to change
for(int p = 2; p <= n; p++)
{
Array[p];
std::cout<<"A"<<std::endl;

}//end of for loop

to

for(int p = 2; p <= n; p++)
{
Array[p] = p;
std::cout<<"A"<<std::endl;

}//end of for loop
0
Commented:
Aslo here

int i = 0;
for (int p =0; p<=n;p++)
{
if(Array[p] !=0)
{
ArrayB[i] = Array[p];          //assigns value of Array that are not zero to ArrayB
arrayCount ++;
}//end of if

}//end of for loop

you have to replace arrayCount ++ with i++
0
Author Commented:
Thanks a lot guys.  Those links were awesome.  Especially the second one (for Dynamic).  I plan on frequenting the first a little more to brush up on everything.

Sounds like it's best to use Dynamic arrays all the time.  I still haven't been able to get my code to work.  I'm wondering though, is my code pointing at a location in memory?
I keep getting

-842150451- 8 times
"                             "
These are the prime numbers that end with 3
-842150451- 8 times
"                             "
The largest prime number is:

i define an integer to keep track of the amount of variables put into my ArrayC (Array that held all my prime numbers ending in 3).

it's been a while, but isn't there something like. (int x =0; x <= Array[].length(); x++)  - just wondering
0
Commented:
And apart from my 2 above postings, replace the rest with

//used to print out the prime numbers
arrayCount = i;
std::cout <<"These are the prime numbers: "<<std::endl;
for(int count =0; count <= arrayCount -1;count++)
{
std::cout << ArrayB[count];
}

int number;// integer used to compare
int j = 0;
for(int count =0; count <= arrayCount -1; count++)
{
number = ArrayB[count];

if ((number % 10) == 3)
{
ArrayC[j] = number;
j++;
}//end of if

}//end of for loop

//used to print all the prime numbers that end with 3
std::cout <<"These are the prime numbers that end with 3: "<<std::endl;
for(int count =0; count <= j-1;count++)
{
std::cout << ArrayC[count];
}//end of if

//used to print the largest prime number
std::cout<<"This is the largest prime number"<<std::endl;
for(int count =0; count <= arrayCount -1; count++)
{
test = ArrayB[count];
if(test > largest)
{
largest = test;

}//end of if

}//end of for loop

}//end of main
0
Author Commented:
here are the changes I just made:  I think it's what you brought up.  I saw that i.  Man I can't believe I missed that.  I used arrayBCount instead.  I don't think there should be a difference though.  I also tried to clean up the bottom, but it still didn't seem right.

#include <iostream>
#include <math.h>      //to use the sqrt and floor functions

int main()//start of main
{
double n = 50;
int arrayCount = 0;
int arrayBCount = 0;
int j;
int* Array = new int[100];            //array to store data to be examined
int* ArrayB= new int[100];            //array to house data of prime numbers
int* ArrayC= new int[100];            //array to house data of all prime numbers that end with 3

/*int Array[100000];
int ArrayB[100000];
int ArrayC[100000];
*/

double largest =0;
double test;

for(int p = 2; p <= n; p++)
{
Array[p] = p;
std::cout<<"A"<<std::endl;

}//end of for loop
/*this part of the code populates the code with values of the type int from 2 to n
*/

double x = sqrt(n);
int y = (int)floor(x);
std::cout<<"B"<<std::endl;

for(int p=2; p<=y; p++)
{
std::cout<<"C"<<std::endl;
if(Array[p] !=0)      //searches through the array to perform on all values that are not zero
{
j=p*p;            //starts with 4 (2&3 are prime) and then runs through the while loop.
while(j<=n)
{
Array[j] = 0;      //assigns the value zero to all the numbers passed through the while loop
j = j+p;
std::cout<<"D"<<std::endl;
}//end of while
}//end of if
}//end of for loop

int i = 0;
for (int p =0; p<=n;p++)
{
if(Array[p] !=0)
{
ArrayB[i] = Array[p];            //assigns value of Array that are not zero to ArrayB
arrayBCount ++;
i++;
}//end of if

}//end of for loop

//used to print out the prime numbers
std::cout <<"These are the prime numbers: "<<std::endl;
for(int count =0; count <= arrayBCount -1;count++)
{
std::cout << ArrayB[count];
}

int number;// int used to compare
for(int count =0; count < arrayBCount -1; count++)
{
number = ArrayB[count];

if ((number % 10) == 3)
{
ArrayC[number];

}//end of if

}//end of for loop

//used to print all the prime numbers that end with 3
std::cout <<"These are the prime numbers that end with 3: "<<std::endl;
for(int count =0; count < arrayCount-1;count++)
{
std::cout << " " << ArrayC[count]<<" "<<std::endl;
}//end of if

//used to print the largest prime number
std::cout<<"This is the largest prime number"<<std::endl;
for(int count =0; count < arrayCount -1; count++)
{
test = ArrayB[count];
if(test > largest)
{
largest = test;

}//end of if

}//end of for loop
std::cout<<" "<< largest <<" "<<std::endl;
delete[] Array;
delete[] ArrayB;
delete[] ArrayC;

}//end of main

0
Author Commented:
Is this legal?  It seems funny to me.

ArrayB[i] = Array[p];

0
Commented:
yes, ArrayB[i] = Array[p];  is legal and correct.
but you still have not replaced all thing that I've written above.
if you use < instead of <= then you don't need the "-1" in  "count < arrayBCount -1"

Also use the additional k variable
int k=0;

ArrayC[k] = number;
k++;

Also use count < k when you print the numbers ending with 3

Again use count < arrayBCount (without "-1") when trying to find the largest number.
But in fact you do not need to search for the prime number with a loop since the biggest prime number will be
ArrayB[arrayCount-1]

Here's the correct source code:

int number;// int used to compare
int k=0;
for(int count =0; count < arrayBCount; count++)
{
number = ArrayB[count];

if ((number % 10) == 3)
{
ArrayC[k] = number;
k++;
}//end of if

}//end of for loop

//used to print all the prime numbers that end with 3
std::cout <<"These are the prime numbers that end with 3: "<<std::endl;
for(int count =0; count < k;count++)
{
std::cout << " " << ArrayC[count]<<" "<<std::endl;
}//end of if

//used to print the largest prime number
std::cout<<"This is the largest prime number"<<std::endl;
for(int count =0; count < arrayBCount; count++)
{
test = ArrayB[count];
if(test > largest)
{
largest = test;

}//end of if

}//end of for loop
std::cout<<" "<< largest <<" "<<std::endl;
delete[] Array;
delete[] ArrayB;
delete[] ArrayC;

}//end of main
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
thanks a lot guys.  I got it to work using a lot of what you all said.  Welkin Maze, you were extremely helpful.
0
Commented:
I'm glad you've got it working. :)
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.