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;
}
|