Hi,

My assignment for class was to produce a program that runs the seive of eratosthenes to determine all the prime numbers between 2 and n, with n no larger than 100. I tryed to write the program with a boolian array that will turn all non-primes to false up to the max of 100, and then only output to the requested input of n.

Quite honestly, this confuses me and I am not even sure I have the process right, let alone the program. As is, I am getting an unresolved external error upon linking. Line 38 is what I am most unsure of, and for some reason think my error is there.

Any advice or help on getting this thing running would be much appriciated. How am I doing?

Thanks.--Will

Attachments
``````//sieve.cpp
//
//Will Urban CS 161 2/20/05
//
//this program determines prime numbers from 1 to a designated
//entry, up to a maximum of 100

#include <iostream>
using namespace std;

//constant and variable declarations

const int SIZE = 99;
int n;

int main ()
{
//variable declarations

bool sieve [SIZE];
bool test = false;
//get stopping point from user

cout << "What number do you want primes to? ";
cin >> n;

//fill in sieve to true

for (int k = 0; k < SIZE ; ++k)
{
sieve [k] = true;
}

for (int i = 2; i < SIZE; ++i)
{
if (sieve [i])
{
for (int j = i + i; j < SIZE ; ++j)
{
sieve [j] = false;
}

}
}
for (int m = 2; m < n; ++m)
{
if (sieve [m] )
{
cout << m + 1 << "is a prime, " << endl;
}
}
return 0;

}``````
2
Contributors
2
Replies
3
Views
12 Years
Discussion Span
Last Post by Siersan

OK, I got a bit further and think I have the basic idea down, however, I am still getting the unresolved externals message. Attatched is my improved code. Thanks. Will

Hi,

My assignment for class was to produce a program that runs the seive of eratosthenes to determine all the prime numbers between 2 and n, with n no larger than 100. I tryed to write the program with a boolian array that will turn all non-primes to false up to the max of 100, and then only output to the requested input of n.

Quite honestly, this confuses me and I am not even sure I have the process right, let alone the program. As is, I am getting an unresolved external error upon linking. Line 38 is what I am most unsure of, and for some reason think my error is there.

Any advice or help on getting this thing running would be much appriciated. How am I doing?

Thanks.--Will

Attachments
``````//sieve.cpp
//
//Will Urban CS 161 2/20/05
//
//this program determines prime numbers from 1 to a designated
//entry, up to a maximum of 100

#include <iostream>
using namespace std;

//constant and variable declarations

const int SIZE = 99;
int stop;

int main ()
{
//variable declarations

bool sieve [SIZE];
//bool test = false;
//get stopping point from user

cout << "What number do you want primes to? ";
cin >> stop;

//fill in sieve to true

for (int k = 0; k < SIZE ; ++k)
{
sieve [k] = 1;
}

for (int i = 2; i * i < SIZE; ++i)
{
if (sieve [i])

for (int j = i + i; j < SIZE ; j += i)
{
sieve [j] = 0;

}

}
for (int m = 2; m < stop; ++m)
{
if (sieve [m] )
{
cout << m + 1 << "is a prime, " << endl;
}
}
return 0;

}``````

What errors you are getting? It compiles and runs just fine for me except every prime is 1 larger than it should be because you add 1 to m when printing.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.