I'm trying to create this pattern: http://mathworld.wolfram.com/Rule60.html

I cannot get my code to print this. It runs, it just does not print the right pattern. I know I am doing something wrong, I'm just not sure what.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	//Declare variables
	bool line[78];
	bool nextLine[78];
        void firstline();
        void coutline();
	void rules();
        void coutnextline();
	int r;


        //Create first line
        firstline();

        //Cout Line
        coutline();

        //Next Lines
        for (r = 0; r < 15; r++)
        {
                rules();
                coutnextline();
        }

	cin.get();
	return 0;
}

//Create first line
void firstline ()
{
//declare variables
        bool line [78];
        int y;

        for(y = 1; y < 78; y++)
        {
                if(y != 39)
                line[y] = 0;
                else if(y == 39)
                line[y] = 1;
        }
}

//Cout line
void coutline()
{
//declare variables
        bool line [78];
        int z;

        for(z = 0; z <= 78; z++)
        {
                if(line[z] == 0)
                        cout << " ";
                else if(line[z] == 1)
                        cout << "#";
        }
}


void rules ()
//Put line in and follow 8 rules
// Rule: 1   2   3   4   5   6   7   8
//      111 110 101 100 011 010 001 000
//       0   0   0   1   1   1   1   0
{
//Declare variables
        bool line[78];
        int x;
	bool nextLine[78];
	for (x=1; x < 78; x++)
	{
		//rule 1
		if (line[x-1] == 1 && line[x] == 1 && line [x+1] == 1)
			nextLine[x] = 0;
                //rule 2
                else if (line[x-1] == 1 && line[x] == 1 && line [x+1] == 0)
			nextLine[x] = 0;
                //rule 3
                else if (line[x-1] == 1 && line[x] == 0 && line [x+1] == 1)
			nextLine[x] = 0;
                //rule 4
                else if (line[x-1] == 1 && line[x] == 0 && line [x+1] == 0)
			nextLine[x] = 1;
                //rule 5
                else if (line[x-1] == 0 && line[x] == 1 && line [x+1] == 1)
			nextLine[x] = 1;
                //rule 6
                else if (line[x-1] == 0 && line[x] == 1 && line [x+1] == 0)
			nextLine[x] = 1;
                //rule 7
                else if (line[x-1] == 0 && line[x] == 0 && line [x+1] == 1)
			nextLine[x] = 1;
                //rule 8
                else if (line[x-1] == 0 && line[x] == 0 && line [x+1] == 0)
			nextLine[x] = 0;
        }
}

void coutnextline()
{
//declare variables
        bool nextLine [78];
        int a;

        for(a = 0; a <= 78; a++)
        {
        if(nextLine[a] == 0)
                        cout << " ";
        else if(nextLine[a] == 1)
                        cout << "#";
        }
}

Your line[] array is local to each function, so your values really won't transfer between the two functions. Do you know how to pass arguments to functions ? If so then pass in a reference to your array as the argument for each function.

commented: Beaten! +1

I'm trying to create this pattern: http://mathworld.wolfram.com/Rule60.html

I cannot get my code to print this. It runs, it just does not print the right pattern. I know I am doing something wrong, I'm just not sure what.

