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

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...
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(____________________________) { }
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...
Given a singly linked list that contains a sequence of integers, write a method that loop...
Given a singly linked list that contains a sequence of integers, write a method that loop through each elements in this singly linked list with O(n) time complexity, and let each elements multiply 6, return the result. code needed in java! thanks in advance!
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and returns a new sequence that is twice the length of the parameter sequence but that contains the contents of the original in palindrome form. For example, if the sequence "super" is passed into the function, the function will return "superrepus".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT