Question

In: Computer Science

16. Write a function that returns the start value of a hailstone sequence that contains the...

16. Write a function that returns the start value of a hailstone sequence that contains the largest value that was reported by largestInAnyHS(n).

Write a contract, then an implementation, of a function that takes exactly one parameter, an integer n, and returns the start value from 1 to n of the hailstone sequence that contains the largest value. The heading must be

  int startHSWithLargest(int n)

This function must not read or write anything.

Modify your main function so that it also shows the result of this function.


C++

Solutions

Expert Solution

Please give thumbs up, thanks

sample output:

code:

#include<iostream>
#include<vector>
using namespace std;
int startHSWithLargest(int n)
{
   int maxvalue=-99999;
   int maxforN=-9999;
   for(int i=1; i<=n; i++)
   {
       int number=i;
           while(number>1)
           {
               if(maxvalue<number)
               {
                   maxvalue=number;
                   maxforN=i;
               }
           if(number%2==0)
               number=number/2;
           else
               number=3*number +1;
           }
   }
   return maxforN;
}
int main()
{
   int number=startHSWithLargest(30);
   cout<<"Number is : "<<number<<endl;
   return 0;
}


Related Solutions

In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns...
In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns the highest similarity score found in the genome as a double. Note: the term genome refers to the string that represents the complete set of genes in an organism, and sequence to refer to some substring or sub-sequence in the genome. Your function MUST be named findBestSimScore Your function should take two parameters in this order: a string parameter for the genome (complete set...
Write a Python function that returns a list of keys in aDict with the value target....
Write a Python function that returns a list of keys in aDict with the value target. The list of keys you return should be sorted in increasing order. The keys and values in aDict are both integers. (If aDict does not contain the value target, you should return an empty list.) This function takes in a dictionary and an integer and returns a list. def keysWithValue(aDict, target): ''' aDict: a dictionary target: an integer ''' # Your code here
Write a function called draw_card. It takes no arguments and returns an integer representing the value...
Write a function called draw_card. It takes no arguments and returns an integer representing the value of a blackjack card drawn from a deck. Get a random integer in the range 1 to 13, inclusive. If the integer is a 1, print "Ace is drawn" and return 1. If the integer is between 2 and 10, call it x, print "<x> is drawn" and return x (print the number, not the string literal "<x>"). If the number is 11, 12,...
Write a function that returns the largest value in a stack (only use push and pop)
Write a function that returns the largest value in a stack (only use push and pop)
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the...
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the total sum of the digits in the integers from 1 to that number inclusive. b. Write a program to input an integer n and call the above function in part a if n is positive, else give ‘Value must be Positive’ message. sample: Enter a positive integer: 1000000 The sum of the digits in the number from 1 to 1000000 is 27000001 Enter a...
1) Write a function equation(A, B) that returns a value C where A + B =...
1) Write a function equation(A, B) that returns a value C where A + B = C a. Write the equation function: b: Write the output of your function for a few values: 2) Write a non-recursive function called fact(n) that computes n! Try computing fact(n) for large values. Can you find the maximum value for n your program will compute correctly? a) Write your fact function: b) Write the output of your function for a few values: 3) Write...
Write a function maxFun() that returns the maximum value in any given integer array. Determine the...
Write a function maxFun() that returns the maximum value in any given integer array. Determine the function parameters (complete the area shown in ___________. ___________maxFun(____________________________) { }
JavaScript Write a function named "reverse_kvs" that has a key-value store as a parameter and returns...
JavaScript Write a function named "reverse_kvs" that has a key-value store as a parameter and returns a new key-value store. Your function should add each key-value pair in the parameter to the new key-value store EXCEPT reversing the key and value. For example, if the key-value "extreme":45 were in the parameter then the returned key-value store should contain the key-value pair 45:"extreme". Code: function reverse_kvs(store) { var reverse_store = {}; for (var key in store) { if (store.hasOwnProperty(key)) { reverse_store[store[key]]...
Write a function called integerPower(base, exponent) that returns the value of baseexponent For example, integerPower(3,4) =...
Write a function called integerPower(base, exponent) that returns the value of baseexponent For example, integerPower(3,4) = 3*3*3*3 Assume that exponent is a non-zero integer, and base is an integer. Function integerPower should use for loop to control the calculation. Do NOT use any math library function. Inputs for this program, which are base and exponent, have to be entered by the user An example of input/output dialog is shown below: Enter Integer Base: 5 Enter Integer Exponent: 3 5 raised...
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT