Save Given a positive integer n, repeating the following calculations will event
ID: 647855 • Letter: S
Question
Save Given a positive integer n, repeating the following calculations will eventually reduce n to l: if n is even then di multiply n by 3 and add 1 to the product. Write a function to test this hypothesis. The function's prototype is int testnumber(int n, int times); where n is the test number and times is the maximum number of times the function may perform the calculations. The function retums th number of times it actually performs the calculations. It is possible that the function does not reduce some numbers to 1. This is the purpose of the second parameter. If the function does not reduce a numberto I then it vide n by 2; otherwise Find and select the file from your computer and then click on "Upload" to upload it Upload Browse... No file selected Review Check to review before finishing (will be flagged in Table of Contents) Prev Table of Contents Next _ Continue Later V Finish Instructions l Gateway| Etudes I Conduct Policy I Privacy Statement | © 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Etudes, Inc. All rights reserved. Etudes20150327 ares Etudes201 201272015 Eid2013032 .01s tudes, inc.All ights reserved. 2008, 2009, 2010, 2011,Explanation / Answer
#include <iostream>
using namespace std;
int testnumber(int n,int times){
if (times <= 0 || n == 1)
return 0;
if (n % 2 == 0)
return 1+testnumber(n/2,times-1);
return 1+testnumber(3*n+1,times-1);
}
int main() {
int n;
int times;
cout << "
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.