Can't execute a while loop from a different function properly.

I can't execute the while loop in stateOne() line 116 properly, if i execute the function from stateTwo() line 256.

The idea behind the code is to simulate a turing machine. The quadruppels are
<S1, 1, move right, S1>
<S1, 0, write 1, S2>
<S2, 1, move left, S2>
<S2, 1, move right, S1>

so the machine should halt when it reads 5 times in a row a '1'. i think it simulates the function f(x) = x + 5 but im not sure if thats the right mathematical translation.

can you help why whe the programm gets "stuck" if stateTwo function calls stateOne function?

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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// TuringMachine.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

////////////////////////////////////////////////////TURING MACHINE///////////////////////////////////////////////////////////

//std libraries
#include <iostream>
#include <cstdlib>
#include <ctime>

//using namespace
using namespace std;

/*
//my files
#include "stateOne.cpp"
#include "stateTwo.cpp"


//my headers
#include "definitions.h"
#include "stateOne.h"
#include "stateTwo.h"
*/


//DEFINITIONS
#define READ cout << "\nMACHINE IS IN STATE " << state <<"! On place " << position << " the machine reads a " << tape[position] << ".";
#define PLACE cout << "\nPosition = " << position;
#define STEPS ;

//constants
const char HALT[] = "\n\n\t\t\t\tThe Turing Machine halts! Press any key for exit.";
const char LEFTEND[] = "\nThats the very end of the tape. The TM can't move left to place -1!";
const char RIGHTEND[] = "\nThats the very end of the tape. The TM can't move right to place 100!";
const char CONFINAL[] = "\n\t\t\t\tThe last 5 readings were a '1'.";									//condition when the machine is finished

//functions
//
//

int main()
{
	//variables
	int tape[100];
	int i = 0, n = 0;
	int START;
	char exit;

	//functions
	int stateOne(int tape[], int position);
	int stateTwo(int tape[], int position);

	srand(time(NULL));

	cout << "\n\t\t\t\tCreate a random Turing Machine tape with length 100:\n";

	for(n = 0; n <= 99; n++)												//fill array with random integers from 0 to 1.
	{
		i = rand() % 2;
		tape[n] = i;
		cout << tape[n];
	}

	for (n = 0; n <= 2; n++)												//3 trys to find a nice place to start.
	{
		cout << "\n\nOn what tape number should the TM start? (0-99)\n";
		cin >> START;

		while (START > 99 | START < 0)
		{
			cout << "\nThe entered number is either to big or to small. Please enter a number between 0 and 99.";
			cin >> START;
		}

		cout << "\nPlace " << START << " is a " << tape[START] << "\n";
	}

	cout << "\nMachine starts in S1.";

	stateOne(tape, START);													//here we go!!!

	cin >> exit;

	return 0;
}

