Latin Square algorithm problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sejoro
    New Member
    • Feb 2008
    • 9

    Latin Square algorithm problem

    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;

    }
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    Originally posted by Sejoro
    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;

    }
    .......i think yiu need to launch rand from seed.

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      He called srand(), no issues there. My usual solution to this sort of thing is to use a while loop: while(randomly chosen value is already there): choose another random value. It's not particularly quick, but it usually works OK.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        I wrote the following little monster once (pre-ANSI/C) and it even doesn't need
        an NxN square matrix to produce a magic square of size N (N must be odd and
        supplied on the command line). Have fun.

        [code=c]
        main(o,O0)char* *O0;{int OO,O;O=--o?atoi(O0[!0]):!
        0;for(o=((OO=O* O)-O+!0+!0)>>!0;OO ;o+=((--OO%O)?-!
        0:((((o-!0)%O)?O:0)+!0) )-(((o-!0)%O)?O:0)){pr intf
        ("\n%*d "+!!(OO%O),!0<< !0<<!0,o+=(o<!0 )?O*O:0);}}
        [/code]

        kind regards,

        Jos

        Comment

        Working...