Question
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.
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
{
complete
}
int sumMultiples(const vector
complete
return 0;
}
void greaterThanK(vector
{
complete
}
void pivot(vector
{
complete
}
bool isSubarray(const vector
{
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
void timePrimes();
int main(){
int question = -1;
cout
cin >> question;
int n = 0, k = 0, m = 0;
vector
string s = "";
vector
vector
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
* n - int - the number of integers to read *
* ************************************************/
void readIntVector(vector
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
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started