简体   繁体   中英

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[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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM