## Print out cycle of directed graph from adjacency matrix using DFS in C - c

### How do I use goto with swtich in C? (FSM)

```Let's say I have this code
switch(x)
{
region_1:
case a:
...
case b:
goto region_1:
region_2:
case c:
...
case d:
goto region_2
default:
...
}
Is it possible in C to have such a recursion-like algorithm? Where from any case I can jump to a label and go though cases again? I need to build a FSM for my grand dad, he' planning to catch fish with it.
```
```Why use a switch at all.
For example, say you want to write an automata that checks if the input matches pattern ab+c+.
Automata:
S0:
a → S1
S1
b → S1
c → S2
S2
c → S2
EOF → ACCEPT
This could be done with switches, but it could also be implemented using goto:
S0: {
int c = getch();
if (c == 'a') goto S1;
goto ERROR;
}
S1: {
int c = getch();
if (c == 'b') goto S1;
if (c == 'c') goto S2;
goto ERROR;
}
S3: {
int c = getch();
if (c == 'c') goto S2;
if (c == EOF) goto ACCEPT;
goto ERROR;
}
ACCEPT: exit(0);
ERROR: exit(1);
But you could also write that as follows:
while (1) {
int next_state;
switch (state) {
case ERROR: exit(1);
case ACCEPT: exit(0);
case S0: {
int c = getch();
if (c == 'a') { next_state = S1; }
else { next_state = ERROR; }
break;
}
case S1: {
int c = getch();
if (c == 'b') { next_state = S1; }
else if (c == 'c') { next_state = S2; }
else { next_state = ERROR; }
break;
}
case S2: {
int c = getch();
if (c == 'c') { next_state = S2; }
else { next_state = ERROR; }
break;
}
}
state = next_state;
}
But that's really a stepping stone to using tables.
#define TOK_OTHER 0
#define TOK_EOF 1
#define TOK_A 2
#define TOK_B 3
#define TOK_C 4
static int char_to_token_map[] = {
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0x00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x30 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x40 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x60 */ 0, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 0x70 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
static int to_token(int c) {
if (c >= 128) return 0;
if (c == EOF) return 1;
return char_to_token_map[c];
}
#define ERROR -2
#define ACCEPT -1
#define S0 0
#define S1 1
#define S2 2
static int automata {
/* state OTHER EOF a b c */
/* ----- ------- ------- ------- ------- ------- */
/* S0 */ { ERROR ERROR S1, ERROR, ERROR },
/* S1 */ { ERROR, ERROR, ERROR, S1, S2, },
/* S2 */ { ERROR, ACCEPT, ERROR, ERROR, S2, }
};
int state = S0;
while (1) {
int new_state;
switch (state) {
case ERROR: exit(1);
case ACCEPT: exit(0);
default: new_state = automata[ state ][ to_token(getch()) ];
}
state = new_state;
}
If you need to do anything before switching state, just put a special case in the switch for that state. Or use a table of function pointers indexed by state.
```
```You could do something like the following
switch ( x )
{
case 1:
L1: switch( y )
{
case a:
//...
case b:
//...
goto L1;
}
case 2:
L2: switch( y )
{
case c:
//...
case d:
//...
goro L2;
}
default:
//...
}```

### C - How to fix recursive function return type error

