Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++ Floating point exception


ghost's Avatar
0 0

I'm trying to do problem 10 on project euler (sum of all primes below 2 million) and my program outputs nothing more than "Floating point exception" when run, and I can't figure out why. here's the code:

#include <vector>
#include <iostream>
using namespace std;

int l=1; //length of vector
vector<double> Primes(2000000,0.0);
int main(){
	int n=2;
	int i=2;
	Primes[0]=2.0; //prevents division by 0  v
	while(n<2000000){
		int cur=0; // current vector item
		bool isPrime=true;
		for(int iter=0;iter<l;iter++){
//commenting out the following line fixes the problem, but
			if(i%(int)Primes[iter]==0){//it need this line
				isPrime=false;
				break;
			}
		}
		if(isPrime){
			Primes[cur]=double(i);
			n=i;
			++l;
		}
		++i;
		++cur;
	}
	double solution=0;
	for(int i=0;i<l;i++){
		cout << Primes[i] << endl;
		solution+=Primes[i];
	}
	return 0;
}

I'm sure this algorithm is messy, but it's better than what I was using before (Primes.push_back(i)) I based it loosely on the working program I made to find n primes.


ghost's Avatar
0 0

MoshBat wrote: Before we go any further, this looks to me like some homework. For your sake, is this correct?

Project Euler is a bunch of challenge type things usually quiet interesting/challenging. But I guess a teach could have set it/ used the project as the basis for the homework.


ynori7's Avatar
Future Emperor of Earth
0 0

Why is your list of primes a double? By definition prime numbers are integers. With this you just have an ass load of extra typecasts that you wouldn't need if you had made an integer vector.


stealth-'s Avatar
Ninja Extreme
0 0

MoshBat wrote: [quote]wolfmankurd wrote: [quote]MoshBat wrote: Before we go any further, this looks to me like some homework. For your sake, is this correct?

Project Euler is a bunch of challenge type things usually quiet interesting/challenging. But I guess a teach could have set it/ used the project as the basis for the homework.[/quote] I refuse to help people with homework. But as it doesn't appear to be, I might give the thing a quick look.[/quote]

Erm, why? You think they should figure it out on their own, or is this just another way of rebelling against the establishment? :P

Edit: @OP, I'd help you, but unfortunately I don't know any C. Well I do, but I'm in the process of learning.


ghost's Avatar
0 0

OP, I'm going to be honest here, there's so much shit with this program that it hurts trying to figure out your intentions with it.

  1. What ynori said, primes are by definition ints, stop with the casting.
  2. No proper casting. For C++ please use the C++ type casting implemented to make casting clearer and safer, read here for more info: http://www.acm.org/crossroads/xrds3-1/ovp3-1.html
  3. No matter how much you increment something, if it's set to 0 at the start of a loop, it'll remain 0.
  4. n is unnecessary.
  5. Incrementation at wrong place.
  6. Will calculate one more prime than is necessary (see 4).

I'm sure there's more in there but it hurt my eyes; fix these issues and your program will work.


korg's Avatar
Admin from hell
0 0

Too be honest I did this a while back and this very same project (or similar) has been around for years. I only got about 6 lines in on the original code and stopped. @OP if you need some help PM me, I still have my script lying around.

EDIT: Just googled "c++ prime numbers 2000000" and found a shit load of info, Check it out on your own. :angry:


ghost's Avatar
0 0

korg wrote: Too be honest I did this a while back and this very same project (or similar) has been around for years. That was kind of covered by the very first line of the OP:

I'm trying to do problem 10 on project euler I'm sure googling the answer will work for him, but it'll really not be worth anything if he doesn't at least try to write the code himself.


korg's Avatar
Admin from hell
0 0

COM wrote: I'm sure googling the answer will work for him, but it'll really not be worth anything if he doesn't at least try to write the code himself.

Exactly, but by finding examples on how people have done it can give him a better idea on how functions work so he can learn and hopefully optimize it from there.


ynori7's Avatar
Future Emperor of Earth
0 0

Modularization will make it easier. You seem to have a problem with over complicating your code. Try breaking it up and just make a function that determines if a number is prime. Then make a program that uses that function.


ghost's Avatar
0 0

MoshBat wrote: I refuse to help people with homework. But as it doesn't appear to be, I might give the thing a quick look.

Makes sense. No point being set something if you don't intend to do it yourself.

Also stealth it's C++


stealth-'s Avatar
Ninja Extreme
0 0

wolfmankurd wrote: [quote]MoshBat wrote: I refuse to help people with homework. But as it doesn't appear to be, I might give the thing a quick look.

Makes sense. No point being set something if you don't intend to do it yourself.

Also stealth it's C++[/quote]

Close Enough :P

Out of curiosity, though, what would be the easiest way to tell the difference, other than knowing C enough to recognize it's not C?

edit: I think I might have found it. In C files you include stdio, but in this one it's including iostream.


ghost's Avatar
0 0

stealth- wrote: [quote]wolfmankurd wrote: Also stealth it's C++

Close Enough :P

Out of curiosity, though, what would be the easiest way to tell the difference, other than knowing C enough to recognize it's not C?

edit: I think I might have found it. In C files you include stdio, but in this one it's including iostream.[/quote]

The title of the topic helps :D, most C is valid in C++, stdio could be included.

I think you probably have trouble differentiating cause if you're starting out you might not have seen much c++ code. Have a look at it and you'll notice the small and large/obvious differences (between GOOD) C++ and C.


stealth-'s Avatar
Ninja Extreme
0 0

Ah, yeah I see what you mean wolfman. Thanks for the explanation.

@ moshbat: I think you misread my question, but thanks anyways ;)