9 Replies - 453 Views - Last Post: 06 March 2013 - 01:57 PM Rate Topic: -----

#1 birddog270  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-March 13

Help with 3X3 Latin Square

Posted 03 March 2013 - 08:04 PM

I am having trouble with my code printing out the latin squares. We are supposed to create a program that will print out all the possible Latin squares for a 3X3 2-D array. I think my code works but when I try it, it only prints out the blank strOut. Any help would be appreciated. Thanks

   public partial class Form1 : Form
    {
        const int   ciRows = 3, ciCols = 3;
        int[,]      aiLatin;

        public Form1()
        {
            InitializeComponent();
            aiLatin = new int[ciRows, ciCols];
        }       

        private string PrintSquare(ref int[,] aiLatin)
        {
            string  strOut = "";
            int     i, j;

            for (i = 0; i < ciRows; i++)
            {
                for (j = 0; j < ciCols; j++)
                {
                    strOut += aiLatin[i, j] + "\t";
                }
                strOut += "\r\n";
            }
            return strOut;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            bool    bRow1 = true, bRow2 = true, bRow3 = true;
            int     i, j;
            string  strOut = "blank";

            for (i = 0; i < ciRows; i++)
                for (j = 0; j < ciCols; j++)
                    aiLatin[i, j] = 1;

                bRow1 = true;
                bRow2 = true;
                bRow3 = true;

                while (bRow1)
                {
                    bRow1 = UpdateRow(aiLatin, 0);
                    while (bRow2)
                    {
                        bRow2 = UpdateRow(aiLatin, 1);
                        while (bRow3)
                        {
                            bRow3 = UpdateRow(aiLatin, 2);
                            if (ValidSquare(aiLatin))
                                strOut += PrintSquare(ref aiLatin);
                        }
                        bRow3 = true;
                    }
                    bRow2 = true;
                }
            txtOutput.Text = strOut;

        }

        //returns a true if can update again. otherwise false
        //updates to the next possible row
        private bool UpdateRow(int[,] aiLatin, int iRow)
        {
            bool bUpdateRow = true;
            /*
            if (bUpdateRow)
            {
                if (aiLatin[iRow, 2] < 3)
                    aiLatin[iRow, 2]++;

                else
                {
                    aiLatin[iRow, 2] = 1;
                    if (aiLatin[iRow, 1] < 3)
                        aiLatin[iRow, 1]++;

                    else
                    {
                        aiLatin[iRow, 1] = 1;
                        if (aiLatin[iRow, 0] < 3)
                            aiLatin[iRow, 0]++;

                        else
                            aiLatin[iRow, 0] = 1;

                    }
                }
            }

            else
                bUpdateRow = false;

            return bUpdateRow;*/

            int j;

            for (j = 2; j > -1; j--)
            {
                if (aiLatin[iRow, j] == 3)
                {
                    aiLatin[iRow, j] = 1;
                    if (j == 0)
                    {
                        return bUpdateRow = false;
                    }
                }
                else
                {
                    aiLatin[iRow, j] += 1;
                     return bUpdateRow = true;
                }
            }
            return bUpdateRow;           
        }

        //returns true if valid latin square
        private bool ValidSquare(int[,] aiLatin)
        {
            //check each row to see if 2 "spots" are same
            int i, j, iTemp;

            for (i = 1; i < 3; i++)
            {
                for (j = 1; j < 3; j++)
                {
                    iTemp = aiLatin[i, 0];

                    if (iTemp == aiLatin[i, j])
                        return false;

                    else if ( j == 1 )
                        {
				            if ( aiLatin[ i, j ] == aiLatin[ i, 1 ] )
					            return false;

				            else if (aiLatin[ i, j ] == aiLatin[ i, 2 ] )
					            return false;

                        }
			        else if ( j == 2 )
				        if ( aiLatin[ i, j ] == aiLatin[ i, 2 ] )
					        return false;
		        }
	        }             
            return true;
        }
        
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Help with 3X3 Latin Square

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3574
  • View blog
  • Posts: 11,114
  • Joined: 05-May 12

Re: Help with 3X3 Latin Square

Posted 03 March 2013 - 09:21 PM

If you set a break point on line 12, is your function even being called?
Was This Post Helpful? 0
  • +
  • -

#3 birddog270  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-March 13

Re: Help with 3X3 Latin Square

Posted 03 March 2013 - 10:27 PM

View PostSkydiver, on 03 March 2013 - 09:21 PM, said:

If you set a break point on line 12, is your function even being called?



Yes it is down on the btnGo code. When the button is pressed, the code is called. I just wrote what I knew how to do the best first, so that is why all of the functions are out of order.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3574
  • View blog
  • Posts: 11,114
  • Joined: 05-May 12

Re: Help with 3X3 Latin Square

Posted 03 March 2013 - 10:40 PM

So if it goes into the function, I don't see how you could end up with an empty string considering that your function will not generate an empty string. If you set a breakpoint on line 58, do you have strOut as an empty string?
Was This Post Helpful? 0
  • +
  • -

#5 birddog270  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-March 13

Re: Help with 3X3 Latin Square

Posted 03 March 2013 - 10:53 PM

View PostSkydiver, on 03 March 2013 - 10:40 PM, said:

So if it goes into the function, I don't see how you could end up with an empty string considering that your function will not generate an empty string. If you set a breakpoint on line 58, do you have strOut as an empty string?



I have done some more trial and error (and debugging), and I have figured out that the ValidSquare() function is the problem. The code to see if the Latin Square is valid is too "strict". It is not validating any of the Latin Squares so in return, the btnGo button/function is returning the empty string.

Any suggestions for fixing the ValidSquare function?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3574
  • View blog
  • Posts: 11,114
  • Joined: 05-May 12

Re: Help with 3X3 Latin Square

Posted 03 March 2013 - 11:07 PM

Wait a minute. You said that your PrintSquare was being called in post #3, and now you are saying that ValidSquare is too strict and not validating anything. If that is the case, given the code you posted, how could your PrintSquare have been called?
051                            if (ValidSquare(aiLatin))
052                                strOut += PrintSquare(ref aiLatin);



Anyway, take a step back and look at the code you wrote for lines 133-144. What happens when i == 1, and j == 1? What happens when i == 1 and j == 2?

This post has been edited by Skydiver: 03 March 2013 - 11:08 PM

Was This Post Helpful? 0
  • +
  • -

#7 birddog270  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-March 13

Re: Help with 3X3 Latin Square

Posted 06 March 2013 - 10:27 AM

View PostSkydiver, on 03 March 2013 - 11:07 PM, said:

Wait a minute. You said that your PrintSquare was being called in post #3, and now you are saying that ValidSquare is too strict and not validating anything. If that is the case, given the code you posted, how could your PrintSquare have been called?
051                            if (ValidSquare(aiLatin))
052                                strOut += PrintSquare(ref aiLatin);



Anyway, take a step back and look at the code you wrote for lines 133-144. What happens when i == 1, and j == 1? What happens when i == 1 and j == 2?



I figured it out. Thanks for the help though! Here is my updated version of the code.

namespace Homework_9___2D_array
{
    public partial class Form1 : Form
    {
        const int   ciRows = 3, ciCols = 3;
        int[,]      aiLatin;

        public Form1()
        {
            InitializeComponent();
            aiLatin = new int[ciRows, ciCols];
        }       

        private string PrintSquare(ref int[,] aiLatin)
        {
            string  strOut = "";
            int     i, j;

            for (i = 0; i < ciRows; i++)
            {
                for (j = 0; j < ciCols; j++)
                {
                    strOut += aiLatin[i, j] + "\t";
                }
                strOut += "\r\n";
            }
            strOut += "\r\n";
            return strOut;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            bool    bRow1 = true, bRow2 = true, bRow3 = true;
            int     i, j, iCount = 0;
            string  strOut = "";

            for (i = 0; i < ciRows; i++)
                for (j = 0; j < ciCols; j++)
                    aiLatin[i, j] = 1;

                bRow1 = true;
                bRow2 = true;
                bRow3 = true;

                while (bRow1)
                {
                    bRow1 = UpdateRow(aiLatin, 0);
                    while (bRow2)
                    {
                        bRow2 = UpdateRow(aiLatin, 1);
                        while (bRow3)
                        {
                            bRow3 = UpdateRow(aiLatin, 2);
                            if (ValidSquare(aiLatin))
                            {
                                strOut += PrintSquare(ref aiLatin);
                                iCount++;
                            }
                        }
                        bRow3 = true;
                    }
                    bRow2 = true;
                }
            txtOutput.Text = strOut + "There are " + iCount + " Latin Squares for a 3X3 matrix.";
        }

        //returns a true if can update again. otherwise false
        //updates to the next possible row
        private bool UpdateRow(int[,] aiLatin, int iRow)
        {
            bool    bUpdateRow = true;
            int     j;

            for (j = 2; j > -1; j--)
            {
                if (aiLatin[iRow, j] == 3)
                {
                    aiLatin[iRow, j] = 1;
                    if (j == 0)                    
                        return bUpdateRow = false;                    
                }
                else
                {
                    aiLatin[iRow, j] += 1;
                    return bUpdateRow = true;
                }
            }
            return bUpdateRow;          
        }

        //returns true if valid latin square
        private bool ValidSquare(int[,] aiLatin)
        {
            int i;

                for (i = 0; i < 3; i++)
                    {
                    //Check to see if any of the values in one spot are equal to the next spot for rows and cols.    
                    if ((aiLatin[i, 0] != aiLatin[i, 1]) && (aiLatin[i, 1] != aiLatin[i, 2]) && (aiLatin[i, 2] != aiLatin[i, 0]) && (aiLatin[0, i] != aiLatin[1, i]) && (aiLatin[1, i] != aiLatin[2, i]) && (aiLatin[2, i] != aiLatin[0, i]))
                        {
                        //another type of check to see if the sum of the 3 spots is equal to 6
                        //don't have to check for three 2's since did that in previous if statement.
                        if (((aiLatin[i, 0] + aiLatin[i, 1] + aiLatin[i, 2] == 6)) && ((aiLatin[0, i] + aiLatin[1, i] + aiLatin[2, i] == 6)))
                            {
                                if (i == 2)
                                    return true;
                            }
                            else
                                return false;
                        }
                        else
                            return false;
                    }
                return false;
        }        
    }
}


Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: Help with 3X3 Latin Square

Posted 06 March 2013 - 10:47 AM

Quote

I think my code works but when I try it, it only prints out the blank strOut.


Best sentence ever.
Was This Post Helpful? 0
  • +
  • -

#9 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Help with 3X3 Latin Square

Posted 06 March 2013 - 10:52 AM

You know how many times I hear that type of statement on this board? I like the one "So my program is complete but I have this problem, it won't do X"

Doesn't sound complete to me. I also get "My program works but when I print the answer it is wrong"

I sometimes get the "My program is done. I just need to write this function." which said function is half of the program.

Gotta love n00bs. :)
Was This Post Helpful? 1
  • +
  • -

#10 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Help with 3X3 Latin Square

Posted 06 March 2013 - 01:57 PM

I tell my boss all the time that I've done the major stuff, it's just the little details that need to be finished :)
class Project {
    public void Main() {
        GetAllInput();
        DoAllProcessing();
        OutputAllData();
   }

   public static void GetAllInput() {
       // details go here
   }

   public static void DoAllProcessing() {
       // details go here
   }

   public static void OutputAllData() {
       // details go here
   }
}


Fortunately, he knows I'm joking :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1