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

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[9][9]){
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[9][9], int i, int num){
for (int j=0;j<9;j++){
if (sudoku[i][j]==num){
return 1;
}
}
return 0;
}
int colExists(int sudoku[9][9], int j, int num) {
for (int i=0;i<9;i++) {
if (sudoku[i][j]==num){
return 1;
}
}
return 0;
}
int valExists(int sudoku[9][9], 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[9][9], 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[9][9], 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[9][9]={{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[][9],
bool solve_sudoku(int sudoku[][9], 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.

Related

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

#include<stdio.h>
#include<stdlib.h>
int visited[100];
int A[8][8] = {
{0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 1, 0, 0, 0, 0, 1},
{0, 1, 0, 0, 1, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 1},
{0, 0, 0, 1, 0, 0, 0, 1},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0}
};
void DFS(int i){
printf("%d ", i);
int back = 0;
visited[i] = 1;
for (int j = 0; j < 8; j++) {
if (visited[j]) {
if (back == 1 ) {
printf("\nrevisited: %d\n", j);
}
back = 1;
}
if(A[i][j]==1 && !visited[j]){
DFS(j);
}
}
}
void DFSLinker() {
for (int k = 0; k < 100; k++) {
visited[k] = 0;
}
for (int k = 0; k < 8; k++) {
if (visited[k] == 0) {
DFS(k);
}
}
}
int main(){
DFSLinker();
return 0;
}
I have an adjacency matrix of an directed graph and I am trying to print out any one cycle in this directed graph.
So far, this code prints out the DFS traversal as well as any of the nodes that I have already visited while doing the DFS traversal.
I know that the cycles in this particular directed graph are as follows:
1 2
4 7
4 5 7
When I try to get the very last revisited nodes in order to print out a cycle, I get 7 5 7, which is not a valid cycle.
What am I missing?

Checking for numbers within arrays in C

The function below is looking through Order1-3 and looks if any of the elements are within each of the 3 column values of each of the 8 rows inside Winning_order. So in this case Order1 values are within the first row of Winning_order so it comes out as true as well as Order2. However Order2 is not a valid output for the program, how can I modify the iterating function below so that it checks to be true. Order3 is meant to be a false as well since its not within Winning_order. The code has been gotten from the answer of this issue issue.
#include <stdio.h>
// Iterating function
int match_arrays(int *arr1, int *arr2, int len)
{
for (int i = 0; i < len; i++) {
if (arr1[i] != arr2[i]) {
return 0;
}
}
return 1;
}
// Main function
int main(void)
{
int Order1[3] = {1,5,9};
int Order2[4] = {1,2,5,3};
int Order3[3] = {4,4,4};
int Winning_order[8][3] = {{1,2,3}, {4,5,6}, {7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}};
for (int i = 0; i < 5; i++) {
if (match_arrays(Order1, Winning_order[i], 3)) {
printf("Order1");
}
if (match_arrays(Order2, Winning_order[i], 3)) {
printf("Order2");
}
if (match_arrays(Order3, Winning_order[i], 3)) {
printf("Order3");
}
}
return 0;
}
Expected Output
Order 1 Order 2
This matching method will output
Order2Order1
Could you explain some detail why Order2 will be true?
#include <stdio.h>
int match_arrays(int* arr_order, int* arr_win, int len_order, int len_win)
{
int idx = 0;
int err = 0;
for (int i = 0; i < len_order; i++) {
if (arr_order[i] != arr_win[idx]) {
err++;
if (err > len_order - len_win) {
return 0;
}
} else {
idx++;
if (idx == len_win)
{
return 1;
}
}
}
return 1;
}
int main()
{
int Order1[3] = {1, 5, 9};
int Order2[4] = {1, 2, 5, 3};
int Order3[3] = {4, 4, 4};
int Winning_order[8][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {1, 4, 7}, {2, 5, 8}, {3, 6, 9}, {1, 5, 9}, {3, 5, 7}};
for (int i = 0; i < 8; i++) {
if (match_arrays(Order1, Winning_order[i], 3, 3)) {
printf("Order1");
}
if (match_arrays(Order2, Winning_order[i], 4, 3)) {
printf("Order2");
}
if (match_arrays(Order3, Winning_order[i], 3, 3)) {
printf("Order3");
}
}
return 0;
}

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[10][10] = { 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[10][10] = { 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[10][10] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* ..., */ { 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
char walk[10][10] = { { 0 }, { 0 } };
char walk[10][10] = { { 0 } };
char walk[10][10] = { 0 }; // initial statement wrongly rejected by compiler
Even better (IMHO) would be to configure your compiler to accept legal code.
godbolt.org accepts your initial 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[10][10] = { { 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[10][10] = {
{ 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[10][10] = { "", "", "", "", "", "", "", "", "", "" };
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[10][10];
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]);

num cannot be resolved to a variable in java

I am creating program to sort the total number of hours employees work in a week in descending order. I am only having one compilation error when I try to execute my code. It says that "num" cannot be resolved to a variable...
The error occurs at following line:
sortHours(num); //Error occurs here
Where am I going wrong?
public class WorkerHours {
public static void main(String[] args) {
int[][] hours = {
{2, 4, 3, 4, 5, 8, 8},
{7, 3, 4, 3, 3, 4, 4},
{3, 3, 4, 3, 3, 2, 2},
{9, 3, 4, 7, 3, 4, 1},
{3, 5, 4, 3, 6, 3, 8},
{3, 4, 4, 6, 3, 4, 4},
{3, 7, 4, 8, 3, 8, 4},
{6, 3, 5, 9, 2, 7, 9}};
int[] weeklyHours = totalHours(hours);
sortHours(num); //Error occurs here
displayDescSort(weeklyHours);
}
public static int[] totalHours(int[][] hours){
int[] result1 = new int[8];
for (int i = 0; i < hours.length; i++){
int sum = 0;
for (int j = 0; j < hours[i].length; j++){
sum += hours[i][j];
}
result1[i] = sum;
}
return result1;
}
public static void sortHours(int[] num){
for (int i = 0; i < num.length - 1; i++){
int currentMax = num[i];
int currentMaxIndex = i;
for (int j = i + 1; j < num.length; j++){
if (currentMax < num[j]){
currentMax = num[j];
currentMaxIndex = j;
}
}
if (currentMaxIndex != i){
num[currentMaxIndex] = num[i];
num[i] = currentMax;
}
}
}
public static void displayDescSort(int[] weeklyHours){
for (int i = weeklyHours.length-1; i >= 0; i--){
System.out.println("Employee" + i + ": " + weeklyHours[i] + " hours");
}
}
}

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[4][5] = {{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");
}
}

Resources