Need some help

Hi i'm making a program that prints the all the prime numbers there are in a limit that the user enters. So if the user enters the limit as 10 the prog should print 1,2,3,5,7. I made the prog using the bool data type however i'm not supossed to use it :( Can anyone tell me how to make the prog without using bool? Heres what i did

#include

using namespace std;

void find_primes (bool p[], long n);
bool is_prime (long i);
void print_primes (bool p[], long n);

int main () {
const long MAXPRIME = 1000000; // 10^6
bool prime[MAXPRIME + 1];
long limit;
// start and finish times

// get limit from user
cout << "This program finds primes using a naive method.
";
cout << "It will search for primes from 1 up to a user-specified limit.
";
cout << "Limit must be less than or equal to " << MAXPRIME << endl;
cout << "Enter limit: ";
cin >> limit;

if (limit <= MAXPRIME) {
find_primes(prime, limit);
print_primes(prime, limit);

return 0;
}
else {
cerr << "Sorry, that number is out of range
";
return 1;
}
}

void find_primes (bool p[], long n) {
for (long i = 1; i <= n; i++)
p[i] = is_prime(i);
}

bool is_prime (long i) {
for (long d = 2; d * d <= i; d++)
if (i % d == 0)
return false;
return true;
}

void print_primes (bool p[], long n) {
for (long i = 1; i <= n; i++)
if (p[i])
// i is prime, print it
cout << i << endl;
}



Comments

  • : Hi i'm making a program that prints the all the prime numbers there are in a limit that the user enters. So if the user enters the limit as 10 the prog should print 1,2,3,5,7. I made the prog using the bool data type however i'm not supossed to use it :( Can anyone tell me how to make the prog without using bool? Heres what i did
    :
    : #include
    :
    : using namespace std;
    :
    : void find_primes (bool p[], long n);
    : bool is_prime (long i);
    : void print_primes (bool p[], long n);
    :
    : int main () {
    : const long MAXPRIME = 1000000; // 10^6
    : bool prime[MAXPRIME + 1];
    : long limit;
    : // start and finish times
    :
    : // get limit from user
    : cout << "This program finds primes using a naive method.
    ";

    interesting....a naive method ;)


    : cout << "It will search for primes from 1 up to a user-specified limit.
    ";
    : cout << "Limit must be less than or equal to " << MAXPRIME << endl;
    : cout << "Enter limit: ";
    : cin >> limit;
    :
    : if (limit <= MAXPRIME) {
    : find_primes(prime, limit);
    : print_primes(prime, limit);
    :
    : return 0;
    : }
    : else {
    : cerr << "Sorry, that number is out of range
    ";
    : return 1;
    : }
    : }
    :
    : void find_primes (bool p[], long n) {
    : for (long i = 1; i <= n; i++)
    : p[i] = is_prime(i);
    : }
    :
    : bool is_prime (long i) {
    : for (long d = 2; d * d <= i; d++)
    : if (i % d == 0)
    : return false;
    : return true;
    : }
    :
    : void print_primes (bool p[], long n) {
    : for (long i = 1; i <= n; i++)
    : if (p[i])
    : // i is prime, print it
    : cout << i << endl;
    : }
    :
    :
    :
    :

  • Well, I cant do your homework but there a several methods you could choose that do not require a bool value. You could put your prime numbers into and interger array and then print that array. You could print them as you find them (use care to keep your loop index from referencing numbers you have already examined), an alternate means of doing them all at once would be to go through your users limit, storing each prime into an array and using the primes in said array to check for the next numbers status (that is to say that as prime numbers go, you only have to check a number against the square of all preceding prime numbers to determine if it is a factor of a prime number - to the point - an array of primes will look like this 1,2,3,5,7,9,11,13, etc... therefore if you set up a recursive function that will check for the modulus result of all known (as determind by your array of primes) and the result is zero, its not a prime number. Mind you its late and been a hard weekend so you might need to ponder this a while for it to make sense. The long and the short of my point is this... you shouldn't have to make three calls to output your numbers, just do the output at the same time you are evaluating a number.
    -Mel

    : Hi i'm making a program that prints the all the prime numbers there are in a limit that the user enters. So if the user enters the limit as 10 the prog should print 1,2,3,5,7. I made the prog using the bool data type however i'm not supossed to use it :( Can anyone tell me how to make the prog without using bool? Heres what i did
    :
    : #include
    :
    : using namespace std;
    :
    : void find_primes (bool p[], long n);
    : bool is_prime (long i);
    : void print_primes (bool p[], long n);
    :
    : int main () {
    : const long MAXPRIME = 1000000; // 10^6
    : bool prime[MAXPRIME + 1];
    : long limit;
    : // start and finish times
    :
    : // get limit from user
    : cout << "This program finds primes using a naive method.
    ";
    : cout << "It will search for primes from 1 up to a user-specified limit.
    ";
    : cout << "Limit must be less than or equal to " << MAXPRIME << endl;
    : cout << "Enter limit: ";
    : cin >> limit;
    :
    : if (limit <= MAXPRIME) {
    : find_primes(prime, limit);
    : print_primes(prime, limit);
    :
    : return 0;
    : }
    : else {
    : cerr << "Sorry, that number is out of range
    ";
    : return 1;
    : }
    : }
    :
    : void find_primes (bool p[], long n) {
    : for (long i = 1; i <= n; i++)
    : p[i] = is_prime(i);
    : }
    :
    : bool is_prime (long i) {
    : for (long d = 2; d * d <= i; d++)
    : if (i % d == 0)
    : return false;
    : return true;
    : }
    :
    : void print_primes (bool p[], long n) {
    : for (long i = 1; i <= n; i++)
    : if (p[i])
    : // i is prime, print it
    : cout << i << endl;
    : }
    :
    :
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories