473,624 Members | 2,469 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 5561
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
3136
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 a pseudorandom number generator. So, rather than calculate the square root to try to get a particular precision answer, you would calculate it to x number of bits. This particular function takes a radicand and the number of iterations as...
32
4978
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 Series? Is there any other way of doing it rather than using series? Thank you, Priyam
4
8465
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 simple task but my mind suddenly got stuck. I am trying to implement a square root function as a practice. I am able to code for the perfect square
1
1697
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 an error, because \x80 is not a valid character in latin-1 (according to what I can find). Is this the expected behavior, or am I missing something?
1
3908
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 ... where each cell in the square is a solid structure to the height specified by the value in that cell. Then conceptually I pour water on top of the square and see where the water collects ... in lakes as it were. **** there are 880 different...
10
13626
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 i go to print out the answer its rounding instead of giving me the answer i need... any help?? double num1, num2, the_root; cout<< "Enter number to have square root taken of it\n"; cin>> num1; cout<< "Enter number for how...
4
8084
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 involving lot of matrices. At one point I need to calculate the square root of a matrix e.g. A which contains non-zero off-diagonal elements. I searched for a lot of info on net but no algorithm worked. My best bet for finding square root was to find...
1
2131
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 nonlinear least square problem. Let me tell you some background first. Imagine you have a tool to process some work piece, say polishing some piece of glas. The tool behaves different on different locations of the piece,
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: www.faust.fr.bw.schule.de/mhb/backtrack/mag6en.htm I've found some information on the siamese method and is this related to the backtracking algorithm?
0
8240
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8625
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8336
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7168
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5565
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2610
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 we have to send another system
2
1487
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.