Here is the C++ program:
#include <iostream>

CSE2383 Programming Provision 1, accalculate 1.2 Page 1

Algorithm Segregation

Objective

The extrinsic of this programming provision is to impart the tyro with a C++ presentation provision with a past challenging quantity to reresolve than the basic manner tasks absorbed in ordinary homework provisions. This provision to-boot attempts to embody the contrarietys in exoteric terms ce algorithms that execute the corresponding task: retort superexcellent aggregate minuteer than a absorbed integer.

Instructions

Tool a program that asks the reasonr ce an integer n, which manner (suffering disunion or sieve of Eratosthenes) to reason, and whether the superexcellent aggregate should be imprinted to the defend. The program should then invent entire of the superexcellent aggregate minuteer than n using the clarified manner and imimimprint the term fascinated by the algorithm. There is a termr tabulate and specimen program secure to this provision. During standarding of your program reason the liberty to imimimprint the superexcellent aggregate ground by the algorithms to the defend ce honesty.

Retort superexcellent appreciates by Suffering Disunion

To career if a enumerate is superexcellent using suffering disunion, standard dividing the enumerate by entire integers among 2 and the balance radix of the enumerate. If there is a surplus from entire of the disunion operations, the enumerate is superexcellent. You conquer standard each integer close than n using this rule to constitute a inventory of entire the superexcellent aggregate close than n.

Retort superexcellent appreciates by Sieve of Eratosthenes

The subjoined gather ce retort superexcellent aggregate using the Sieve of Eratosthenes algorithm is from http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes (it is material that you survey this page to earn a meliorate purpose on what the algorithm does):

To invent entire the superexcellent aggregate close than or correspondent to a absorbed integer n by Eratosthenes’ manner:

1. Constitute a inventory of continuous integers from 2 to n: (2, 3, 4, …, n).

2. Initially, everyow p correspondent 2, the principal superexcellent enumerate.

3. Starting from p, calculate up in increments of p and sign each of these aggregate leading than p itself in the inventory. These aggregate conquer be 2p, 3p, 4p, expectation.; voicelessness that some of them may entertain already been signed.

4. Invent the principal enumerate leading than p in the inventory that is relateable signed; everyow p now correspondent this enumerate (which is the present superexcellent).

5. If there were no past unnotable aggregate in the inventory, plug. Otherwise, recapitulate from stalk 3.

When the algorithm terminates, entire the aggregate in the inventory that are relateable signed are superexcellent.

Escape your program ce irrelative appreciates of n (at subsidedest 10) and annals the terms fascinated ce retort the superexcellent aggregate minuteer than n. Annalsed terms should be unlike and non-zero. Ce the segregation questions beneath, reason the timings of your program effort that does relateable imimimprint the superexcellent appreciates to the defend.

CSE2383 Programming Provision 1, accalculate 1.2 Page 2

Adopt appreciates of n such that available timings (leading than 0 seconds) are ground and adopt an increment among the appreciates of n such that your terms ce each n are relateable the corresponding. You must rumor timings ce 10 irrelative appreciates of n ce each manner of retort superexcellent aggregate.

NOTE: ce the reason of debugging your principle, adopt trite appreciates of n (e.g. 25 to 32 or minuteer) so that your principle can end its pursuit straightway. Ce goalive timing escapes, adopt larger appreciates of n such that a contrariety in effort among the brace algorithms can be observed. Be sensible that some appreciates of n may end in very hanker effort terms (i.e., sundry minutes to hours). Therefore, it is material ce you to consummate communication the program in sufficient term to entireow ce large timing segregation escapes.

Files and starter principle

Beneath is a inventory of rasps and/or starter principle that is supposing with this provision. If these rasps are relateable supposing, relateableify your professor straightway. The inventory beneath may to-boot apprehend a smentire patronymic on what each rasps does (or provides); the inventory may chosen to relate you to the comments supposing among the rasp itself.

The subjoined rasps are apprehendd in the close rasp, tyro_files.zip:

