I am currently working on a Project Euler problem involving finding primes.
I am coding in C++
The error I get is: "The program '[5688] Current.exe' has exited with code -1073740791 (0xc0000409)."
I am currently using Visual Studio Express 2013
( The code I have is not complete but here is what I have so far )
[code]
#include <iostream>
#include <conio.h>
int main(){
bool prime[13196];
int smallPrime[4] = { 2, 3, 5, 7 };
for (int set = 0; set < 13196; set++){
prime[set] = true;
}
for (int x = 0; x < 4; x++){
for (int y = 2; y < 13196; y++){
int temp = smallPrime[x] * y;
prime[temp] = false;
}
}
std::cout << prime[2] << " " << prime[13194];
for (int z = 0; z < 13196; z++){
if (prime[z] == true){
std::cout << z << "\n";
}
}
_getch();
return 0;
}
[/code]
I know everything works through trial an error other than this portion :
[code]
for (int x = 0; x < 4; x++){
for (int y = 2; y < 13196; y++){
int temp = smallPrime[x] * y; //specifically this line, and the one below it (Which marks all multiples of two as false in the boolean array "prime"
prime[temp] = false;
}
}
[/code]
What really confuses me, is it successfully completes the action I am asking it to do, then crashes. I truly have no idea what the issue is.
(This is not my final code, so I am specifically looking for advice on how to get rid of this errorcode whilst still accomplishing what I am trying to do.
Since it is C++ you should use <iostream> instead of <conio.h>
I am assuming this is homework for a CS course so I am not going to give you the answer. Set a breakpoint on the line you think you are having a problem with and run the program in debug mode stepping through the 'for' loop and look for values of temp that may cause a *cough* array access out of bounds.
your ''y'' can go to 13196, but smallprime is equal 2 at the minimum.
so when your ''y'' goes over 6598, temp goes over 13196, because 6599 times 2 = 13198.
your array named prime has 13196 ''positions'' and so when temp is higher then that, your program tries to access memory that it hasn't got permission to do so and crashes.
-snip misread dates-
Sorry, you need to Log In to post a reply to this thread.