//STATE 1
//
//
int stateOne(int tape[], int position)
{
	//vars
	int conFive = 0;
	int state = 1;

	//functions
	int stateTwo(int tape[], int position);

	while ((tape[position] == 1 | tape[position] == 0) && (position >= 0 || position <= 99))					//while reading 1 or 0 and array values are in scope...
	{

		//Dieser Code macht deutlich warum sich zweiwertige Variablen nicht selbst durch Ausschliessung bedingen. Obwohl der Ausdruck nur die Werte 1 und 0 bearbeitet, sagt
		//die Funktion <while (tape[position] == 1) ...>,  obwohl logisch äquivalent zu <while (tape[position] != 0) ...>, nicht das Gleiche aus!
		//Im ersten Fall läuft die Schleife nur 1x, weil die Bedingung immer wahr. Im Zweiten aber solange sie ungleich 0 ist. Schleifen sollten nicht POSITIVE Bedingungen erwarten!

		/*while (tape[position] == 1)
		{
		cout << "\nMACHINE IS IN STATE 1! On place " << position << " the machine reads a " << tape[position] << ".";
		position++;
		cout << "\nCOMMAND => move to place " << position << " and stay in S1.";
		}*/
				
		while (tape[position] != 0 && position <= 99)															//and while the TM is reading 1 ...
		{		
			//cout << "\nMACHINE IS IN STATE 1! On place " << position << " the machine reads a " << tape[position] << ".";
			READ;
			cout << "\n\t=> COMMAND: move to place " << position + 1 << " and stay in State " << state << ".";
			position++;																							//move right
			conFive++;																							//increment final halt condition

			if (position == 99 || position > 99)																//if the tape ends...
			{	
				PLACE;
				cout << RIGHTEND;
				cout << HALT;
				return 0;
				break;
			}

			if (conFive == 5)																					//or if final halt condition is true
			{
				cout << CONFINAL;
				cout << HALT;
				return 0;
				break;
			}

			break;
		}

		while (tape[position] != 1)			 																	//or while the TM is readin 0s...
		{
			if (position > 0)																					//and position is not 0
			{
				READ;
				cout << "\n\t=> COMMAND: replace place " << position << " with 1, move to place " << position - 1 << " and move to S2.";
				tape[position] = 1;																				//overwrite  the 0 with a 1
				position--;																						//move left
				stateTwo(tape, position);																		//and move to state 2.
				break;
			}
			else if (position == 0 && tape[position] == 1)														//else, if the very first number is a 1
			{	
				while (tape[position] != 0 && position <= 99)													//start to repeat again final halt condition (=read 5x 1 in a row)
				{
					READ;
					cout << "\n\t=> COMMAND: move to place " << position + 1 << " and stay in S1.";
					position++;																					//move right
					conFive++;																					//increment final halt condition

					if (position == 99 || position > 99)														//but when the tape ends
					{	
						PLACE;
						cout << RIGHTEND;																		
						cout << HALT;																			//the TM halts.
						break;
						return 0;
					}

					if (conFive == 5)																			//or the final halt condition is true
					{
						cout << CONFINAL;
						cout << HALT;																			//and the TM halts.
						break;
						return 0;
					}

					break;
				}
			}
			else																								//if all conditions above are not true, then we read in position 0 a 0
			{
				PLACE;
				cout << LEFTEND;
				cout << HALT;																					//and the TM halts. (because it can't move right under such conditions)
				break;
				return 0;
			}

		}
		//break;
		return 0;
	}
	return 0;
}

//STATE 2
//
//
int stateTwo(int tape[], int position)
{
	//var
	int conFive = 0;
	int state = 2;

	//functions
	int stateOne(int tape[], int position);

	while ((tape[position] == 1 | tape[position] == 0) && (position >= 0 || position <= 99))		//while reading 1 or 0 and array values are in scope...
	{
		while (tape[position] != 0 && position <= 99)												//while reading 1
		{
			READ;
			cout << "\n\t=> COMMAND: move to place " << position - 1 << " and stay in S2.";			//move left and stay in state 2
			position--;																				
			conFive++;																				//increment final halt condition

			if (position < 0)																		//if the TM reads a number below 0
			{
				//cout << "\nMACHINE IS IN STATE 2! On place " << position << " the machine reads a " << tape[position] << ".";
				PLACE;
				cout << LEFTEND;
				cout << HALT;																		//it halts
				break;
				return 0;
			}

			if (position == 0 && tape[position] == 0)												//if position 0 is a zero (and the TM is in state 2)
			{
				PLACE;
				cout << LEFTEND;
				cout << HALT;																		//it halts
				break;
				return 0;
			}

			if (conFive == 5)																		//if final halt condition is true
			{
				cout << CONFINAL;
				cout << HALT;																		//halt														
				break;
				return 0;
			}
			//break;
		}

		while (tape[position] != 1 && position <= 99)												//while reading 0s
		{
			//cout << "\nMACHINE IS IN STATE 2! On place " << position << " the machine reads a " << tape[position] << ".";
			READ;
			cout << "\n\t=> COMMAND: move to place " << position + 1 << " and move to S1.";			//move right and go to state 1
			position++;
			stateOne(tape, position);																//go to state 1 
			/*
			HERE IS THE PROBLEM! after compiling and executing the code, going to state 1 doesn't behave as it has to.
			it writes only 1 line and the program "stops".
			*/
			break;
		}
		return 0;
	}
	return 0;
}
What is your expected output?
What is your broken output?
i think i fixed it! the break; in line 141 caused the problems.
and i added a new function (besides other adjustments) so i can leave the programm cleanly.


