# CS2112: Exercises + Answers from \$13 Control structures

* Try informally proving Bohm & Jacopini's theorem for yourself. You may be able to simplify things by using one extra integer variable rather than many booleans (why are these equivalent?). Try numbering each box in a random flowchart and creating extra code that uses the integer to ensure each box is obeyed in the correct order.

An integer is a list of binary digits, so an integer and a set of booleans are equivalent.

Identify each sequential block of code by a number > 0.
Embed these blocks of code as the cases of a switch inside a loop.
Declare a global variable that indicates the case/block to select and loop while the variable is > 0.
Add code at the end of each case/block to set this variable to the next block number, and use a value of 0 to finish.

e.g. from CS106 - this is already well structured, but it shows you the sort of changes you need to make:

```void mystery (Node *list)
{ Node *upto, *temp, *prev;
upto= list;
while (upto != NULL)
{ temp= list;
while (temp->value != upto->value)
temp= temp->next;
if (temp == upto)
{ prev= upto;
upto= upto->next;
} else
{ prev->next= upto->next;
free (upto);
upto= prev->next;
}
return;
}
```

```void mystery (Node *list)
{ Node *upto, *temp, *prev;
int next= 1;
while (next) switch (next) {
case 1:
upto= list;
next= 2; break;
case 2:
if (upto != NULL) next= 3; else next= 0; break;
case 3:
temp= list;
next= 4; break;
case 4:
if (temp->value != upto->value) next= 5; else next= 6; break;
case 5:
temp= temp->next;
next= 6; break;
case 6:
if (temp == upto) next= 7; else next= 8; break;
case 7:
prev= upto;
upto= upto->next;
next= 2; break;
case 8:
prev->next= upto->next;
free (upto);
upto= prev->next;
next= 2; break;
}
return;
}
```