Hello!

I'm just starting to learn C++ for school, and our first assignment is creating a series of numbers according to the following rule:

current value                 next value
1                                 none - the sequence terminates
even                            current / 2
odd                              3*current + 1

I have to compute for the initial values 1-60. My task is to count the length o the series for each of the initial values, so the outcome should be:

series length          number of occurences
1                         1
2                         1
3                         1
4                         1
5                         1
6                         2
7                         1
8                         3
9                         3
...
40                       0

so far, my code is :

// SeriesLength.h

#pragma once

using namespace System;

namespace SeriesLength {

	int count = 0;

	int series(int seed){

		if (seed == 1){
			count ++;
			return count;
		}

		if (seed == 2 || seed == 4 || seed == 6 || seed == 8 || seed == 10|| seed == 12 || seed == 14 || seed == 16){
			seed = seed / 2;
			count ++;
		}
		else{
			seed = seed * 3 + 1;
			count ++;
		}
		series(seed);

		return count;
	}

	int main(){
		const int MAX_NUM = 4;
		const int MAX_CALLS = 41;
		
		int num_calls[MAX_CALLS];
		//int length = 0;
		//int occurences = 0;

		cout << "series length \t number of occurences";

		for (int i = 0; i < MAX_NUM; i++){
			//cout << length << occurences;
			num_calls[i]++;
		}

		for (int i = 0; i < MAX_NUM; i++){
			//cout << length << occurences;
			cout << num_calls[i];
		}
		return 1;
	}
}

however, the compiler's complaining about my 'cout' and i don't know what to do...help would be GREATLY appreciated, as this assignment is due tomorrow.

thanks!

Hong

Recommended Answers

All 12 Replies

How about using namespace std; ?

Hello!

I'm just starting to learn C++ for school, and our first assignment is creating a series of numbers according to the following rule:

current value                 next value
1                                 none - the sequence terminates
even                            current / 2
odd                              3*current + 1

I have to compute for the initial values 1-60. My task is to count the length o the series for each of the initial values, so the outcome should be:

series length          number of occurences
1                         1
2                         1
3                         1
4                         1
5                         1
6                         2
7                         1
8                         3
9                         3
...
40                       0

so far, my code is :

// SeriesLength.h

#pragma once

using namespace System;

namespace SeriesLength {

	int count = 0;

	int series(int seed){

		if (seed == 1){
			count ++;
			return count;
		}

		if (seed == 2 || seed == 4 || seed == 6 || seed == 8 || seed == 10|| seed == 12 || seed == 14 || seed == 16){
			seed = seed / 2;
			count ++;
		}
		else{
			seed = seed * 3 + 1;
			count ++;
		}
		series(seed);

		return count;
	}

	int main(){
		const int MAX_NUM = 4;
		const int MAX_CALLS = 41;
		
		int num_calls[MAX_CALLS];
		//int length = 0;
		//int occurences = 0;

		cout << "series length \t number of occurences";

		for (int i = 0; i < MAX_NUM; i++){
			//cout << length << occurences;
			num_calls[i]++;
		}

		for (int i = 0; i < MAX_NUM; i++){
			//cout << length << occurences;
			cout << num_calls[i];
		}
		return 1;
	}
}

however, the compiler's complaining about my 'cout' and i don't know what to do...help would be GREATLY appreciated, as this assignment is due tomorrow.

thanks!

Hong

Include the header file iostream.h. Coz it is the header file containing cout and cin.

So now I redid my code and sorted out a few glitches in logic, so the new code is:

include <iostream>
using namespace std;

#include <assert.h>

#include "/unc/hedlund/121/lib/cpluslib.h"



int counter = 0;
int counter2 = 0;

int series(int seed){



        cout<< "seed = " << seed << "\n";
        if (seed == 1){
                counter2 = counter ++;
                counter = 0;
                cout << "counter = " << counter2 << "\n";
                return counter2;
        }
        if (odd(seed)){
                seed = seed * 3 + 1;
                counter ++;
        }

        else{
                seed = seed / 2;
                counter ++;
        }

        series(seed);

        return counter;

}

int main(){
        const int MAX_NUM = 10;

        const int MAX_CALLS = 41;

        int num_calls[MAX_CALLS];
          int num_calls[MAX_CALLS];
          
        int a;

        cout<< "series length \t number of occurences\n";

        for (int i = 1; i < MAX_NUM; i++){
                a = series(i);
                cout << "a = " << a << "\n";

                num_calls[a] = num_calls[a]+1;

                //cout << num_calls[a];
        }

        for (int i = 1; i < MAX_NUM; i++){
                cout << i << "\t" << num_calls[i] << "\n";
        }

        return 1;
}

When I print out the counter and the "a" that the series function should return to, a is always 0 for some reason... what am I doing wrong?

btw - the odd(seed) function is something my professor gave me, so I know that's working properly...

thanks!

Hong


int counter = 0;

Hong

Try changing the declaration of counter variable as a local variable and check. I couldnt work out and see coz u havent included the header files. declare int counter=0 inside the serial function.

Beuls

I'm unable to post the file because that's something my professor gave us and told us to write that particular line (we log into a school unix server) so once we put that line up there, we can use the odd/even function... and the local variable didn't work either. Is there something wrong with my logic?

thanks!

Hong

can u pls explain ur prob a little more? coz i cant get u....actually where will the series end?

pls give me some examples for ur series...
eg. if it starts with 1, the series is .........
with 2, it is ......... and so on

Beuls

if the current seed is say a 4, the series will go 4, 2, 1. if it was a 3, it will go 3, 10, 5, 16, 8, 4, 2, 1.

try this one

#include <iostream.h>
#include <conio.h>
<code>
int series(int seed)
{
int count=0;
if (seed==1)
return 1;
while (seed>1)
{
if (seed%2==0)
seed=seed/2;
else
seed=3*seed+1;
count++;


}
return count;
}
void main()
{
int i,a;
cout<<"Seed           Series Length"<<endl;
for (i=1;i<=40;i++)
{
a=series(i);
cout<<i<<"                         "<<a<<endl;
}


getch();


}
</code>

You can use ur odd and even functions if u want

Beulah

beuls use [ ] instead of < > for code tags

thank u

thanks everyone!

Is your problem solved or not?

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.