warriorfan808
asked on
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
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
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.
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.
ASKER
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.smart2help.com/e-books/ticpp-2nd-ed-vol-one/Frames.html
See Part 13: Dynamic Object Creation.
See Part 13: Dynamic Object Creation.
For tutorial check this link
http://www.cplusplus.com/doc/tutorial/dynamic.html
http://www.cplusplus.com/doc/tutorial/dynamic.html
ASKER
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.
ASKER
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?
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.
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.
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
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
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++
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++
ASKER
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
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
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
//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
ASKER
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
#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
ASKER
Is this legal? It seems funny to me.
ArrayB[i] = Array[p];
ArrayB[i] = Array[p];
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks a lot guys. I got it to work using a lot of what you all said. Welkin Maze, you were extremely helpful.
I'm glad you've got it working. :)
ASKER