if you want your own turing machine, here is the whole code: (still a little buggy in the prediction)

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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// TuringMachine.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

////////////////////////////////////////////////////TURING MACHINE///////////////////////////////////////////////////////////

//std libraries
#include <iostream>
#include <cstdlib>
#include <ctime>

//using namespace
using namespace std;

/*
//my files
#include "stateOne.cpp"
#include "stateTwo.cpp"


//my headers
#include "definitions.h"
#include "stateOne.h"
#include "stateTwo.h"
*/


//DEFINITIONS
#define READ cout << "\nMACHINE IS IN STATE " << state <<"! On place " << position << " the machine reads " << tape[position] << ".";
#define PLACE cout << "\nPosition = " << position;
#define STEPS ;

//constants
const char HALT[] = "\n\n\t\t\t\tThe Turing Machine halts! Press any key for exit.";
const char LEFTEND[] = "\nThats the very end of the tape. The TM can't move left to place -1!";
const char RIGHTEND[] = "\nThats the very end of the tape. The TM can't move right to place 100!";
const char CONFINAL[] = "\n\n\n\t\t\t\tThe last 5 readings were '1'. AMAZING!!!"; //condition when the machine is finished

//functions
//
//

int main()
{
	//variables
	int tape[100];
	int i = 0, n = 0;
	int START;

	//functions
	int stateOne(int tape[], int position);
	int stateTwo(int tape[], int position);
	int halt(int condition);

	srand(time(NULL));

	cout << "\n\t\t\t\tCreate a random Turing Machine tape with length 100:\n";

	for(n = 0; n <= 99; n++) //fill array with random integers from 0 to 1.
	{
		i = rand() % 2;
		tape[n] = i;
		cout << tape[n];
	}

	for (n = 0; n <= 2; n++) //3 trys to find a nice spot to start.
	{
		cout << "\n\nOn what tape number should the TM start? (0-99)\n";
		cin >> START;

		while (START > 99 | START < 0)
		{
			cout << "\nThe entered number is either to big or to small. Please enter a number between 0 and 99.\n";
			cin >> START;
		}
		
		cout << "\nPositon " << START << " is a " << tape[START];

		if (START - 5 <= 94)
		{
			cout <<	". The next 5 positions are: " << tape[START + 1] << " "
				<< tape[START + 2] << " " << tape[START + 3] << " "
				<< tape[START + 4] << " " << tape[START + 5] << " ";
		} 

	}

	cout << "\nMachine starts in S1.";

	stateOne(tape, START); //here we go!!! FUN! FuN! fUn!

	return 0;
}

