473,499 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Latin Square algorithm problem

9 New Member
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;

}
Mar 29 '08 #1
3 5552
whodgson
542 Contributor
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.
Mar 29 '08 #2
Laharl
849 Recognized Expert Contributor
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.
Mar 29 '08 #3
JosAH
11,448 Recognized Expert MVP
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.

Expand|Select|Wrap|Line Numbers
  1. main(o,O0)char**O0;{int OO,O;O=--o?atoi(O0[!0]):!
  2. 0;for(o=((OO=O*O)-O+!0+!0)>>!0;OO;o+=((--OO%O)?-!
  3. 0:((((o-!0)%O)?O:0)+!0))-(((o-!0)%O)?O:0)){printf
  4. ("\n%*d "+!!(OO%O),!0<<!0<<!0,o+=(o<!0)?O*O:0);}}
  5.  
kind regards,

Jos
Mar 29 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3117
by: Clint Olsen | last post by:
Hello: I posted a thread on comp.programming awhile back asking about an algorithm I implemented on square root. The idea was to use the square root of a prime number as a convenient way to get...
32
4941
by: priyam.trivedi | last post by:
Hi! Could anyone tell me how to find the square root of a number without using the sqrt function. I did it by using Newton's Formula. How can it be done by using the Binomial Theorem/Taylor...
4
8434
by: sathyashrayan | last post by:
(This is not a home work question) Dear group, I want a program to find one number between a set of natural number.A program to guess a number in between a Natural number set.This should be a...
1
1687
by: Chris Curvey | last post by:
Hey all, I'm trying to write something that will "fail fast" if one of my users gives me non-latin-1 characters. So I tried this: u'\x80' I would have thought that that should have raised...
1
3893
by: magic man | last post by:
I am 50 years old ...and am working physical models of the math structure called a magic square .. for my own interest. My present problem is this. I have a topograhical model for the square...
10
13616
by: socondc22 | last post by:
my program is trying to use the babylonian algorithm in order to find the square root... i have a number for the number to have the square root taken of and also a number to run the loop... Whenever...
4
8066
by: krishnai888 | last post by:
I had already asked this question long back but no one has replied to me..I hope someone replies to me because its very important for me as I am doing my internship. I am currently writing a code...
1
2117
by: Uwe Kotyczka | last post by:
Hallo, sorry for multiposting, but I am really looking for some hint to solve my problem. And no, I don't use Matlab, but maybe the matlab people have an idea nevertheless. I have to solve a...
2
968
by: j_depp_99 | last post by:
I would like to know if the 6x6 magic square can be done using the backtracking algorithm and if so how long would it take. I found a link online that does this pretty quick though: ...
0
7169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4597
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.