For this computer assignment you are to write and implement
For this computer assignment, you are to write and implement a using C++ implemented using microsoft visual program to find and print all prime numbers within a range [lower upper] using the algorithm known as the Sieve of Eratosthenes. The program will have basic interactive user interface to take input values of lower and upper and allow the user to continue or quit the game.
You are required to implement the following functions:
void sieve( set& s, const int lower, const int upper){}
This routine can be used to apply the Sieve of Eratosthenes algorithm to remove all nonprime numbers from the integer set s = { lower, …, upper }.
void print_primes( const set& s, const int lower, const int upper){}
This routine can be used to print the 1 elements in the integer set s (NO_ITEMS = 8 primes per line and ITEM_W = 6 spaces allocated for each prime).
void run_game(set& s){}
This routine maintains a loop to take inputs from a user. The user will be asked for the range of the prime numbers. If the range is not valid, e.g. lower is greater than upper, the user will be asked to input again. The routine will invoke sieve() and print_primes(). Once the results are displayed, the user will be asked whether to continue or quit the game. In case of continuing the game, the user will be asked for values of the range again. The game continues until the user requests to stop.
int main() {
set s;
run_game(s);
return 0;
}
Output
Please input lower bound and upper bound and hit enter (e.g. 10 100):
There are 2 prime numbers between 5 and 10:
5 7
Do you want to continue or not? Please answer yes or no and hit enter:
Please input lower bound and upper bound and hit enter (e.g. 10 100):
There are 21 prime numbers between 10 and 100:
11 13 17 19 23 29 31 37
41 43 47 53 59 61 67 71
73 79 83 89 97
Do you want to continue or not? Please answer yes or no and hit enter:
Please input lower bound and upper bound and hit enter (e.g. 10 100):
There are 70 prime numbers between 100 and 500:
101 103 107 109 113 127 131 137
139 149 151 157 163 167 173 179
181 191 193 197 199 211 223 227
229 233 239 241 251 257 263 269
271 277 281 283 293 307 311 313
317 331 337 347 349 353 359 367
373 379 383 389 397 401 409 419
421 431 433 439 443 449 457 461
463 467 479 487 491 499
Do you want to continue or not? Please answer yes or no and hit enter:
Solution
#include <iostream>
#include <string>
using namespace std;
void run_game(bool *prime, int l, int m)
{
int number = 0;
for (int p = 2; p*p <= m; p++)
{
if (prime[p] == true)
{
// Update all the multiples of p
for (int i = p * 2; i <= m; i += p)
prime[i] = false;
}
}
for (int p = l; p <= m; p++)
if (prime[p])
number++;
cout << \"There are \" << number << \" prime numbers between \" << l << \" and \" << m <<\":\"<<endl;
// Print all prime numbers
for (int p = l; p <= m; p++)
if (prime[p])
cout << p << \" \";
cout << endl;
}
int main()
{
int l,m;
bool check = false;
string user;
do {
cout << \"Please input lower bound and upper bound and hit enter (e.g. 10 100): \" << endl;
cin >> l;
cin >> m;
// initialize entire boolean array to true //
bool *prime = new bool[m + 1];
memset(prime, true, m + 1);
run_game(prime, l, m);
cout << \"Do you want to continue or not? Please answer yes or no and hit enter:\";
cin >> user;
if (user == \"yes\")
{
check = true;
}
else if (user == \"no\")
break;
else
continue;
} while (check == true);
return 0;
}


