Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using C++ implemented using microsoft visual studio creat an algorithm that uses

ID: 3795222 • Letter: U

Question

using C++ implemented using microsoft visual studio creat an algorithm that uses an optimization feature by looking at the key values for m sqrt ( upper ). However, in your implementation, test all numbers m such that m * m upper, rather than computing the square root of upper. In this file, the main function is already implemented. 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:

Explanation / Answer

#include "time.h" enum { ttuUnknown, ttuHiRes, ttuClock } TimerToUse = ttuUnknown; LARGE_INTEGER PerfFreq; // ticks per second int PerfFreqAdjust; // in case Freq is too big int OverheadTicks; // overhead in calling timer void DunselFunction() { return; } void DetermineTimer() { void (*pFunc)() = DunselFunction; // Assume the worst TimerToUse = ttuClock; if ( QueryPerformanceFrequency(&PerfFreq) ) { // We can use hires timer, determine overhead TimerToUse = ttuHiRes; OverheadTicks = 200; for ( int i=0; i < 20; i++ ) { LARGE_INTEGER b,e; int Ticks; QueryPerformanceCounter(&b); (*pFunc)(); QueryPerformanceCounter(&e); Ticks = e.LowPart - b.LowPart; if ( Ticks >= 0 && Ticks >= 1; PerfFreqAdjust++; } } return; } double DoBench(void(*funcp)()) { double time; /* Elapsed time */ // Let any other stuff happen before we start MSG msg; PeekMessage(&msg,NULL,NULL,NULL,PM_NOREMOVE); Sleep(0); if ( TimerToUse == ttuUnknown ) DetermineTimer(); if ( TimerToUse == ttuHiRes ) { LARGE_INTEGER tStart, tStop; LARGE_INTEGER Freq = PerfFreq; int Oht = OverheadTicks; int ReduceMag = 0; SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); QueryPerformanceCounter(&tStart); (*funcp)(); //call the actual function being timed QueryPerformanceCounter(&tStop); SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL); // Results are 64 bits but we only do 32 unsigned int High32 = tStop.HighPart - tStart.HighPart; while ( High32 ) { High32 >>= 1; ReduceMag++; } if ( PerfFreqAdjust || ReduceMag ) { if ( PerfFreqAdjust > ReduceMag ) ReduceMag = PerfFreqAdjust; tStart.QuadPart = Int64ShrlMod32(tStart.QuadPart, ReduceMag); tStop.QuadPart = Int64ShrlMod32(tStop.QuadPart, ReduceMag); Freq.QuadPart = Int64ShrlMod32(Freq.QuadPart, ReduceMag); Oht >>= ReduceMag; } // Reduced numbers to 32 bits, now can do the math if ( Freq.LowPart == 0 ) time = 0.0; else time = ((double)(tStop.LowPart - tStart.LowPart - Oht))/Freq.LowPart; } else { long stime, etime; SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); stime = clock(); (*funcp)(); etime = clock(); SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL); time = ((double)(etime - stime)) / CLOCKS_PER_SEC; } return (time); }