Solved

Posted on 2006-04-26

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

18 Comments

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.

delete[] Array;

delete[] ArrayB;

delete[] ArrayC;

See Part 13: Dynamic Object Creation.

For tutorial check this link

http://www.cplusplus.com/doc/tutorial/dynamic.html

http://www.cplusplus.com/d

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.

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

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++

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

//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

#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

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

Title | # Comments | Views | Activity |
---|---|---|---|

c++ vs2003 loging the program jumps from function to function | 4 | 62 | |

c++ mixing int and strings in multi array | 8 | 58 | |

c++ array loading from DB inheriting previous data when empty | 5 | 54 | |

Modify static variables within a class in VC++ | 3 | 85 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**15** Experts available now in Live!