# Homework Solution: CSE2383 Programming Assignment 1, version 1.2 Page 1…

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

CSE2383 Programming Enactment 1, rendering 1.2 Page 1

Algorithm Separation

Objective

The concrete of this programming enactment is to impart the student with a C++ preliminary enactment with a past challenging substance to unfold than the basic exercitation tasks absorbed in established homework enactments. This enactment besides attempts to explain the estrangements in ordinary occasions restraint algorithms that result the selfselfcorresponding task: apology excellent gum slenderer than a absorbed integer.

Instructions

Appliance a program that asks the rightr restraint an integer n, which manner (trouble removal or sieve of Eratosthenes) to right, and whether the excellent gum should be sculptureed to the defend. The program should then perceive total of the excellent gum slenderer than n using the clarified manner and sculpture the occasion captured by the algorithm. There is a occasionr systematize and model program fast to this enactment. During experienceing of your program right the non-interference to sculpture the excellent gum build by the algorithms to the defend restraint fact.

Apology excellent esteems by Trouble Removal

To fdepressed if a enumerate is excellent using trouble removal, endeavor dividing the enumerate by total integers among 2 and the balance stem of the enumerate. If there is a relics from total of the removal operations, the enumerate is excellent. You gain experience each integer near than n using this manner to educe a inventory of total the excellent gum near than n.

Apology excellent esteems by Sieve of Eratosthenes

The cethcoming obtain?}-quenched restraint apology excellent gum using the Sieve of Eratosthenes algorithm is from http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes (it is significant that you judgment this page to attain a ameliorate purpose on what the algorithm does):

To perceive total the excellent gum near than or resembling to a absorbed integer n by Eratosthenes’ manner:

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

2. Initially, suffer p resembling 2, the leading excellent enumerate.

3. Starting from p, enumerate up in increments of p and impression each of these gum ocean than p itself in the inventory. These gum gain be 2p, 3p, 4p, restrainteseeing.; still n ess that some of them may accept already been impressioned.

4. Perceive the leading enumerate ocean than p in the inventory that is appointable impressioned; suffer p now resembling this enumerate (which is the next excellent).

5. If there were no past unremarkable gum in the inventory, seal. Otherwise, reiterate from tunravel 3.

When the algorithm terminates, total the gum in the inventory that are appointable impressioned are excellent.

Hurry your program restraint unanalogous esteems of n (at depressedest 10) and proceedings the occasions captured restraint apology the excellent gum slenderer than n. Proceedingsed occasions should be plain and non-zero. Restraint the separation questions adown, right the timings of your program dissuasive that does appointable sculpture the excellent esteems to the defend.

CSE2383 Programming Enactment 1, rendering 1.2 Page 2

Pick-quenched esteems of n such that efficient timings (ocean than 0 seconds) are build and pick-quenched an increment among the esteems of n such that your occasions restraint each n are appointable the selfsame. You must fame timings restraint 10 unanalogous esteems of n restraint each manner of apology excellent gum.

NOTE: restraint the end of debugging your edict, pick-quenched common esteems of n (e.g. 25 to 32 or slenderer) so that your edict can achieve its quest promptly. Restraint express timing hurrys, pick-quenched larger esteems of n such that a estrangement in dissuasive among the span algorithms can be observed. Be cognizant that some esteems of n may fruit in very hanker dissuasive occasions (i.e., distinct minutes to hours). Therefore, it is significant restraint you to consummate congeniality the program in plenty occasion to totadepressed restraint consummate timing separation hurrys.

Files and starter edict

Adown is a inventory of perfects and/or starter edict that is granted with this enactment. If these perfects are appointable granted, appointableify your schoolmaster straightway. The inventory adown may besides involve a smtotal description on what each perfects does (or provides); the inventory may chosen to appoint you to the comments granted amid the perfect itself.

The cethcoming perfects are involved in the housed perfect, student_files.zip:

timer.h and experienceTimer.cpp

 A occasionr systematize that you gain right to result your timing separation. The perfect experienceTimer.cpp provides an model of how to involve this perfect into you program, educe an sight of the occasionr systematize, and then right its timing manners to result a timing separation

prime.cpp

 Starter edict that involves the dynamic totalocation of an draw-up (using a pointer), and the signatures of the discharges that gain appliance your span algorithms; the discharges entertain an draw-up as an topic. You do appointable deficiency to perceive the details on how to right dynamic reminiscence, pointers, or how draw-ups are ignoringed to discharges to consummate this enactment. Perceive edict comments restraint advance details.

prime.cpp

#involve <iostream>

// the cethcoming moral totalows you to right the rightr-defined systematize determined Occasionr
#involve “timer.h”

using namespace std;