//STATE 1
//
//
int stateOne(int tape[], int position)
{
	//vars
	int conFive = 0;
	int state = 1;

	//functions
	int stateTwo(int tape[], int position);
	int halt(int condition);

	while ((tape[position] == 1 | tape[position] == 0) && (position >= 0 || position <= 99)) //while the machine is reading 1s or 0s in S1 and array values are in scope...
	{

		//Dieser Code macht deutlich warum sich zweiwertige Variablen nicht selbst durch Ausschliessung bedingen. Obwohl der Ausdruck nur die Werte 1 und 0 bearbeitet, sagt
		//die Funktion <while (tape[position] == 1) ...>,  obwohl logisch äquivalent zu <while (tape[position] != 0) ...>, nicht das Gleiche aus!
		//Im ersten Fall läuft die Schleife nur 1x, weil die Bedingung immer wahr. Im Zweiten aber solange sie ungleich 0 ist. Schleifen sollten nicht POSITIVE Bedingungen erwarten!

		/*while (tape[position] == 1)
		{
		cout << "\nMACHINE IS IN STATE 1! On place " << position << " the machine reads a " << tape[position] << ".";
		position++;
		cout << "\nCOMMAND => move to place " << position << " and stay in S1.";
		}*/
				
		while (tape[position] != 0 && position <= 99) //while the machine is reading 1s in S1
		{		
			//cout << "\nMACHINE IS IN STATE 1! On place " << position << " the machine reads a " << tape[position] << ".";
			READ;
			cout << "\n\t=> COMMAND: move to place " << position + 1 << " and stay in state " << state;
			position++;	//move one position to the right
			conFive++;	//increment final halt condition

			if (position >= 99)	//halt at the right end
			{	
				PLACE;
				halt(position);
				return 0;
				break;
			}

			if (conFive == 5) //halt when machine read 5x '1'
			{
				PLACE;
				halt(conFive);
				return 0;
				break;
			}

		}

		while (tape[position] != 1)	//while reading 0s in S1
		{
			if (position > 0) //and position is not 0
			{
				READ;
				cout << "\n\t=> COMMAND: replace place " << position << " with 1, move to place " << position - 1 << " and move to state " << state + 1;
				tape[position] = 1;																				//overwrite 0 with 1
				position--;																						//move left
				stateTwo(tape, position);																		//and move to state 2.
				break;
			}
			else if (position == 0 && tape[position] == 1) //else, if the machine is reading 1s
			{	
				while (tape[position] != 0 && position <= 99) //while reading 1s in S1
				{
					READ;
					cout << "\n\t=> COMMAND: move to place " << position + 1 << " and stay in state " << state;
					position++;	//move right and stay in S1
					conFive++;	//increment final halt condition

					if (position >= 99)	//halt at the right end
					{	
						PLACE;
						halt(position);	
						break;
						return 0;
					}

					if (conFive == 5) //halt when machine read 5x '1'
					{
						PLACE;
						halt(conFive); 
						break;
						return 0;
					}

					break;
					return 0;
				}
			}
			else //halt at the left end
			{
				PLACE;
				halt(position);	
				break;
				return 0;
			}

		}
		//break;
		return 0;
	}
	return 0;
}

//STATE 2
//
//
int stateTwo(int tape[], int position)
{
	//vars
	int conFive = 0;
	int state = 2;

	//functions
	int stateOne(int tape[], int position);
	int halt(int condition);

	while ((tape[position] == 1 | tape[position] == 0) && (position >= 0 || position <= 99)) //while reading 1 or 0 in S2 and array values are in scope...
	{
		while (tape[position] != 0 && position <= 99) //while reading 1s in S2
		{
			READ;
			cout << "\n\t=> COMMAND: move to place " << position - 1 << " and stay in state " << state;	
			position--;	//move one position to the left, stay in state 2 and																		
			conFive++; //increment final halt condition

			if (position < 0) //halt at left end
			{
				//cout << "\nMACHINE IS IN STATE 2! On place " << position << " the machine reads a " << tape[position] << ".";
				PLACE;
				halt(position);
				break;
				return 0;
			}

			if (position == 0 && tape[position] == 0) //halt at left end
			{
				PLACE;
				halt(position);
				break;
				return 0;
			}

			if (conFive == 5) //halt when machine read 5x '1'
			{
				PLACE;
				halt(conFive);
				break;
				return 0;
			}
			//break;
		}

		while (tape[position] != 1 && position <= 99) //while reading 0s in S2
		{
			//cout << "\nMACHINE IS IN STATE 2! On place " << position << " the machine reads a " << tape[position] << ".";
			READ; 
			cout << "\n\t=> COMMAND: move to place " << position + 1 << " and move to state " << state - 1;  
			position++; //move one position to the right and...
			stateOne(tape, position); //go to state 1 
			break;
		}
		return 0;
	}
	return 0;
}

int halt(int condition)
{
	//vars
	char exit;

	if (condition == 5) //halt when machine read 5x '1'
	{
		cout << CONFINAL;
		cout << HALT;																															
	}

	if (condition <= 0) //halt at left end
	{
		cout << LEFTEND;
		cout << HALT;																		
	}

	if (condition >= 99) //halt at right end
	{
		cout << RIGHTEND;
		cout << HALT;
	}

	cin >> exit;
	return 0;
}

Last edited on
Topic archived. No new replies allowed.