Hey there! Hope you guys can help me out..

Here is my assignment:

1 2 3
2 3 1
3 1 2

Program Requirements: The program will prompt the user for the order of Latin Square
that the user desires (again, the above is order 3, which means 3x3; an order of 4 would
mean 4x4) and then generate a Latin Square of the requested order. The maximum order that
may be requested is 10.

The program will output the result in tabular form as above (without the boxes).

Extra Credit: For extra credit, once the Latin Square has been displayed, you may prompt the user to ask if they would like a different Latin Square of the same order, if they answer yes, then generate a new Latin Square of the same order. Hint: Randomizing your initial number choices makes this much easier.

------------------------------------------------------------------------------------

So basically, Sudoku puzzles are latin squares of 9x9. My goal is to generate completed ones, and not a solver, which was my original thought. I think this is easier...??

I've given it a go.. And it is very messy so I hope you'll bear with me.. haha.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
bool numOK(int latinsq[][10], int row, int col, int size, int num);

int main()
{
    int testnum = (rand() + time(0)) % 100; // <-- YUCK
    int size;
    int latinsq[10][10];

    cout << "What order would you like your Latin Square to be?\n"
    << "(e.g. if you desire a 3x3 grid, just type: 3):  ";
    cin >> size;

    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
         /* Inside here I might change how I find a random number.. as the way I've done it above is pretty crappy.  Do loop?*/
            while (!numOK(latinsq,i,j,size,testnum));
            latinsq[i][j] = testnum;

        }
    }
}

bool numOK(int latinsq[][10], int row, int col, int size, int num)
{
    for (int i = 0; i < size; i++){
        if (latinsq[row][i] == num)
        {
            return false;
        }
    }
    for (int i = 0; i < size; i++){
        if (latinsq[i][col] == num)
        {
            return false;
        }
    }
    return true;
}

Right now it doesn't even compile, and I'm just stuck. If someone could point me in the right direction, it would help, or just give me some pointers? Thanks for everything in advance. =/

You should probably use srand(time(0)); at the top (http://www.cplusplus.com/reference/clibrary/cstdlib/rand/)and do your subsequent calls to rand() % dimension (not rand %100 as a 3 by 3 is not going to need numbers up to 99). As it is your program is trapped on one value and it keeps looping ad nauseum.

Alright.. thanks for your help.

So, after seeing the rand to 100, i've changed it to use numbers up to the dimension input by the user, with the size integer. Still unsure why it's stuck.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
bool numOK(int latinsq[][10], int row, int col, int size, int num);

int main()
{
    int testnum;
    int size;
    int latinsq[10][10];

    cout << "What order would you like your Latin Square to be?\n"
    << "(e.g. if you desire a 3x3 grid, just type: 3):  ";
    cin >> size;

    testnum = (rand() + time(0)) % size; //Changed
    srand(time(0)); 

    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
         /* Inside here I might change how I find a random number.. as the way I've done it above is pretty crappy.  Do loop?*/
            while (!numOK(latinsq,i,j,size,testnum));
            latinsq[i][j] = testnum;

        }
    }
}

bool numOK(int latinsq[][10], int row, int col, int size, int num)
{
    for (int i = 0; i < size; i++){
        if (latinsq[row][i] == num)
        {
            return false;
        }
    }
    for (int i = 0; i < size; i++){
        if (latinsq[i][col] == num)
        {
            return false;
        }
    }
    return true;
}

??

srand(time(0)) goes before rand() and then you don't need the time(0) portion to generate your number. Test it out, but I think it should be rand()%size +1 .

You still have only 1 test number per run of the program, you should probably be generating a new random number within your for loop. The real problem is that once your function is returning false for the first value put in, your while loop goes forever. Inside your for loop you should probably have an if statement - if it doesn't fit in the puzzle, throw it out and continue the loop else put it in the puzzle.

srand(time(0)) goes before rand() and then you don't need the time(0) portion to generate your number. Test it out, but I think it should be rand()%size +1 .

You still have only 1 test number per run of the program, you should probably be generating a new random number within your for loop. The real problem is that once your function is returning false for the first value put in, your while loop goes forever. Inside your for loop you should probably have an if statement - if it doesn't fit in the puzzle, throw it out and continue the loop else put it in the puzzle.

Ah.. right. Makes sense. Thanks for all your help so far, I truly appreciate it. I'm getting lost within all the stupid int's. Sorry if I'm a pain, I'm really new at this.

Ok so -

for (int i = 0; i < size; i++)
           {
                    for (int j = 0; j < size; j++)
                    {
            srand(time(0)); // How about putting the random number in here?
            testnum = rand() % size +1; // better?

            while (!numOK(latinsq,i,j,size,testnum));
            latinsq[i][j] = testnum;

            if (testnum != latinsq[i][j])
            {
                srand(time(0));
                testnum = rand() % size +1;
            }
        }
    }
}

Unsure where to start the if statement, it'll have to be after the while loop, right? Sounds easier than it is I think.. Is the if statement ok if it just generates another random number? The Else i thought would be pointless because the while loop already put it in the puzzle? Lol I am so confused sorry, it's late.

testnum = rand() % size + 1;
               if(!numOK(latinsq,i,j,size,testnum))
				continue;
		else
				latinsq[i][j] = testnum;

for the inner for loop. Since it's done this way you'd have to rework your outer loop(s) to prevent the program for getting trapped. You could make the outer loop a while and check the program in each of the unoccupied positions. That's changing a lot but I haven't gotten it to a point where it converges.

This article has been dead for over six months. Start a new discussion instead.