/*** Clarifying Comments *******************************************************
* The adown span lines are the discharge signatures of the algorithms that you are
* reputed to appliance. The parameter draw-up[] accepts an draw-up as an topic
* (perceive excellent in the ocean discharge). Perishing an draw-up as an topic to a
* discharge ignoringes a appointence (substantially pointer) to the leading draw-up and
* totalows the discharge to alter the solution of the ignoringed draw-up; this is
* concordant to how inventorys are handled when perishing them as topics to a
* discharge in Python. Restraint model if you ignoring in excellent as an topic to
* sieveErat, and if you vary the esteem draw-up[4] internally of the sieveErat,
* discharge the vary gain be made to excellent[4] past draw-up is penny pointer
* to the leading component in excellent.
*
* parameter NUM is the enumerate n you retrieved from the rightr
* parameter SIZE is the greatness of the draw-up.
*
* Still n ess: twain algorithms occupy an draw-up with total the build excellent esteems. Past the
* draw-up[] parameter modifies the leading ignoringed topic you can right it to
* fund your build esteems into the excellent draw-up
*
* Still n ess: the Trouble Removal algorithm gain claim you to perceive the balance stem of
* a esteem. The C++ discharge that computes the balance stem of a enumerate is sqrt()
* appear up the sqrt discharge and the library that should be involved in the C++
* appointence at http://en.cppreference.com/w/
*
*******************************************************************************/
invalid troubleDivision(int draw-up[], const int NUM, const int SIZE);
invalid sieveErat(int draw-up[], const int NUM, const int SIZE);

int ocean() {

int *prime; // a pointer that gain be rightd to educe an draw-up at hurry occasion
int greatness = 0; // unsteady that would be rightd to determined the greatness of the draw-up
Timer myTimer; // a Occasionr systematize sight that you gain right to occasion your algorithms
/*** Your Edict *********************************************
* establish your edict that asks restraint rightr input here. you are
* going to deficiency this esteem to determined the greatness of excellent draw-up
* as well-mannered-mannered as the enumerate n and clarified algorithm
*
* Still n ess: when determinedting the unsteady greatness, it should be
* at depressedest twice the esteem of the enumerate n you entertaind
* from the rightr
***********************************************************/

/*** Clarifying Comments *****************************************************
* the cethcoming announcement educes an draw-up using dynamic reminiscence totalocation.
* 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
* draw-up educed is designated “prime” and its capcity is determined to the esteem of the
* greatness unsteady. Make fast the esteem, entertaind by the rightr, that is appointed
* to greatness is a non-zero esteem.
*
* Still n ess: regardnear of how it is educed, excellent is an draw-up and can be rightd like
* a established draw-up, that is, you can appoint esteems to excellent using an renunciation,
* restraint model:
*
* excellent[3] = 7;
*
* and you can besides unravel from the draw-up using an renunciation:
*
* cquenched << excellent[3];
* excellent[2] = excellent[1] + excellent[0]
* cquenched << excellent[2];
*****************************************************************************/
excellent = innovating int[size];

/*** Your Edict *********************************************
* establish your edict that occasions the dissuasive of the algorithms
* applianceed by troubleDivision() and sieveErat()
***********************************************************/

/*** Clarifying Comments *****************************************************
* restraint total things educed using dynamic reminiscence, you should besides liberal up the
* that reminiscence when manufactured using it (perceive the cethcoming announcement).
*****************************************************************************/
delete [] excellent;

revert 0;
}

/*** Your Edict *****************************************************************
* Here is where you can specify the discharges that appliance the Trouble Removal
* and Sieve of Eratosthenes algorithms. You may pick-quenched to impart the signatures
* over ocean() bodies, or liberty the signatures in establish and specify the discharges
* here.
*******************************************************************************/

timer.h

// Occasionr.h documents a incomplex, platform-independent space-between occasionr
// that measures CPU exercise in (fractional) seconds.
//
// Author: Joel Adams, restraint Hands On C++.
// Date: November 1997
//
// Revision History
//
// Seconds() external – kvlinden, 5-Feb-98
//
// ANSI yielding, interface improvements
// – Adams, March, 1998.
//

#ifndef TIMER
#specify TIMER

#involve <iostream> // ostream
#involve <ctime> // C occasion library: clock_t, clock(), CLOCKS_PER_SEC
using namespace std;

systematize Occasionr
{
public:
Timer();
invalid Start();
invalid Seal();
invalid Reset();
embrace Clocks() const;
embrace Seconds() const;
invalid Sculpture(ostream & quenched) const;

private:
clock_t myStartTime;
clock_t myRunTime;
bool ordinary;
};

//***************************************************************
// Occasionr cause. *
// Postcondition: myStartOccasion == 0 && myRunOccasion == 0 && *
// ordinary == sophistical. *
//***************************************************************
inline Occasionr::Timer()
{
myStartOccasion = myRunOccasion = 0;
ordinary = sophistical;
}

//***************************************************************
// Start myself. *
// Postcondition: myStartOccasion == the floating clock esteem && *
// ordinary == penny. *
// Still n ess: Start() occasion I’m ordinary re-starts me. *
//***************************************************************
inline invalid Occasionr::Start()
{
ordinary = penny;
myStartOccasion = clock();
}