int main()
{
	//Declare variables
	bool line[78];
	bool nextLine[78];
        void firstline();
        void coutline();
	void rules();
        void coutnextline();
	int r;


        //Create first line
        firstline();
...
//Create first line
void firstline ()
{
//declare variables
        bool line [78];
 ...
void coutline()
{
//declare variables
        bool line [78];

The bool line[78] declared in main() has nothing to do with bool line[78] delared in firstline() and other functions. All of them are distinct entities. firstline() initializes some local memory, which ceases to exist as soon as firstline returns. coutline() prints some uninitialized garbage. Etc.

You should either make your lines global (bad solution) or wrap them in a class (slightly better) or pass them around as parameters.

I didn't look at your generating code. At this moment it is irrelevant.

I attempted to fix it, though I have a feeling that I am doing the exact same thing wrong. I'm not getting the right pattern.

#include <iostream>
#include <fstream>

using namespace std;

bool line[78];
void firstline(bool []);
void coutline(bool []);
void rules(bool []);

int main()
{
	//Declare variables
	int r;


        //Create first line
        firstline(line);

        //Cout Line
        coutline(line);

        //Next Lines
        for (r = 0; r < 16; r++)
        {
                rules(line);
        }

	cin.get();
	return 0;
}

//Create first line
void firstline(bool firstline[])
{
	int y;
        for(y = 0; y <= 78; y++)
        {
                if(y != 38)
                firstline[y] = 0;
                else if(y == 38)
                firstline[y] = 1;
        }
}

//Cout line
void coutline(bool coutline[])
{
	int z;
        for(z = 0; z <= 78; z++)
        {
                if(coutline[z] == 0)
                        cout << " ";
                else if(coutline[z] == 1)
                        cout << "#";
        }
}


void rules (bool rules[])
//Put line in and follow 8 rules
// Rule: 1   2   3   4   5   6   7   8
//      111 110 101 100 011 010 001 000
//       0   0   1   1   1   1   0   0
{
	int x;
	bool nextLine[78];
	for (x=1; x < 78; x++)
	{
		//rule 1
		if (rules[x-1] == 1 && rules[x] == 1 && rules[x+1] == 1)
			nextLine[x] = 0;
                //rule 2
                if (rules[x-1] == 1 && rules[x] == 1 && rules[x+1] == 0)
			nextLine[x] = 0;
                //rule 3
                if (rules[x-1] == 1 && rules[x] == 0 && rules[x+1] == 1)
			nextLine[x] = 1;
                //rule 4
                if (rules[x-1] == 1 && rules[x] == 0 && rules[x+1] == 0)
     			nextLine[x] = 1;
                //rule 5
                if (rules[x-1] == 0 && rules[x] == 1 && rules[x+1] == 1)
			nextLine[x] = 1;
                //rule 6
                if (rules[x-1] == 0 && rules[x] == 1 && rules[x+1] == 0)
			nextLine[x] = 1;
                //rule 7
                if (rules[x-1] == 0 && rules[x] == 0 && rules[x+1] == 1)
			nextLine[x] = 0;
                //rule 8
                if (rules[x-1] == 0 && rules[x] == 0 && rules[x+1] == 0)
			nextLine[x] = 0;
        }
	coutline (nextLine);
}
void rules (bool rules[])
{
	bool nextLine[78];
...
}

What argument is passed to rules ? Does is relate to nextLine ?

Almost everywhere you have a loop and access your arrays you have an out of bounds array access, for line declared as

bool line[78];

then if it is accessed as line[N] N must conform to the constraint 0 <= N < 78

However

void firstline(bool firstline[])
{
	int y;
        for(y = 0; y <= 78; y++)
        {
                if(y != 38)
                firstline[y] = 0;

y can be 78 - out of bounds

void coutline(bool coutline[])
{
	int z;
        for(z = 0; z <= 78; z++)
        {
                if(coutline[z] == 0)

z can be 78 - out of bounds

void rules (bool rules[])
//Put line in and follow 8 rules
// Rule: 1   2   3   4   5   6   7   8
//      111 110 101 100 011 010 001 000
//       0   0   1   1   1   1   0   0
{
	int x;
	bool nextLine[78];
	for (x=1; x < 78; x++)
	{
		//rule 1
		if (rules[x-1] == 1 && rules[x] == 1 && rules[x+1] == 1)
			nextLine[x] = 0;

x+1 can be 78 - out of bounds

Also it is bad practice to litter your code with the magic value 78 when in call cases that value has the same semantic meaning, width of the pattern being created. declare a const int at the top of the file with a good semantic name and use that, then if the width of your pattern changes you only have to change 1 place.

Thanks for all the help so far. I'm just starting c++ again and I keep making a bunch of stupid mistakes. Anyway. I have another question now.

In rules:

void rules (bool rules[])
{
	int x;
	for (x=1; x < width; x++)
	{
		//rule 1
		if (rules[x-1] == 1 && rules[x] == 1 && rules[x+1] == 1)
			line2[x] = 0;
                //rule 2
                else if (rules[x-1] == 1 && rules[x] == 1 && rules[x+1] == 0)
			line2[x] = 0;
                //rule 3
                else if (rules[x-1] == 1 && rules[x] == 0 && rules[x+1] == 1)
			line2[x] = 1;
                //rule 4
                else if (rules[x-1] == 1 && rules[x] == 0 && rules[x+1] == 0)
			line2[x] = 1;
                //rule 5
                else if (rules[x-1] == 0 && rules[x] == 1 && rules[x+1] == 1)
			line2[x] = 1;
                //rule 6
                else if (rules[x-1] == 0 && rules[x] == 1 && rules[x+1] == 0)
			line2[x] = 1;
                //rule 7
                else if (rules[x-1] == 0 && rules[x] == 0 && rules[x+1] == 1)
			line2[x] = 0;
                //rule 8
                else if (rules[x-1] == 0 && rules[x] == 0 && rules[x+1] == 0)
			line2[x] = 0;
        }

        coutline(line2);
}

At first I just had line instead of line2, but I realized that affected my pattern because I was changing the line that I was inputing too. Now I would like to contine this rule for as many itterations necessary. I would like to just put something like:

line2[x] = line[x];

but I know that doesn't work.

How can I do this? Right now it will only print the first 2 lines.

I would say use vector<bool> instead of your array however vector<bool> is not a standard vector and that may cause issues.

However I notice that rather than assigning true or false you always assign 1 or 0 to your array elements which means you could just as easily use int (or char if you are space concious) as vector<int> line; You would need to size the vector before using it but then if you had vector<int> line2; you could just do this line = line2; And you could still access the vector as if it was an array using the [] operator.

Generally when using C++ using vector rather than arrays should be preferred.

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.