A program in C written by ChatGPT and me

I asked ChatGPT to write a program to simulate a Turing machine with a transition table given by me. The Turing machine is example no. 4 from Marxen and Buntrock’s paper on 5-state Turing machines, which is available from: https://turbotm.de/~heiner/BB/simmbP_4.html .

ChatGPT produced some good code in a second or so. But it took me an hour to get it working because of misunderstandings and what seem like ChatGPT mistakes in understanding.

The code, as edited by me, is reproduced below ( I don’t know how best to post code in a blog) :

#include <stdio.h >

#define TAPE_LENGTH 8000

int main() {
int tape[TAPE_LENGTH] ;
int tape_head = TAPE_LENGTH / 2; // initialize tape head in the middle of the tape
int state = 1; // start in state 1
int oldstate;

for (int i = 0; i < TAPE_LENGTH; i++) { tape[i] = 0; }

int transition_function[6][2][3] = {
    {{1, -1, 0}, {1, 1, 0}},
    {{1, -1, 2}, {1, 1, 2}},
    {{1, 1, 3}, {0, -1, 5}},
    {{0, 1, 4}, {0, -1, 1}},
    {{1, -1, 1}, {0, 1, 4}},
    {{1, -1, 0}, {0, -1, 3}}
}; // transition function

for (int i = 0; i < 1000000; i++) {
    int symbol = tape[tape_head]; // read symbol from tape

    // apply transition based on current state and symbol
    oldstate=state;
    state = transition_function[state][symbol][2];
    tape[tape_head] = transition_function[oldstate][symbol][0];
    tape_head += transition_function[oldstate][symbol][1];

    // check for out-of-bounds tape head position
    if (tape_head < 0 || tape_head >= TAPE_LENGTH) {
        printf("Error: tape head out of bounds\n");
        return 1;
    }
}

// print final tape contents
for (int i = 0; i < TAPE_LENGTH; i++) {
    printf("%d ", tape[i]);
}
printf("\n");

return 0;

}

Published
Categorized as History
meditationatae's avatar

By meditationatae

Canadian

Discover more from meditationatae

Subscribe now to keep reading and get access to the full archive.

Continue reading