Thread: Latin Square program

  1. #31
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Sorry I forgot the limits.h thing. My point (perhaps I wasn't clear) was that you could do
    Code:
    if (matrix[i][j]==symbol_count[0])
                symbol_count[0]++;
    But that would get tedious to handle every single symbol (255 if/else statements). Instead, remove the if/else stuff and just put the following in the body of the loop
    Code:
    symbol_count[matrix[i][j]]++;
    Note, you will need multiple sets of loops to check, and you will need to reset all the counts to zero between each row you check, and between each column you check.

    If you've learned how to write your own functions, consider doing so. Some useful functions might be
    Code:
    bool reset_all_counts(int counts[], int num);  // resets all num elements of counts[] to 0
    bool check_row(int n, int matrix[n][n])  // it's important to put n first, and put it in the [] for matrix, since it's a VLA
    // similar for check_col

  2. #32
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by anduril462 View Post
    Sorry I forgot the limits.h thing. My point (perhaps I wasn't clear) was that you could do
    Code:
    if (matrix[i][j]==symbol_count[0])
                symbol_count[0]++;
    But that would get tedious to handle every single symbol (255 if/else statements). Instead, remove the if/else stuff and just put the following in the body of the loop
    Code:
    symbol_count[matrix[i][j]]++;
    Note, you will need multiple sets of loops to check, and you will need to reset all the counts to zero between each row you check, and between each column you check.

    If you've learned how to write your own functions, consider doing so. Some useful functions might be
    Code:
    bool reset_all_counts(int counts[], int num);  // resets all num elements of counts[] to 0
    bool check_row(int n, int matrix[n][n])  // it's important to put n first, and put it in the [] for matrix, since it's a VLA
    // similar for check_col
    We still haven't gone over functions. What I did find out though today, was my teacher just downloads some university powerpoint, and just slaps our University name on the ppt, a credit and that is all he does.

    To say he has taught us anything would be lying. It's an online course so probably why.

  3. #33
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Okay, well, no functions then, but the general idea is still the same. Note, if you've ever done sudoku puzzles, and verified your answer yourself, this is remarkably similar (in fact it's a subset of the checks you do for a sudoku checker). Maybe that helps you envision how to solve this. Unfortunately, I have to go. Hopefully somebody will take up the cause.

  4. #34
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by anduril462 View Post
    Okay, well, no functions then, but the general idea is still the same. Note, if you've ever done sudoku puzzles, and verified your answer yourself, this is remarkably similar (in fact it's a subset of the checks you do for a sudoku checker). Maybe that helps you envision how to solve this. Unfortunately, I have to go. Hopefully somebody will take up the cause.
    Thank you so much for your time pal.

  5. #35
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Thank you for the help anduril and others. I have finished the assignment. You guys are the greatest and I'm really sorry for the constant questions.

  6. #36
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Cdd101 View Post
    Thank you for the help anduril and others. I have finished the assignment. You guys are the greatest and I'm really sorry for the constant questions.
    You're welcome, and don't be sorry. This is how people learn. We are happy to help (we volunteer our time -- if we didn't like it, we wouldn't be here). This is especially true when helping forum members who are polite, who follow the forum rules, and who make serious efforts to learn and work through their problems.

  7. #37
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Hey guys, sorry to bump this so I turned in the assignment and got an 70. I realized what I did was wrong so was wondering if I could obtain some help on knowing how to do this. He wants a function to be used but I'm not to sure on how to make functions. Just trying to learn from my mistake. Sorry to be a bother, it's okay if you guys don't reply since it has already been graded just wanted to get a better grasp on how to do this assignment.

  8. #38
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    I also learned that using i, j, n as my variable names is a stupid thing to do. And i'm going to send a message to the instructor about having to use his variable names. By using more meaningful names, I'm able to remember which int does what.

  9. #39
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Functions in C - Cprogramming.com

    What he was probably looking for, in step #4 of the assignment, was a funciton like:
    Code:
    int verify_latin_square(int n, char matrix[n][n])
    {
        // checks that matrix is a valid latin square
        // returns true/non-zero if it is, otherwise returns false/0
    }

  10. #40
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by anduril462 View Post
    Functions in C - Cprogramming.com

    What he was probably looking for, in step #4 of the assignment, was a funciton like:
    Code:
    int verify_latin_square(int n, char matrix[n][n])
    {
        // checks that matrix is a valid latin square
        // returns true/non-zero if it is, otherwise returns false/0
    }
    Sorry to be a bother for you pal. I should probably give up with this since it's already graded but..If i don't learn now im going to struggle for the rest of the class with functions.

    A function should it be called outside of the main? If so is it better on top of the main or the bottom or it doesn't matter. (Nooby question)

    I'm in no hurry to get help on this or don't mind if i don't get help since it's already graded. Just trying to learn. Just telling you this in case you have something better to do then help me, pal.
    Last edited by Cdd101; 10-11-2013 at 03:49 PM.

  11. #41
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Nvm as long as the prototype is named on top it doesn't matter.

  12. #42
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    What I was trying to do here was try making the function say if the rows are the same. It's not a latin Square.I'm aware that the first n=row, and that the second n=column.

    Code:
    int verify_latin_square(int n, char matrix[n][n])
    {
    
        /*What I was trying to do here was try making the function say if the rows are the same. It's not a latin Square.
        I'm aware that the first n=row, and that the second n=column.*/ 
             for(n=0; n<matrix; n++) {
                for(matrix=0; matrix<n; matrix++) {
                    if(matrix[n][n] ==matrix[n][n]) {
                        printf("This is not a latin square");
        }
        return (matrix[n][n]); /*Wasn't to sure on how to do a return value*/
    }
            }
    }
    Sorry doing pseudo-code first and then trying to turn it into actual code. I guess it's easier for me to think on how to do this then on how to insert it onto here.

  13. #43
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Read, read, read all you can about functions. Do the practice problems you find in your text book, online, etc. The simplest ones to start (i.e. ignore this function for your program until you have a good understanding of them in general).

    If you're doing pseudo code, don't try so hard to make it look like real code, make it more like a hybrid C/your native language.

    Also, you have the right syntax for returning a value, but matrix[n][n] is out of bounds (remember, matrix is a n-by-n matrix, so valid indexes are 0 to n-1). What you should be doing is returning either 1 (true -- matrix is a latin square) or 0 (false -- matrix is not a latin square). So something like:
    Code:
    for each row
        for each column
            if ...
                return false
    Note, I don't print anything in this function. One of the most important function rules is: a function should do one thing and do it well. If you are writing a function to verify a matrix, don't have it print anything. In main you will do something like:
    Code:
    if verify_latin_square(n, matrix)
        print "is a latin square"
    else
        print "not a latin square"
    Your pseudo code for how to check for a latin square is off though. Note, in that function, n was meant to be the size of the matrix (which you will pass in from main), and matrix is the matrix you are checking. Don't change them. You would want something like:
    Code:
    int verify_latin_square(int n, char matrix[n][n])
    {
        int row, col;  // for iterating through rows/columns
    I'll leave you to think about how to check/verify a latin square some more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Latin Square Generator. Code won't keep looping.
    By st00ch in forum C Programming
    Replies: 1
    Last Post: 09-28-2011, 09:47 AM
  2. Square root program
    By thisisdarshan in forum C Programming
    Replies: 5
    Last Post: 10-06-2009, 12:52 AM
  3. magic square program
    By Zarakava in forum C Programming
    Replies: 10
    Last Post: 01-27-2009, 07:06 PM
  4. Latin square
    By Ron in forum C Programming
    Replies: 6
    Last Post: 05-22-2006, 06:24 PM
  5. Help with magic square program
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-15-2002, 05:57 AM