```I'm not sure why my solve_sudoku function results in this error:
error: void value not ignored as it ought to be
My full code is below. Please note I have to keep void as return type.
Any help is appreciated.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void print_sudoku(int sudoku){
printf("The Sudoku contains:\n");
for (int j=0; j<9; j++)
{
for (int i=0; i<9;i++)
{
printf("%d ",sudoku[j][i]);
}
printf("\n");
}
}
int rowExists(int sudoku, int i, int num){
for (int j=0;j<9;j++){
if (sudoku[i][j]==num){
return 1;
}
}
return 0;
}
int colExists(int sudoku, int j, int num) {
for (int i=0;i<9;i++) {
if (sudoku[i][j]==num){
return 1;
}
}
return 0;
}
int valExists(int sudoku, int i, int j, int num) {
for (int r=0;r<3;r++){
for (int s=0;s<3;s++){
if (sudoku[r+i][s+j]==num){
return 1;
}
}
}
return 0;
}
int DNE(int sudoku, int *i, int *j) {
for (*i=0; *i<9; (*i)++){
for (*j=0;*j<9;(*j)++){
if (sudoku[*i][*j]==0){
return 1;
}
}
}
return 0;
}
void solve_sudoku(int sudoku, int depth){
int i=0;
int j=0;
if (!DNE(sudoku, &i, &j)){
return;
}
for (int k=1;k<=9;k++){
if (!rowExists(sudoku, i, k) && !colExists(sudoku, j, k) && !valExists(sudoku, i-(i%3), j-(j%3), k)){
sudoku[i][j]=k;
if (solve_sudoku(sudoku, depth)){
return;
}
sudoku[i][j]=0;
}
}
return;
}
#ifndef __testing
int main(){
int Sudoku={{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
printf("Input puzzle is:\n");
print_sudoku(Sudoku);
solve_sudoku(Sudoku, 0);
printf("Solution is:\n");
print_sudoku(Sudoku);
}
#endif
```
```The following line inside solve_sudoku function expectes that solve_sudoku function will return a boolean value but return type of the function is void, thus creating the error.
if (solve_sudoku(sudoku, depth))
You can change signature of the function like below and return true/false as per condition.
Tips: when passing a multi dimesional array you don't need to specify the first dimension. Ex: int sudoku[],
bool solve_sudoku(int sudoku[], int depth)
```
```Because your solve_sudoku returns void, so the condition check if (solve_sudoku(sudoku, depth)) shows that error - we can't check a "void" is true or false.
You should let it return int.```

### C: Array initialization requires a brace-enclosed initializer list - simple code

```I am a complete beginner in C, and I have ran into what I am assuming to be a simple error. I have looked up similar issues online but I cannot find an issue with my code. There is very little and I do not know what the issue is.
Here is the error:
C: Array initialization requires a brace-enclosed initializer list
and this is my complete code
#include <stdio.h>
int main() {
char walk = { 0 };
for (int row = 0; row < 10; row++) {
for (int col = 0; col < 10; col++) {
walk[row][col] = '.';
printf("%c", walk[row][col]);
}
}
getchar();
return 0;
}
```
```When using char walk = { 0 }; I get the compiler error "C: Array initialization requires a brace-enclosed initializer list".
That is your compiler being terribly anal-retentive.
The statement in question is perfectly legal C. It defines a 10x10 2-D array of char named walk where every element (of 100) is 0.
To comply with your compiler whims, use one of
char walk = { { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* ..., */ { 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
char walk = { { 0 }, { 0 } };
char walk = { { 0 } };
char walk = { 0 }; // initial statement wrongly rejected by compiler
Even better (IMHO) would be to configure your compiler to accept legal code.
```
```You compile the code with extra warnings enabled, which is a very good idea.
The compiler insists that you initialize an array of arrays with an initializer with the same structure. In your case you can try it char walk = { { 0 } };.
You might have an even more restrictive setting where the compiler indicates that not enough initializers are present. The complete initializer would be:
char walk = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};
Or a more compact version:
char walk = { "", "", "", "", "", "", "", "", "", "" };
But looking at your code, walk does not need an initializer at all since you set all entries right below the declaration:
#include <stdio.h>
int main() {
char walk;
for (int row = 0; row < 10; row++) {
for (int col = 0; col < 10; col++) {
walk[row][col] = '.';
printf("%c", walk[row][col]);
}
}
getchar();
return 0;
}
PS: as pmg says, your code is legal and would compile as is with the default permissive settings, but using the compiler warnings to avoid silly mistakes trumps the extra constraints. Code that compiles cleanly with high warning levels usually has fewer bugs.
Note also that you can initialize walk with a single call to memset(walk, '.', sizeof(walk)); and you could output single characters more efficiently with putchar(walk[row][col]);```

### Storing Multiple Matrices in a Cell array (Matlab)

