quick description:

f(n)=n/2 if even
f(n)=3*n+1 if odd

with given start value it can step back and forth between the two eqtns. Example: f(7)=22, f(22)=11, f(11)=34, f(34)=17


You enter two start values. first and last start value and the program should generate all values in between.
With a given max value the looping will stop
or stop when the given step value is reached.
It should output the value it's testing and the highest "f(n)" of that value.

I hope u guys can read my messy beginner code...something is wrong with the loop/loops cuz no values is printed out.

#include<iostream>
using namespace std;

int main()
{
  int limit;
  int steps;
  int multiple = 0;
  int start;
  int start_last;
  int highest = 0;
  int a;
  int b;

  
  cout << "First start value: ";
  cin >> start;
  cout << "Last start value: ";
  cin >> start_last;
  cout << "Max value: "; 
  cin >> limit;
  cout << "How many steps: ";
  cin >> steps;

  cout << endl << "start_value max_value"
          << endl << "_ _ _ _ _ _ _ _ _ _ _ _ " << endl;

 for ( int i = 0; start_last >= start +i; ++i)
 {
     b = start +i;     // <-- Somethings not right here on this line.
                            // I think... 
   while (b <= limit || multiple <= steps)  // <- because b never gets 
  	{                                             // in here and it gets stuck. 
	   if (b%2 == 0)  //even number
	     {
	       a = b/2;
	     }
	   else               //else odd number 
	     {
	       a = 3*b+1;
	     }
	   
	   if (a > highest)
	     {
	       highest = a;
	     }   
 
 	   b = a;
	   ++multiple;
	}
      cout << start +i << "         " << highest <<  endl;
      highest = 0;  //reset highest
      multiple = 0; //reset multiple
}

return 0;
}

One thing I notice is that on line #48 you make this assignment:

b = a;

and then on line #30 you totally undo the previous operation with each loop iteration:

b = start +i;

So I'm not sure what you are trying to do, but something here doesn't make sense.

Edited 7 Years Ago by Clinton Portis: n/a

I agree with Clinton about the b=a thing. I personally think I need another example as to which is which value, but on the surface I think you might need && instead of || in your while loop. You want to make sure if either one of the conditions isn't true than stop the loop (while loop is "on" with a true value).

hi, yes.

b = start +i;
is suppoaed to generate all the start values between the two inputed start values.

the other b = a; is just within the while loop and is the f(n) i use to check the limit with.

Make use of function.

1) Create a function that does this f(34)=17 , for a given value.

Make sure it works.

For use a for loop with from start to finish and just call that function.

It will be more readable, better code/practice and you will
get a better grade.

This article has been dead for over six months. Start a new discussion instead.