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 function that tells whether a hailstone sequence contains a number that is greater than...
Write a function that tells whether a hailstone sequence contains a number that is greater than 1000. Write a contract, then an implementation, of a function that takes exactly one parameter, an integer n, and returns true if the hailstone sequence starting with n contains a number that is greater than 1000, and returns false otherwise. The heading must be as follows. bool bigHailstone(int n) This function must not read or write anything. Your algorithm for bigHailstone must be a...
C++ The minimum function. (a) Write a function that takes two integers and returns the value...
C++ The minimum function. (a) Write a function that takes two integers and returns the value of the smaller one. In the main() function provide 5 test cases to verify its correctness. (b) Write the function that takes two characters and return the smaller one in the lexicographical order. Write the main() function that tests that functions for 5 different pairs of character type variables. (c) Write a generic function that takes two numeric objects and returns the value of...
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...
a. Write a function sumDigits that takes a positive integer value and returns the total sum...
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 Runs: Enter a positive integer: 1000000 The sum of the digits in the number from 1 to 1000000 is 27000001 Enter a positive...
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(____________________________) { }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT