change the main function put values of l for lower bound and

change the main function, put values of l for lower bound and m for higher bound. You need to allocate static boolean array prime with size m+1. To have predefined numbers instead of user input

#include
#include
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 <<\":\"<    // 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;
}

Solution

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


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 <<\":\"; // Print all prime numbers
for (int p = l; p <= m; p++)
if (prime[p])
cout << p << \" \";
cout << endl;
}
int main()
{
//put values of l for lower bound and m for higher bound
int l=40,m=66;

bool check = false;
string user;

do {

cout << \"lower bound is: \"<<l<<\" and upper bound is: \" << m<<endl;

//allocate static boolean array prime with size m+1. //
static bool *prime = new bool[m + 1];
memset(prime, true, m + 1);
run_game(prime, l, m);
cout<<endl;
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;
}

========================

output sample for l=44 and m=66

lower bound is: 40 and upper bound is: 66
There are 6 prime numbers between 40 and 66:41 43 47 53 59 61

Do you want to continue or not? Please answer yes or no and hit enter:

-----------------------

output sample for l=12 and m=20

lower bound is: 12 and upper bound is: 20
There are 3 prime numbers between 12 and 20:13 17 19

Do you want to continue or not? Please answer yes or no and hit enter:

---------------------------------------------------------------------------------------------

If you have any query, please feel free to ask.

Thanks a lot.

change the main function, put values of l for lower bound and m for higher bound. You need to allocate static boolean array prime with size m+1. To have predefi
change the main function, put values of l for lower bound and m for higher bound. You need to allocate static boolean array prime with size m+1. To have predefi
change the main function, put values of l for lower bound and m for higher bound. You need to allocate static boolean array prime with size m+1. To have predefi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site