```I am currently attempting to generate 30 different matrices and store them in a cell array, but when I run the program I just have a cell array with 30 copies of the same matrix. What am I doing wrong?
m_A = 20;
ID = 3754;
m_B = .002*ID + 3.1;
J_A = 0.0013*ID + 0.188;
g = -9.81;
i = 1;
n = 30/i;
%stores matrices in a cell array
matrixresults = cell(1, n);
for k = 1:n
%generates A matrices
for M = 0:i:30
J_C = .5*M*((.1)^2);
matrixresults{k} = [(m_A), 0, -1, 1, 0, 0, 0;
(5*J_A), 0, -0.2, 0, 0, 0, 0;
(10*J_C), 0, 0, -0.1, 0, .1, 0;
0, M, 0, 0, 1, -1, -1;
0, (10*J_C), 0, 0, 0, -0.1, 0.1;
0, m_B, 0, 0, -1, 0, 0;
0.5, -1, 0, 0, 0, 0, 0];
end
end
```
```Try this:
clc; clear;
m_A = 20;
ID = 3754;
m_B = .002*ID + 3.1;
J_A = 0.0013*ID + 0.188;
g = -9.81;
i = 1;
n = 30/i;
%stores matrices in a cell array
A = cell(1, n);
for k = 1:i:30
J_C = .5*k*((.1)^2);
A{k} = [(m_A), 0, -1, 1, 0, 0, 0;
(5*J_A), 0, -0.2, 0, 0, 0, 0;
(10*J_C), 0, 0, -0.1, 0, .1, 0;
0, k, 0, 0, 1, -1, -1;
0, (10*J_C), 0, 0, 0, -0.1, 0.1;
0, m_B, 0, 0, -1, 0, 0;
0.5, -1, 0, 0, 0, 0, 0];
end```

### Find intersection in 2d arrays

```I wrote a small code to find intersection of two 2d array, unfortunately it is not working, so maybe you can help me.. Intersection is, if both numbers on place (x,y) is a "1". Otherwise there should be "0"
void intersection(int *mat, int rows, int cols) {
int rows1 = 5, cols1 = 4; // secend matrix is in function because i just need it here
int ma2[] = { 0, 0, 1, 0, 1, // 1. Zeile
0, 0, 1, 0, 1, // 2. Zeile
0, 0, 1, 1, 0, // 3. Zeile
0, 0, 1, 0, 0 // 4. Zeile
};
int i = 0;
int j = 0;
int x = 0;
int y = 0;
while (j < cols && y < cols1) { // maybe it is better with a for loop ??
j += 1;
y += 1;
while (i < rows && x < rows1) {
i += 1;
x += 1;
if (mat[j*rows+i] == 1 && ma2[y*rows1+x] == 1) {
printf("%d ", mat[j*rows+i]);
break;
} else {
printf("%d ", mat[j*rows+i]);
break;
}
}
printf("\n");
}
}
int main (void) {
int rows = 5, cols = 4; //first matrix is in main, because i need it for other functions
int ma[] = { 0, 0, 1, 0, 0, // 1. Zeile
1, 0, 0, 0, 0, // 2. Zeile
1, 0, 1, 0, 0, // 3. Zeile
0, 0, 1, 0, 0 // 4. Zeile
};
intersection(ma, rows, cols);
return 0;
}
Output should be (in this case):
{ 0, 0, 1, 0, 0, // 1. Zeile
0, 0, 0, 0, 0, // 2. Zeile
0, 0, 1, 0, 0, // 3. Zeile
0, 0, 1, 0, 0 // 4. Zeile
};
but i just get a matrix with 1 row
Greets ;)
```
```try this
#define min(x,y) ((x) < (y) ? (x) : (y))
void intersection(int *mat, int rows, int cols) {
rows = min(rows, 5);//rows <--> cols
cols = min(cols, 4);
int ma2[] = { 0, 0, 1, 0, 1, // 1. Zeile
0, 0, 1, 0, 1, // 2. Zeile
0, 0, 1, 1, 0, // 3. Zeile
0, 0, 1, 0, 0 // 4. Zeile
};
int i, j;
for(i = 0; i < cols; ++i){
for(j = 0; j < rows; ++j){
//printf("%d ", mat[i*rows + j] == ma2[i*rows + j] ? mat[i*rows + j] : 0);
printf("%d ", mat[i*rows + j] & ma2[i*rows + j]);
}
printf("\n");
}
}
```
```I solved the problem with this solution ;) Thank you everyone for helping me ...
void intersection(int *mat, int rows, int cols) {
int ma2 = {{0, 1, 0, 0, 0},
{0, 1, 0, 0, 0},
{1, 1, 0, 1, 0},
{0, 1, 0, 0, 0}};
int i = 0;
int j = 0;
int t = 1;
int s = 0;
for(j = 0; j < cols; j++) {
for (i = 0; i < rows; i++) {
if (ma2[j][i] && mat[j*rows+i] == 1) {
printf("%d ", t);
} else {
printf("%d ", s);
}
}
printf("\n");
}
}```