Hi all,
The code below I've compiled with GCC using cygwin.
when running the program you can input the size (N) of the latin square to find. IE: N*N
I can generate n=2 (2 by 2) squares and n=3 (3 by 3) squares.
But nothing higher, why? Well only because the code seems to want to terminate after however many loops. I was hoping it would be obvious as to why, though I can't see why this wouldn't just keep looping forever.
Maybe someone with fresh eyes might.
Code:
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
int count,size,horz,vert,checking_dups;
int skip_input=0;
int grid[100][100];
Display_Grid()
{
for(count=0; count<=40; count++)
{
printf("\n");
}
printf("---------------Displaying grid---------------\n");
for(vert=1; vert<=size; vert++)
{
for(horz=1; horz<=size; horz++)
{
printf("%d", grid[horz][vert]);
printf(" ");
}
printf("\n");
}
printf("\n---------------------------------------------\n");
}
Gen_Numbers()
{
for(vert=1; vert<=size; vert++)
{
for(horz=1; horz<=size; horz++)
{
grid[horz][vert] = (rand() % (size - 1 + 1) + 1);
}
}
}
Check_dups_on_rows()
{
for(vert=1; vert<=size; vert++)
{
for(horz=1; horz<=size; horz++)
{
for(checking_dups=1; checking_dups<size; checking_dups++)
{
if(grid[horz][vert]!=grid[horz+checking_dups][vert])
{}
else//duplicates found
{
skip_input=1; //skips grid size input goes straight to gen numbers again
main();
}
}
}
}
}
Check_dups_on_columns()
{
for(horz=1; horz<=size; horz++)
{
for(vert=1; vert<=size; vert++)
{
for(checking_dups=1; checking_dups<size; checking_dups++)
{
if(grid[horz][vert]!=grid[horz][vert+checking_dups])
{}
else//duplicates found
{
skip_input=1; //skips grid size input goes straight to gen numbers again
main();
}
}
}
}
}
main()
{
if(skip_input==1)
{
start:
Gen_Numbers();
Check_dups_on_rows(); //checks for duplicates in rows
Check_dups_on_columns(); //checks for duplicates in columns
Display_Grid();
goto start;
}
else
{
/*rand*/
/*x = (rand() % (max - min + 1) + min);*/
printf("enter grid size (99 max) N^x: x=:");
scanf("%d",&size);
goto start;
}
}