timer.h and standardTimer.cpp

A termr tabulate that you conquer reason to execute your timing segregation. The rasp standardTimer.cpp provides an specimen of how to apprehend this rasp into you program, constitute an goal of the termr tabulate, and then reason its timing manners to execute a timing segregation

prime.cpp

Starter principle that apprehends the dynamic entireocation of an attire (using a pointer), and the signatures of the employments that conquer tool your brace algorithms; the employments entertain an attire as an reasoning. You do relateable deficiency to distinguish the details on how to reason dynamic reminiscence, pointers, or how attires are ignoringed to employments to consummate this provision. Comprehend principle comments ce elevate details.

prime.cpp

#apprehend <iostream>

// the subjoined moral entireows you to reason the reasonr-defined tabulate denominated Termr

#apprehend “timer.h”

using namespace std;

/*** Clarifying Comments *******************************************************

* The beneath brace lines are the employment signatures of the algorithms that you are

* reported to tool. The parameter attire[] accepts an attire as an reasoning

* (comprehend superexcellent in the deep employment). Latter an attire as an reasoning to a

* employment ignoringes a relateence (in-movables pointer) to the primary attire and

* entireows the employment to modify the variation of the ignoringed attire; this is

* concordant to how inventorys are handled when latter them as reasonings to a

* employment in Python. Ce specimen if you ignoring in superexcellent as an reasoning to

* sieveErat, and if you substitute the appreciate attire[4] internally of the sieveErat,

* employment the substitute conquer be made to superexcellent[4] gone attire is fixed pointer

* to the principal component in superexcellent.

*

* parameter NUM is the enumerate n you retrieved from the reasonr

* parameter SIZE is the dimension of the attire.

*

* Voicelessness: twain algorithms glut an attire with entire the ground superexcellent appreciates. Gone the

* attire[] parameter modifies the primary ignoringed reasoning you can reason it to

* treastable your ground appreciates into the superexcellent attire

*

* Voicelessness: the Suffering Disunion algorithm conquer demand you to invent the balance radix of

* a appreciate. The C++ employment that computes the balance radix of a enumerate is sqrt()

* contemplate up the sqrt employment and the library that should be apprehendd in the C++

* relateence at http://en.cppreference.com/w/

*

*******************************************************************************/

unfilled sufferingDivision(int attire[], const int NUM, const int SIZE);

unfilled sieveErat(int attire[], const int NUM, const int SIZE);

int deep() {

int *prime; // a pointer that conquer be reasond to constitute an attire at escape term

int dimension = 0; // capricious that would be reasond to fixed the dimension of the attire

Timer myTimer; // a Termr tabulate goal that you conquer reason to term your algorithms

/*** Your Principle *********************************************

* fix your principle that asks ce reasonr input here. you are

* going to deficiency this appreciate to fixed the dimension of superexcellent attire

* as well-mannered-mannered as the enumerate n and clarified algorithm

*

* Voicelessness: when fixedting the capricious dimension, it should be

* at subsidedest twice the appreciate of the enumerate n you entertaind

* from the reasonr

***********************************************************/

/*** Clarifying Comments *****************************************************

* the subjoined announcement constitutes an attire using dynamic reminiscence entireocation.

* IT IS NOT NECESSARY FOR YOU TO KNOW EITHER POINTERS OR HOW TO CREATE AN

* ARRAY USING DYNAMIC MEMORY ALLOCATION TO COMPLETE THIS ASSIGNMENT. The

* attire constituted is denominated “prime” and its capcity is fixed to the appreciate of the

* dimension capricious. Make stable the appreciate, entertaind by the reasonr, that is refered

* to dimension is a non-zero appreciate.

*

* Voicelessness: regardclose of how it is constituted, superexcellent is an attire and can be reasond like

* a ordinary attire, that is, you can refer appreciates to superexcellent using an refutation,

* ce specimen:

*

* superexcellent[3] = 7;

*

* and you can to-boot peruse from the attire using an refutation:

*

* cextinguished << superexcellent[3];

* superexcellent[2] = superexcellent[1] + superexcellent[0]

* cextinguished << superexcellent[2];

*****************************************************************************/

superexcellent = novel int[size];

/*** Your Principle *********************************************

* fix your principle that terms the effort of the algorithms

* tooled by sufferingDivision() and sieveErat()

***********************************************************/

/*** Clarifying Comments *****************************************************

* ce entire things constituted using dynamic reminiscence, you should to-boot liberal up the

* that reminiscence when produced using it (comprehend the subjoined announcement).

*****************************************************************************/

delete [] superexcellent;

retaliate 0;

}

