Hello I have just started learning programming & this problem is really stumping me. I have to do a syracuse sequence (explained below) & I know my code below is very wrong, (it needs to use for statements in there) but can you give me any advice on what to do?

Let n be a positive integer and f(n) be the transformation that sends n to n/2 if n is even, and sends n to 3n+1 if n is odd. Starting with a positive value u called the seed, the sequence of integers iteratively generated by f
and u is called a Syracuse sequence.

For example, starting with the seed u = 1, the subsequent terms of the sequence are 4, 2, and 1.
The length of the sequence (excluding the seed) is therefore 3.

For u = 4, the next terms are 2 and 1. The length is 2.

For u = 404, the next terms are 202, 101, 304, 152, 76, 38, 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40,
20, 10, 5, 16, 8, 4, 2, and 1. The length is 27.

Some examples of the program execution are as follows. The entered value is shown in bold.

Example 1
Enter a positive number: 4
The sequence length is: 2
The maximum is: 2

Example 2
Enter a positive number: 12
The sequence length is: 9
The maximum is: 16

#include <iostream>
#include <string>
using namespace std;

int check_odd(int& n);  //checks if n is an even or odd number then does syracuse fucntion

int main()
{
	int n;

	cout << "Enter a positive number: ";
	cin >> n;
	cout << "n= "<< n << endl;

	check_odd(n);

	return 0;
}

int check_odd(int& n)
{
	int co = 0; // variable to count how many times the equation n/2 or 3n + 1 is done
	while (n >= 1)
		if (n % 2 == 0) {
			cout << "The number " << n << " is even" << endl;
			n = (n/2);
			co++;
			cout << "f(n) = " << n << "  co = "<< co << endl;
		}

		else if (n % 2 != 0) {
			cout << "The number " << n << " is odd" << endl;
			n = ((3 * n) + 1);
			co++;
			cout << "f(n) = " << n << "  co = "<< co << endl;
		}

}

I have broken it up into smaller problems, ie
--check whether n is an even or odd number - I can do that
-- if odd, perform 3n + 1 , if even, n/2 (expression) - kindof can do this
-- if n is greater than 1, count how many times the expression is performed. - Not working at all

Recommended Answers

All 10 Replies

-- if n is greater than 1, count how many times the expression is performed. - Not working at all

co is being incremented just fine.

co is being incremented just fine.

But when I do a run of the program, when co is displayed it reads like 2113 and stuff which is wrong?

I am a newbie as well , so check carefully. I slightly modified your code and it seems to work well:

#include <iostream>
#include <string>
using namespace std;

 void check_odd(int& n);  //checks if n is an even or odd number then does syracuse fucntion

int main()
{
    int n;

    cout << "Enter a positive number: ";
    cin >> n;
    cout << "n= "<< n << endl;

    check_odd(n);

    return 0;
}

 void check_odd(int& n)
{
    int co = 0; // variable to count how many times the equation n/2 or 3n + 1 is done
    int initial;
    int max = 0;
    while (n > 1)
    {
        if (n % 2 == 0) 
        {

            n = (n/2);
            co++;

            if (max < n)
                max = n;
        }

        else if (n % 2 != 0) 
        {

            n = ((3 * n) + 1);
            co++;
                if (max < n)
                max = n;

        }

    }
cout << "The sequence length is: "<<co << endl;
cout << "The maximum is: "<<max<<endl;
}

Shabtai, could you please post using code tags ?

Member Avatar for stephen.id

Shabtai, could you please post using code tags ?

somtimes if you copy and paste the code that isnt in tags its not usaully formatted to be compatible with your IDE, somtimes a solution is to copy and paste that code and paste it into another prorgam like notepad or wordpad

somtimes if you copy and paste the code that isnt in tags its not usaully formatted to be compatible with your IDE, somtimes a solution is to copy and paste that code and paste it into another prorgam like notepad or wordpad

Please explain more, I have no idea what I am supposed to do. I am a newbie.

Shabtai, could you please post using code tags ?

I have no idea what to d . Please explain or direct me to a FAQ in this site

I am a newbie as well , so check carefully. I slightly modified your code and it seems to work well:

#include <iostream>
#include <string>
using namespace std;

 void check_odd(int& n);  //checks if n is an even or odd number then does syracuse fucntion

int main()
{
    int n;

    cout << "Enter a positive number: ";
    cin >> n;
    cout << "n= "<< n << endl;

    check_odd(n);

    return 0;
}

 void check_odd(int& n)
{
    int co = 0; // variable to count how many times the equation n/2 or 3n + 1 is done
    int initial;
    int max = 0;
    while (n > 1)
    {
        if (n % 2 == 0) 
        {

            n = (n/2);
            co++;

            if (max < n)
                max = n;
        }

        else if (n % 2 != 0) 
        {

            n = ((3 * n) + 1);
            co++;
                if (max < n)
                max = n;

        }

    }
cout << "The sequence length is: "<<co << endl;
cout << "The maximum is: "<<max<<endl;
} 

end quote.

Thank you :D the way to calculate the max was also giving me alot of trouble & it was so simple too. I figured out how to do it also another way. Thanks

Thanks guys for your help :) especially with calculating the max.

end result

#include <iostream>
#include <string>
using namespace std;

int check_odd(int& n);  //checks if n is an even or odd number then does syracuse fucntion

int main()
{
	int n;

	cout << "Enter a positive number: ";
	cin >> n;
	cout << "n= "<< n << endl;

	check_odd(n);

	return 0;
}

int check_odd(int& n)
{
	int co = 0; // variable to count how many times the equation n/2 or 3n + 1 is done
	int max = 0;
	for (int i= 0; n != 1; i++)
		if (n % 2 == 0) {
			cout << "The number " << n << " is even" << endl;
			n = (n/2);
			co++;
			if (max < n)
				max = n;
			cout << "f(n) = " << n << "  co = " << co << " Maximum =  " << max << endl;
		}

		else if (n % 2 != 0) {
			cout << "The number " << n << " is odd" << endl;
			n = ((3 * n) + 1);
			co++;
			if (max < n)
				max = n;
			cout << "f(n) = " << n << "  co = "<< co << " Maximum =  " << max << endl;
		}

}

Thanks guys for your help :) especially with calculating the max.

end result

#include <iostream>
#include <string>
using namespace std;

int check_odd(int& n);  //checks if n is an even or odd number then does syracuse fucntion

int main()
{
	int n;

	cout << "Enter a positive number: ";
	cin >> n;
	cout << "n= "<< n << endl;

	check_odd(n);

	return 0;
}

int check_odd(int& n)
{
	int co = 0; // variable to count how many times the equation n/2 or 3n + 1 is done
	int max = 0;
	for (int i= 0; n != 1; i++)
		if (n % 2 == 0) {
			cout << "The number " << n << " is even" << endl;
			n = (n/2);
			co++;
			if (max < n)
				max = n;
			cout << "f(n) = " << n << "  co = " << co << " Maximum =  " << max << endl;
		}

		else if (n % 2 != 0) {
			cout << "The number " << n << " is odd" << endl;
			n = ((3 * n) + 1);
			co++;
			if (max < n)
				max = n;
			cout << "f(n) = " << n << "  co = "<< co << " Maximum =  " << max << endl;
		}

}

I do not think this will compile. Your function does not return any value, so return must be void in the prototype nad in the definition

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.