Solving Patterns w/ arrays. Well, trying.

My assignment is to create a series of these numbers starting with 1. I am really not sure at this point where my code is going wrong. I've worked through it with paper and pencil for the first two iterations and it seems as if it would work. It only outputs 1 for every reihe number though. If you need to know a what a reihe number is they work as such.
1
11 one one on the previous line
21 two ones on the previous line
1211 one two then one one on the previous line
111221 one onethen one two then two oneson the previous line

and so on...
here's my code thus far.
Any help greatly appreciated. Thank you!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//-------------------------------------\\
// Purpose: To compute the first thirty 
// iterations of a Reihe series.
// Name: Will Beeler
// Date: November 25, 2009
//-------------------------------------\\

#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std; 

int reiheNumber[10000];
int digitCount[3];
int elemCount;

// Find the number of occurrences of 1, 2 and 3
void CountDigits(int reiheNumber[], int digitCount[], int length);
// Display a Reihe number using 50 digits per line
void DisplayReihe(int reiheNumber[], int length);
// Display the number of occurences of 1, 2, and 3
void DisplayDigitCounts(int digitCount[]);
// Create the next reihe number
void CreateNextNumber(int reiheNumber[], int& length);

int main()
{  
	reiheNumber[0] = 1;
	elemCount = 1;
	cout << "Row   Length  One   Two Three          Reihe Number\n";
	CountDigits(reiheNumber, digitCount, elemCount);
	cout << "  1      1";
	DisplayDigitCounts(digitCount);
	DisplayReihe(reiheNumber,elemCount);
	for (int i=2; i<=30; i++)
	{
		CreateNextNumber(reiheNumber,elemCount);
		CountDigits(reiheNumber, digitCount, elemCount);
		cout << setw(3) << i << setw(7) << elemCount;
		DisplayDigitCounts(digitCount);
		DisplayReihe(reiheNumber,elemCount);
	} // end for
	return 0;
} // end main


void CreateNextNumber(int reiheNumber[], int& length)
{
	int reiheCopy[10000];
	int i = 0;
	int elem;
	int k = 0;
	int count = 1;

	//figure out how many elem there are
	while (reiheNumber[i] != 0)
	{
		if(i !=0)
		{
		length++;
		}
		i++;
	}
	
	i = 0;

	while(i < length)
	{
		while( reiheNumber[i] == reiheNumber[i+1] )
		{
			count++;
			elem = reiheNumber[i];
			i++;
		}

		reiheCopy[k] = count;
		reiheCopy[k+1] = elem;
		k = k+2;
		count = 1;
		i++;
	}

	//copy reiheCopy[] into reiheNumber[]
	for( int f(0); f < length; f++ )
	{
		reiheNumber[f] = reiheCopy[f];
	}
}


void CountDigits(int reiheNumber[], int digitCount[], int length)
{
	int i = 0;

	digitCount[0] = 0;
	digitCount[1] = 0;
	digitCount[2] = 0;

	while( i < length )
	{
		if( reiheNumber[i] == 1)
		{
			digitCount[0] = digitCount[0] + 1 ;
		}
		else if (reiheNumber[i] == 2)
		{
			digitCount[1] = digitCount[1] + 1;
		}
		else if (reiheNumber[i] == 3)
		{
			digitCount[2] = digitCount[2] +1;
		}

		i++;
	}
}

void DisplayDigitCounts(int digitCount[])
{
	cout << setw(6) << digitCount[0];
	cout << setw(6) << digitCount[1];
	cout << setw(6) << digitCount[2];
}

void DisplayReihe(int reiheNumber[], int length)
{


	for(int i(0); i < length; i++)
	{
		if(i == 0)
		{
			cout << setw(7);
		}

		if(i%50 == 0 && i != 0 )
		{
			cout << endl;
			cout << setw(34);
		}
		cout << reiheNumber[i];
	}
	cout << endl;
}

Last edited on
you have some trouble in CreateNextNumber().

reiheNumber[i+1] was always zero, so the count never incremented. you want to count digits until the element changes, so you may as well use elem.

try something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	while(reiheNumber[i] != 0) // more reliable than element count
	{
		count = 0; // start from zero
		elem = reiheNumber[i]; // get element to count
		while( reiheNumber[i] == elem ) // check for element change
		{
			count++;
			//elem = reiheNumber[i];
			i++;
		}

		reiheCopy[k] = count;
		reiheCopy[k+1] = elem;
		k = k+2;
		// count = 0;
		//i++;
	}


the number of digits grows sometimes, so use the length of reiheCopy[] instead of the length of reiheNumber[].
1
2
length = k;
//copy reiheCopy[] into reiheNumber[] 

man, those digits get long!
Topic archived. No new replies allowed.