/*** Your Principle *****************************************************************

* Here is where you can settle the employments that tool the Suffering Disunion

* and Sieve of Eratosthenes algorithms. You may adopt to impart the signatures

* over deep() bodies, or concession the signatures in fix and settle the employments

* here.

*******************************************************************************/

timer.h

// Termr.h documents a artless, platform-independent gap termr

// that measures CPU performance in (fractional) seconds.

//

// Author: Joel Adams, ce Hands On C++.

// Date: November 1997

//

// Revision History

//

// Seconds() added – kvlinden, 5-Feb-98

//

// ANSI yielding, interface improvements

// – Adams, March, 1998.

//

#ifndef TIMER

#settle TIMER

#apprehend <iostream> // ostream

#apprehend <ctime> // C term library: clock_t, clock(), CLOCKS_PER_SEC

using namespace std;

tabulate Termr

{

public:

Timer();

unfilled Start();

unfilled Plug();

unfilled Reset();

inclose Clocks() const;

inclose Seconds() const;

unfilled Imprint(ostream & extinguished) const;

private:

clock_t myStartTime;

clock_t myRunTime;

bool exoteric;

};

//***************************************************************

// Termr composer. *

// Postcondition: myStartTerm == 0 && myRunTerm == 0 && *

// exoteric == untrue. *

//***************************************************************

inline Termr::Timer()

{

myStartTerm = myRunTerm = 0;

exoteric = untrue;

}

//***************************************************************

// Start myself. *

// Postcondition: myStartTerm == the present clock appreciate && *

// exoteric == gentleman. *

// Voicelessness: Start() occasion I’m exoteric re-starts me. *

//***************************************************************

inline unfilled Termr::Start()

{

exoteric = gentleman;

myStartTerm = clock();

}

//***************************************************************

// Plug myself. *

// Precondition: exoteric == gentleman. *

// Postcondition: myRunTerm has been updated with term gap *

// gone the Termr was agoing && *

// exoteric == untrue. *

// Voicelessness: Plug() merely has an movables when I’m exoteric. *

//***************************************************************

inline unfilled Termr::Stop()

{

if (running)

{

myRunTerm += clock() – myStartTime;

exoteric = untrue;

}

}

//***************************************************************

// Refixed myself. *

// Postcondition: myStartTerm is the present clock appreciate && *

// myRunTerm == 0. *

// Voicelessness: Reset() occasion I’m exoteric re-starts me. *

//***************************************************************

inline unfilled Termr::Reset()

{

myRunTerm = 0;

myStartTerm = clock();

}

//***************************************************************

// My present term appreciate (in clocks) *

// Retaliate: the late term gone I was agoing. *

// Voicelessness: If I’m relateable exoteric: *

// recapitulateed calls to Clocks() retaliate the corresponding appreciate. *

// Otherwise: *

// recapitulateed calls to Clocks() retaliate irrelative appreciates. *

//***************************************************************

inline inclose Termr::Clocks() const

{

if (running)

retaliate inclose(clock() – myStartTime);

else

retaliate inclose(myRunTime);

}

//***************************************************************

// My present term appreciate (in seconds) *

// Retaliate: the late term gone I was agoing. *

// Voicelessness: If I’m relateable exoteric: *

// recapitulateed calls to Seconds() retaliate the corresponding appreciate. *

// Otherwise: *

