Hailstone Sequence Is Incorrect

I need help getting the first value of the longest hailstone sequences that begin from 1 to n. This is what I currently have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  /* next(n) prints the hailstone sequence starting at n. next(1) will never be 
computed by the program since no number follows 1 in the hailstone sequence. */

int next(int n)
   {
      int i = n;
      while(i != 1)
      {
         if(i % 2 == 0)
         {
            i = i/2;
         }
         else 
         { 
            i = 3 * i + 1;
         }
         return (i);
      }
   }

//printHailstone(n) prints the hailstone sequence starting at n.

   void printHailstone(int n)
   {
      int i = n;  
      if(i == 1)
      {
         printf("%i ", i);
      }
      else
      {
         printf("%i ", i);
         printHailstone(next(i));
      }
   }

// length(n) returns the length of the hailstone sequence of n.

   int length(int n)
   {
      int length = 1;
      if(n == 1)
      {
         return 1;
      }
      while(n != 1)
      {
         n = next(n);
         length= length+1;
      } 
      return length;
   }

/* hailstoneMax(n) returns the largest number in the hailstone sequence 
starting with n. */

   int hailstoneMax(int n)
   {
      int maxNum = 0;
      if(n == 1)
      {
         return n;
      }
      while(n != 1)
      {
         if(n > maxNum)
         {
            maxNum = n;
         }
         n = next(n);
      }
      return maxNum;
   }

/* longestHailstone(n) returns the longest of the hailstone sequences that 
start with numbers from 1 to n. */

   int longestHailstone(int n)
   {
      int longest = 1;
      for(int i = 1; i < n; i++)
      {
         if(length(i+1) > length(i))
         {
            longest = length(i+1);
         }
      }
      return longest;
   }

/* firstOfLongest(n) returns the first number of the longest hailstone sequence
that starts with numbers from 1 to n. */
int firstOfLongest
   int count = 1;
   {
      int i = 1;
      while(i < n)
      {
         
         if(length(i+1) > length(i))
         {
            count = count+1;
         }
         i++;
      }
      return count;
   }


The output for 3 is:
What number do you want to start the hailstone sequence with?
3
You are starting the hailstone sequence with 3.
3 10 5 16 8 4 2 1
The length of the hailstone sequence is 8.
The largest number in the sequence is 16.
The longest sequence starting with a number from 1 to 3 has a length of 8.
The longest sequence starting with a number from 1 to 3 begins with 3.

Anything above 3 is usually incorrect on the last line of the output, for example 9 gives an output of 7 on the last line and 7 and 8 give an output of 6 on the last line.

What am I doing wrong for that last portion?(I have to only use loops for this assignment.)
Last edited on
Topic archived. No new replies allowed.