In: Computer Science
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.
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;
}