i'm trying to write a program that takes two numbers from the user prints out the sequence between the 2 numbers and then outputs wthe longest sequence

for example if the user inputed 8 and 10

it should out put

8 4 2 1
9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
10 5 16 8 4 2 1

i got the code as follows that does it for 1 number

#include <iostream>
using namespace std;


int f(int n);


int main()
{
int first;
cout << "Enter First Integer: ";
cin >> first;


while(first != 1) //test ithat first is not qual to 1
{
cout <<  first << " ";



first = f(first);//call function


}
cout << first << endl;


return 0;
}


int f(int n)
{


if(n %2 == 0)//test to see if even
{
return n /=2;


}
else
{
return n = 3*n+1;//is odd


}


}

i need it to do it for two numbers and out but the sequence between those two numbers and then compare them and output that number 9 is the longest sequence it contained 20 numbers any help with this would greatly be appreeciated. forgive me i'm new to the forum and i'm not sure how to use
the code tags.

Edited 3 Years Ago by happygeek: fixed formatting

Here is the code so far now where i'm stuck is how to put the first number into a temp variable call my function and then store the count into a variable and compare the count with the next number called after calling the function.

For example the numbers entered are 8 and 10
after the first call to my function the result is:

8 4 2 1 count = 4

next call

9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 count2 = 20

last call
10 5 16 8 4 2 1 count3 = 7

once i compared the counter i found the longest sewuence out put the longest sequence, i'm stuck as to where to count and then compare the reults here is what i have so far.

do you know how to use interpunction?
your ramblings are completely incomprehansible.

Attachments
/*************************************************************************/
/*                                                                       */
/*                    WRITTEN BY: COREY GRITSCH                          */
/*                    Program   : 3.3 Chapter 3 Pg 96                    */
/*                    Course    : CMPSC 202                              */
/*                    Instructor: Dr. Appel	                             */
/*                    Date      : 9/20/04							     */
/*																		 */
/*   This program demonstrates basic mathematical function using c-math  */
/*   It takes a real number and raises it to a given power               */
/*                                                                       */
/*************************************************************************/


#include <iostream>
using namespace std;

int Hailstone(int n);



int main()
{
	int first,second;
	int count = 0;
	int minnum = 0;
	cout << "Enter First Integer: ";
	cin >> first;
	cout << "Enter Second Integer: ";
	cin >> second;
	//count = second;

	for(int i = first;i <= second;i++)
	{
		first = i;
		
		while(first != 1)
		{
			cout << first << " ";
			first = Hailstone(first);
			count++;
		}

		cout << first << " ";
		
	}
	cout << i << endl;
	return 0;
}
	/*while(first <= second)
	{
		
		cout << f(first) << " ";
	
		
	}
	cout << count;
	return 0;
}*/
   

int Hailstone(int n)
{
	
	if(n %2 == 0)//test to see if even
	{
		n /=2;
	}
	else
	{
		n = 3*n+1;//is odd
	}
	return n;
	//while(n != 1)
	//{
	//	n = f(n);
	//}
	
}

thanks everyone for your help, i no longer need help with this program, got it all figured out and it's working perfectly.

Could you post your working code for the benifit of the community? Thanks in advance.

For the benifit of the community could you refrain from digging up threads almost 2 years old ? Thanks in advance.
If you have the same problem please create a new thread with your effort upto now. :) If you want you can post a link to this thread from your post, so that we know what you are talking about.

This question has already been answered. Start a new discussion instead.