lundi 16 janvier 2017

My C program is exiting with a non-zero status and I don't know why. Currently running the code in repl.it

Hi I'm currently running a code in c programming, and it keeps exiting with a non-zero status.

The code is being used for a sudoku game and what I'm essentially trying to do is generate a random 9x9 2D array. After randomly generating it, I have a function checking if the grid is a valid grid. I don't know why I'm getting a non-zero status.There might be something wrong with the method of how I'm randomly generating the numeber but I'm not sure. Help is appreciated thank you.

#include "stdio.h"
#include <time.h>
#include <stdlib.h>


int check(int list[][9]){
    // Disable stdout buffering
    setvbuf(stdout, NULL, _IONBF, 0);

    int i, j, x, y, ans = 0, sum = 0, sum2 = 0, sum3 = 0;

    for(i=0;i<9;i++){               //This nested for loop checks if columns and rows are correct
      sum = 0;
      sum2 = 0;
      for(j=0;j<9;j++){
        sum += list[i][j];
        sum2 += list[j][i];
        if((j == 8) && ((sum != 45) || (sum2 != 45)))
         ans = 1;
      }
    }

    for(x=0;x<3;x++){         //This nested for loop checks if boxes are right
      for(y=0;y<3;y++){
        sum3=0;
        for(i=0;i<3;i++){
          for(j=0;j<3;j++){
            sum3+= list[i+(3*y)][j+(3*x)];
            if((i==2 && j==2) && (sum3 != 45))
              ans = 1;
          }
        }
      }
    }

    return ans;
}




int main(void){
  // Disable stdout buffering
  setvbuf(stdout, NULL, _IONBF, 0);

  int list[9][9] = {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};


  int i, j, r;

  for(;;i++){

    for(i=0; i<9; i++){
       for(j=0; j<9; j++){
        srand(time(NULL));
        r = rand() % ((9 + 1 - 1) + 1);
        list[i][j] = r;
      }
    }

    if(check(list[i][j]) == 0)
      break;
  }

  printf("done");

  return 0;
}




Aucun commentaire:

Enregistrer un commentaire