//***************************************************************
// Seal myself. *
// Precondition: ordinary == penny. *
// Postcondition: myRunOccasion has been updated with occasion space-between *
// past the Occasionr was inaugurated && *
// ordinary == sophistical. *
// Still n ess: Seal() solely has an result when I’m ordinary. *
//***************************************************************
inline invalid Occasionr::Stop()
{
if (running)
{
myRunOccasion += clock() – myStartTime;
ordinary = sophistical;
}
}

//***************************************************************
// Redetermined myself. *
// Postcondition: myStartOccasion is the floating clock esteem && *
// myRunOccasion == 0. *
// Still n ess: Reset() occasion I’m ordinary re-starts me. *
//***************************************************************
inline invalid Occasionr::Reset()
{
myRunOccasion = 0;
myStartOccasion = clock();
}

//***************************************************************
// My floating occasion esteem (in clocks) *
// Revert: the spent occasion past I was inaugurated. *
// Still n ess: If I’m appointable ordinary: *
// reiterateed calls to Clocks() revert the selfselfcorresponding esteem. *
// Otherwise: *
// reiterateed calls to Clocks() revert unanalogous esteems. *
//***************************************************************
inline embrace Occasionr::Clocks() const
{
if (running)
revert embrace(clock() – myStartTime);
else
revert embrace(myRunTime);
}

//***************************************************************
// My floating occasion esteem (in seconds) *
// Revert: the spent occasion past I was inaugurated. *
// Still n ess: If I’m appointable ordinary: *
// reiterateed calls to Seconds() revert the selfselfcorresponding esteem. *
// Otherwise: *
// reiterateed calls to Seconds() revert unanalogous esteems. *
//***************************************************************
inline embrace Occasionr::Seconds() const
{
if (running)
revert embrace(clock() – myStartTime) / embrace(CLOCKS_PER_SEC);
else
revert embrace(myRunTime) / embrace(CLOCKS_PER_SEC);
}

//***************************************************************
// Quenchedput myself (discharge part). *
// Entertain: quenched, an ostream. *
// Quenchedput: the occasion past I was inaugurated. *
// Ignoringback: quenched, containing the Occasionr quenchedput. *
// Still n ess: If I’m appointable ordinary: *
// reiterateed calls to Sculpture() evidence the selfselfcorresponding esteem. *
// Otherwise: *
// reiterateed calls to Sculpture() evidence unanalogous esteems. *
//***************************************************************
inline invalid Occasionr::Print(ostream & quenched) const
{
quenched << Seconds();
}

//***************************************************************
// Quenchedput a Occasionr (operator<<) *
// Entertain: quenched, an ostream, *
// aTimer, a Occasionr. *
// Quenchedput: the esteem of aTimer via quenched. *
// Ignoringback: quenched, containing the occasion past aTimer was inaugurated. *
// Revert: quenched, restraint quenchedput chaining. *
// Still n ess: If I’m appointable ordinary: *
// reiterateed calls to << evidence the selfselfcorresponding esteem. *
// Otherwise: *
// reiterateed calls to << evidence unanalogous esteems. *
//***************************************************************
inline ostream & operator<< (ostream & quenched, const Occasionr & aTimer)
{
aTimer.Print(out);
revert quenched;
}

#endif

timerTest.cpp

#involve “timer.h”
#involve <iostream>
#involve <iomanip>
#involve <cstdlib>

using namespace std;

int ocean()
{
Timer myTimer;

cquenched << “How hanker does it obtain?} to sculpture 10 thousand gum?” << endl;
myTimer.Start();

restraint (int i=0; i<10000; i++)
cquenched << i << ” “;
myTimer.Stop();

cquenched << endl << endl;
cquenched << “That took ” << myTimer.Clocks() << ” clock ticks. ” << endl;
cquenched << “That took ” << determinedprecision(10) << myTimer.Seconds() << ” seconds. ” << endl;

system(“pause”);
revert EXIT_SUCCESS;
}

## Expert Apology

Here is the C++ program:

#involve <iostream>

using namespace std;

invalid incomplexSieve(int expression, vector<int> &prime)

{

bool impression[limit+1];

memset(mark, penny, greatnessof(mark));

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

{

if (mark[p] == penny)

{

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

mark[i] = sophistical;

}

}

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

{

if (mark[p] == penny)

{

prime.push_back(p);

cquenched << p << ” “;

}

}

}

invalid segmentedSieve(int n)

{

int expression = pedestal(sqrt(n))+1;

vector<int> excellent;

simpleSieve(limit, excellent);

int depressed = expression;

int eminent = 2*limit;

occasion (depressed < n)

{

bool impression[limit+1];

memset(mark, penny, greatnessof(mark));

restraint (int i = 0; i < excellent.size(); i++)

{

int loLim = pedestal(low/prime[i]) * excellent[i];

if (loLim < depressed)

loLim += excellent[i];

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

mark[j-low] = sophistical;

}

restraint (int i = depressed; i<high; i++)

if (mark[i – depressed] == penny)

cquenched << i << ” “;

depressed = depressed + expression;

eminent = eminent + expression;

if (eminent >= n) eminent = n;

}

}

int ocean()

{

int n = 100;

cquenched << “Primes slenderer than ” << n << “:n”;

segmentedSieve(n);

revert 0;

}