Hey again,
I'm trying to write a program that will output a random valid Latin Square (9x9 square of numbers 1 - 9 with no repetition in the rows and columns) and can't get my numbers to not repeat. I figured that the code I have now would be enough, but it doesn't seem to be working. Could somebody help me out?
#include <iostream>
#include <ctime>
using namespace std;
int main(){
srand(time(0));
int grid[9][9];
int test = 0;
int check = 0;
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++){
grid[i][j] = rand() % 9 + 1;
}
}
for(int loop = 0; loop < 9; loop++){
for(int run = 0; run < 9; run++){
test = grid[run][run];
for(int repeat = 0; repeat < 9; repeat++){
check = grid[repeat][repeat];
if(test == check){
grid[repeat][repeat] = rand() % 9 + 1;
repeat = -1;
}
}
}
}
//Printing the Latin Square
int rows = 1;
for(int print = 0; print < 9; print++){
for(int dim = 0; dim < 9; dim++){
cout << grid[print][dim] << " ";
if(rows < 9){
rows++;
}
else{
cout << endl;
rows = 1;
}
}
}
system("PAUSE");
return 0;
}