Problem with WHILE loop

Hello all,

I have an exercise im working on and its not working due to the while loops i think.

this is what it gives me.

values for x

75 678 13 4

Starting at 75 it takes 12 steps to reach 1
The largest number in the series is 340 at position 4
Starting at 678 it takes 45 steps to reach 1
The largest number in the series is 4372 at position 30
Starting at 13 it takes 45 steps to reach 1
The largest number in the series is 4372 at position 30
Starting at 4 it takes 45 steps to reach 1
The largest number in the series is 4372 at position 30

this is what the correct output should be

Starting at 75 it takes 14 steps to reach 1
The largest number in the series is 340 at position 4
Starting at 678 it takes 51 steps to reach 1
The largest number in the series is 4372 at position 19
Starting at 13 it takes 9 steps to reach 1
The largest number in the series is 40 at position 2
Starting at 4 it takes 2 steps to reach 1
The largest number in the series is 4 at position 1




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

#include<iostream>
using namespace std;


int main()
{
  int x;
  int y;
  int a=1;
  int s=0;
  int j = 1;
  int i = 1;

  cin>>x;


  while (cin) // loop that will run formulas untill there is no more data
    {

  cout<< "Starting at "<<x; // this out puts the firts x values 

  while(a<= x)  // formula that runs and build the sequence untill it reaches 1
    {

 	a++;
 	y=x%2;
 	if(y==0)
 	{
 	x=x/2;
 	}
 	else
 	{
 	x=3*(x+1);
 	x=x-2;
 	}
      while (j<x) // loop that looks thru the sequence built by the formula above and finds the large number
        {

          j++;



          if (s<x) // finding the largest number in the sequence 
            {
              s=x;

            }
          while (i < a ) // the index number of the largest number
            {

              i++;
            }
            



        }

        }
    




  cout<<" it takes "<<a<<" steps to reach 1"<<endl;
  cout<< "The largest number in the series is "<<s<<" at position "<<i <<endl;



  cin>>x;
    }
    
  return 0;

}


Last edited on
closed account (48T7M4Gy)
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
#include<iostream>

using namespace std;
int main()
{
    int x{0}, xstart{0}, xmax{0}, imax{0};
    
    int count{1};
    
    cout << "Enter a number: ";
    cin >> xstart;
    x = xstart;
    
    while(x > 1)
    {
        if (x > xmax)
        {
            xmax = x;
            imax = count;
        }
        
        if(x % 2 == 0)
        {
            x=x/2;
        }
        else
        {
            x = 3*x + 1;
        }
        cout << x << ' ';
        
        
        count++;
    }
    
    cout
    << '\n'
    << "Starting at " << xstart << " it takes " << count << " steps to reach 1\n"
    << "The largest number in the series is "
    << xmax << " at position " << imax << '\n';
    
    return 0;
}


Enter a number: 678
339 1018 509 1528 764 382 191 574 287 862 431 1294 647 1942 971 2914
 1457 4372 2186 1093 3280 1640 820 410 205 616 308 154 77 232 116 58 29
 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 
Starting at 678 it takes 52 steps to reach 1
The largest number in the series is 4372 at position 19
Program ended with exit code: 0
Last edited on
Topic archived. No new replies allowed.