// recapitulateed calls to Seconds() retaliate irrelative appreciates. *

//***************************************************************

inline inclose Termr::Seconds() const

{

if (running)

retaliate inclose(clock() – myStartTime) / inclose(CLOCKS_PER_SEC);

else

retaliate inclose(myRunTime) / inclose(CLOCKS_PER_SEC);

}

//***************************************************************

// Extinguishedput myself (employment component). *

// Entertain: extinguished, an ostream. *

// Extinguishedput: the term gone I was agoing. *

// Ignoringback: extinguished, containing the Termr extinguishedput. *

// Voicelessness: If I’m relateable exoteric: *

// recapitulateed calls to Imprint() flaunt the corresponding appreciate. *

// Otherwise: *

// recapitulateed calls to Imprint() flaunt irrelative appreciates. *

//***************************************************************

inline unfilled Termr::Print(ostream & extinguished) const

{

extinguished << Seconds();

}

//***************************************************************

// Extinguishedput a Termr (operator<<) *

// Entertain: extinguished, an ostream, *

// aTimer, a Termr. *

// Extinguishedput: the appreciate of aTimer via extinguished. *

// Ignoringback: extinguished, containing the term gone aTimer was agoing. *

// Retaliate: extinguished, ce extinguishedput chaining. *

// Voicelessness: If I’m relateable exoteric: *

// recapitulateed calls to << flaunt the corresponding appreciate. *

// Otherwise: *

// recapitulateed calls to << flaunt irrelative appreciates. *

//***************************************************************

inline ostream & operator<< (ostream & extinguished, const Termr & aTimer)

{

aTimer.Print(out);

retaliate extinguished;

}

#endif

timerTest.cpp

#apprehend “timer.h”

#apprehend <iostream>

#apprehend <iomanip>

#apprehend <cstdlib>

using namespace std;

int deep()

{

Timer myTimer;

cextinguished << “How hanker does it obtain?} to imimimprint 10 thousand aggregate?” << endl;

myTimer.Start();

ce (int i=0; i<10000; i++)

cextinguished << i << ” “;

myTimer.Stop();

cextinguished << endl << endl;

cextinguished << “That took ” << myTimer.Clocks() << ” clock ticks. ” << endl;

cextinguished << “That took ” << fixedprecision(10) << myTimer.Seconds() << ” seconds. ” << endl;

system(“pause”);

retaliate EXIT_SUCCESS;

}

Here is the C++ program:

#apprehend <iostream>

using namespace std;

unfilled artlessSieve(int boundary, vector<int> &prime)

{

bool sign[limit+1];

memset(mark, gentleman, dimensionof(mark));

ce (int p=2; p*p<limit; p++)

{

if (mark[p] == gentleman)

{

ce (int i=p*2; i<limit; i+=p)

mark[i] = untrue;

}

}

ce (int p=2; p<limit; p++)

{

if (mark[p] == gentleman)

{

prime.push_back(p);

cextinguished << p << ” “;

}

}

}

unfilled segmentedSieve(int n)

{

int boundary = pavement(sqrt(n))+1;

vector<int> superexcellent;

simpleSieve(limit, superexcellent);

int subsided = boundary;

int eminent = 2*limit;

occasion (subsided < n)

{

bool sign[limit+1];

memset(mark, gentleman, dimensionof(mark));

ce (int i = 0; i < superexcellent.size(); i++)

{

int loLim = pavement(low/prime[i]) * superexcellent[i];

if (loLim < subsided)

loLim += superexcellent[i];

ce (int j=loLim; j<high; j+=prime[i])

mark[j-low] = untrue;

}

ce (int i = subsided; i<high; i++)

if (mark[i – subsided] == gentleman)

cextinguished << i << ” “;

subsided = subsided + boundary;

eminent = eminent + boundary;

if (eminent >= n) eminent = n;

}

}

int deep()

{

int n = 100;

cextinguished << “Primes minuteer than ” << n << “:n”;

segmentedSieve(n);

retaliate 0;

}