Thread: Latin Square Generator. Code won't keep looping.

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    13

    Latin Square Generator. Code won't keep looping.

    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;
    }
    
    }

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    1. Learn how to indent your code.
    2. You are overrunning the bounds of your array. In C, arrays begin with 0 and got to size-1
    3. Define Main-FAQ()
    4. Never call main();
    5. Don't use goto.
    6. Learn how to Write functions
    7. Global Variables are Bad
    8. You never seed your random number generator and your logic for producing random numbers is flawed. I think your checking dups function is exiting out.

    Make those adjustmens and then repost with updated problems. Try to be a little more specific as to what is going on.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code to find th square root of a number
    By CODY21 in forum C++ Programming
    Replies: 34
    Last Post: 10-29-2010, 09:27 AM
  2. Looping code with a viable ios::noreplace code
    By Medic87 in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2010, 06:43 AM
  3. Looping a code
    By marchyde07 in forum C Programming
    Replies: 7
    Last Post: 08-22-2010, 11:25 AM
  4. Latin square
    By Ron in forum C Programming
    Replies: 6
    Last Post: 05-22-2006, 06:24 PM
  5. Code for random number generator???
    By mero24 in forum C++ Programming
    Replies: 5
    Last Post: 05-02-2005, 03:15 AM