There is a segmentation fault but I can't see any problem in infinite loops, problems with memory access etc

62 views Asked by At

I wanted to make a program that will generate sudoku 9x9 with brute force and a fixed array of numbers form 1-9. There seems to be segmentation error when shuffling the array randomly and I can't seem to find where it actually is.

The full code is here and the problem is in the inputRandoms function:

#include <time.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int count = 0;
int matrix[9][9] = { 0 };
void inputRow(int matrix[9][9], int suffeledArr[], int* row);
void inputRandoms(int arr[]);
void newValueInput(int value[]);
void SinglePrintMatrix(int matrix[9][9]);

void main() {
  int values[9] = { 1,2,3,4,5,6,7,8,9 };
  srand(time(0));
  inputRandoms(values);
  for (int i = 0; i <= 7; i++) {
    if (count == 0) {
      inputRow(matrix, values, &count);
    }
    else {
      newValueInput(values);
    }
  }
  SinglePrintMatrix(matrix);
  getch();
}

// inputs the row in the matrix
void inputRow(int matrix[9][9], int suffeledArr[], int* count) {
  for (int i = 0; i < 9; i++) {
    matrix[*count][i] = suffeledArr[i];
  }
  (*count)++;
}

//generates new inputs and cheaks for uniqueness in colunm, if not unique then rerolls with      //inputRandoms() function
void newValueInput(int value[]) {
  inputRandoms(value);
  int temp[9] = { 0 };
  int i, k = count;

  for (int j = 0; j < 9; j++) {
    for (i = 0; i <= count; i++) {
      //puts the value of one colunm in temp[i]
      temp[i] = matrix[i][j];
    }
    //     //puts the latest value in temp without removing one of the data
    temp[count + 1] = value[j];
    // if(count!=k&&count!=8){
    //     temp[i+1]=value[i];
    // }
    for (k = count + 1; k >= 0; k--) {
      //cheaks for ununique value
      if (temp[k] == temp[8 - k]) {
        if (((k) / (8 - k)) == 1) {//is uniwue
          inputRow(matrix, value, &count);
          break;
        }
        else {//is not unique
          newValueInput(value);
          break;
        }
      }
    }
  }
}
//suffels the value[] array to generate unique combanition of values
void inputRandoms(int arr[]) {
  int j = 0, temp = 0, i = 0;
  for (i = 8; i > 0; i--) {
    while (j < 0 && j >= 10) {
      j = rand() % (i + 1); //segmentation error
    }
    temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
  }
}



// prints out the matrix
void SinglePrintMatrix(int matrix[9][9]) {
  printf("Matrix:\n");
  for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
      printf("%d ", matrix[i][j]);
    }
    printf("\n");
  }
  printf("\n");
}

I have wasked ChatGPT and some other AI what to do and tried to debug but I don't know what I was doing when I was using GDB.

1

There are 1 answers

0
Notiq On

You need to change inputRandoms to fix the loop and make proper shuffling. Also try to make sure that j is within bounds. Another reason could be that you're not looking for unique columns correctly. Here's some improved code:

void inputRandoms(int arr[]) {
    for (int i = 8; i > 0; i--) {
        int j;
        do {
            j = rand() % (i + 1);
        } while (j < 0 || j >= 10);
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

void newValueInput(int value[]) {
    inputRandoms(value);
    int temp[9];
    for (int j = 0; j < 9; j++) {
        for (int i = 0; i <= count; i++) {
            temp[i] = matrix[i][j];
        }
        temp[count] = value[j];
        int unique = 1;
        for (int k = 0; k < count; k++) {
            if (temp[k] == temp[count]) {
                unique = 0;
                break;
            }
        }
        if (unique) {
            inputRow(matrix, value, &count);
            break;
        }
    }
}