Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please complete the code where it says complete in bold. CODE IS HERE #include #include #include #include using namespace std; /************************** * Your solutions go

Please complete the code where it says complete in bold.

image text in transcribedimage text in transcribedimage text in transcribed

CODE IS HERE

#include

#include

#include

#include

using namespace std;

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

* Your solutions go here *

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

int collatzLength(int n){

int count = 0;

while (n!= 1){

if (n%2 == 0){

n = n/2;

}

else if (n%2 == 1){

n = 3 * n + 1;

}

count++;

}

return count;

}

void printStats(const vector &v)

{

complete

}

int sumMultiples(const vector &v, int n){

complete

return 0;

}

void greaterThanK(vector &v, int k)

{

complete

}

void pivot(vector &v, int k)

{

complete

}

bool isSubarray(const vector &a, const vector &b)

{

complete

return false;

}

bool isPrimeA(int n)

{

complete

return false;

}

int sumPrimesA(int n)

{

complete

return 0;

}

bool isPrimeB(int n)

{

complete

return false;

}

int sumPrimesB(int n)

{

complete

return 0;

}

int sieveOfErathosthenes(int n)

{

complete

return 0;

}

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

* Main and helper functions for quick testing *

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

void readIntVector(vector &v, int n);

void timePrimes();

int main(){

int question = -1;

cout

cin >> question;

int n = 0, k = 0, m = 0;

vector v;

string s = "";

vector a;

vector b;

switch (question){

case 1: //collatzLength

cout

cin >> n;

cout

break;

case 2: //printStats

cout

cin >> n;

cout

readIntVector(v, n);

cout

printStats(v);

break;

case 3: //sumMultiples

cout

cin >> n;

cout

readIntVector(v, n);

cout

cin >> k;

cout

cout

break;

case 4: //greaterThanK

cout

cin >> n;

cout

readIntVector(v, n);

cout

cin >> k;

cout

greaterThanK(v, k);

for (int j = 0; j

break;

case 5: //pivot

cout

cin >> n;

cout

readIntVector(v, n);

cout

cin >> k;

cout

pivot(v, k);

for (int j = 0; j

break;

case 6: //isSubarray

cout

cin >> n >> m;

cout

for (int i = 0; i

cin >> s;

a.push_back(s);

}

cout

for (int i = 0; i

cin >> s;

b.push_back(s);

}

cout

cout

break;

case 7: //primes

cout

cin >> n;

cout

break;

case 8: //time primes

cout

timePrimes();

break;

}

return 0;

}

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

* Read a vector of integers in from cin *

* v - vector & - the integer vector to fill *

* n - int - the number of integers to read *

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

void readIntVector(vector &v, int n){

int m = 0;

for (int i = 0; i

cin >> m;

v.push_back(m);

}

}

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

* Testing run times of different approaches to finding the sum of prime numbers under a threshold *

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

void timePrimes(){

int sum = -1;

chrono::high_resolution_clock::time_point start;

chrono::high_resolution_clock::time_point end;

double elapsed = -1;

vector x = {10, 100, 1000, 10000, 100000, 500000};

for (int i = 0; i

{

cout

start = chrono::high_resolution_clock::now();

sum = sumPrimesA(x[i]);

end = chrono::high_resolution_clock::now();

elapsed = chrono::duration_cast<:duration>>(end - start).count();

cout

start = chrono::high_resolution_clock::now();

sum = sumPrimesB(x[i]);

end = chrono::high_resolution_clock::now();

elapsed = chrono::duration_cast<:duration>>(end - start).count();

cout

start = chrono::high_resolution_clock::now();

sum = sieveOfErathosthenes(x[i]);

end = chrono::high_resolution_clock::now();

elapsed = chrono::duration_cast<:duration>>(end - start).count();

cout

cout

}

cout

start = chrono::high_resolution_clock::now();

long sum2 = sieveOfErathosthenes(1000000);

end = chrono::high_resolution_clock::now();

elapsed = chrono::duration_cast<:duration>>(end - start).count();

cout

cout

}

The goal of this lab is to start thinking in terms of C++ and to start to think about the importance of algorithm choice with respect to run time. C++ solutions to these problems should be written in a file called lab_1.cpp (a skeleton is available on Blackboard) and submitted on turnin before January 30th at 11:59 pm. Note that your submission should use good C++ coding style and should include appropriate comments. Points will be deducted if these are not present. There will be time in lab to discuss these problems in small groups and I highly encourage you to collab- orate with one another outside of class. However, you must write up your own solutions independently of one another. Feel free to communicate via Discord and to post questions on the appropriate forum in Blackboard. Do not post solutions. Also, please include a list of the people you work with in a comment at the top of your submission. Have fun! 1. (10 pts) Given any positive integer n, define ,n even Sn/2 f(n):= 3n +1 ,n odd. The Collatz conjecture states that the sequence ai: = { ,i = 0 1 f(ai1) ,i > 0 eventually reaches 1. Write a function int collatzLength(int n) that determines the first i for which ai = 1 for a given n. For example, when n=17, the sequence ai begins 17,52, 26, 13, 40, 20, 10,5, 16, 8, 4, 2, 1,... and 212 = 1. Thus, collatzLength(17) should return 12. 2. (5 pts) Write a function void printStats (const vector &v) that prints the minimum, mean, and maximum of the values in the given vector separated by spaces. For example, vector int> y = {1, 2, 3, 4, 5}; printStats(v); should print 1 3 5 If the given vector is empty, your function should print Empty vector. Note that passing the vector v by reference (as opposed to by value) is usually preferred. This saves both time and space. When the vector will not be changed by the function, include the keyword const as we have done here. 3. (10 pts) Write a function int sumMultiples (const vector &nums, int n) that returns the sum of all multiples of values in the vector nums less than n. For example, vector nums = {3, 5}; int n = 30; sumMultiples (nums, n); should return 195. In particular, 195 = 3+5+6+9+10 + 12 + 15 +18+20+21 + 24 + 25 + 27. Assume that the elements of nums are positive integers. 4. (10 pts) Write a function void greaterThank(vector &v, int k) that removes all values less than or equal to k from the given vector. For example, vector y = {4, 9, 2, 3, 7}; int k = 4; greaterThank (v, k); should change the vector v to {9, 7}. 5. (10 pts) Write a function void pivot (vector &v, int k) that rearranges the elements of v so that all values less than or equal to k appear before those that are greater than k. This function should use no additional memory for storage and should use the fewest number of operations possible. For example, vector y = {5, 8, 3, 4, 3, 1, 9}; int k = 3; pivot (v, k); should change v to {1, 3, 3, 4, 8, 5, 9}. 6. (10 pts) Given two vectors a and b, write a function bool isSubarray(const vector &a, const vector &b) that returns true if a is a subarray of b and false otherwise. For ex- ample, vector a = {"a", "t", "C"}; vector b = {"a", "a", "t", "a", "t", "c", "g"}; isSubarray(a, b); should return true since the sequence ("a", "t", "c"} appears in b starting at index 3. On the other hand, vector a {"c", "g", "C"}; vector b = {"a", "a", "t", "a", "t", "C", "g"}; isSubarray(a, b); should return false since the sequence {"c", "g", "C"} does not appear in b. = 7. Prime numbers cannot be written as the product of two integers both greater than 1. Put another way, if p is prime, then p mod i + 0 for all 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 15th International Conference Dexa 2004 Zaragoza Spain August 30 September 3 2004 Proceedings Lncs 3180

Authors: Fernando Galindo ,Makoto Takizawa ,Roland Traunmuller

2004th Edition

3540229361, 978-3540229360

More Books

Students also viewed these Databases questions

Question

Explain in detail the different methods of performance appraisal .

Answered: 1 week ago

Question

What do you think of the MBO program developed by Drucker?

Answered: 1 week ago