C++ question

Started by: doog | Replies: 16 | Views: 1,032

doog
2

Posts: 177
Joined: Oct 2006
Rep: 10

View Profile
Feb 25, 2009 7:45 PM #363953
Quote from Fluxinator
So wait, you need a program to count from 3 to 100, and print if it's prime? If so I can make an example for you.



yeah thats all it is lol, I think it may be because I used int instead of double for some reason... idk, I'm gonna work on it again. I hate being noob at c++ =/
Automaton
2

Posts: 4,779
Joined: Nov 2007
Rep: 10

View Profile
Feb 25, 2009 10:38 PM #364026
well so far I've quickly cooked up this:


#include
#include

using namespace std;


int main( int argc, char *argv[] )
{
int i = 0, y = 0, x = 0, num = 0, isPrime = 0, primeCount = 0;
int primes[100];

for( i = 0; i <= 100; i++ )
{
isPrime = 0;

for( y = 0; y < sqrt( 100 ); y++)
{
if( i % y == 0)
{
isPrime = 0;
break;
}
else
isPrime = 1;
}

if( isPrime )
{
primes[x] = i;
x++;
primeCount++;
}
}

cout << "\nThere are " << primeCount << " prime numbers" << endl << endl;

for( i = 0; i < primeCount; i++ )
{
cout << primes << " is prime" << endl;
}

retu
rn 0;
}


but I get a seg fault for some reason. I'll debug it tomorrow, but you should get the gist of what you should do. Just edit the very first program on page 1 I made to loop over numbers and test them (increase 'num' each time).

[edit]
and you don't need a double, sqrt can only take an int as a paramater, but it returns a double.