Hailstone sequence

I cant figure out how to return the largest number in a hailstone sequence while using a loop and no recursion.

My sequence is {7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}

The answer my code is giving me is 22.

1
2
3
4
5
6
7
8
9
10
11
  int largestNum(int n)
{
	int t = 0;
	int k = 0;
	while(k != n)
	{
		k++;
		t = max(next(n), length(n));
	}
	return t;
}
closed account (LNboLyTq)
Hi Falcon612,

Can you show us the function next() and the function length()?

Andy
Falcon612,

Try this:
1
2
3
4
5
6
7
8
int sequence[]{ 7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1 };
int max{ 0 };

for (int i = 0; i < sizeof(sequence) / sizeof(*sequence); i++)
{
	if (max < sequence[i])
		max = sequence[i];
}
Topic archived. No new replies allowed.