Thread: Latin Square program

  1. #16
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Read the directions carefully. Good. Now do it again. Now do it 10 more times. Make sure you understand what the assignment is asking. Your program is not supposed to generate the Latin square. It is supposed to check if the matrix the user provided is a Latin square.

  2. #17
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    in the example on top your thread,
    its
    1 2 3
    3 1 2
    2 3 1

    so after the end of the horizontal top number its down to below right?
    another example i assume:
    1 2 3 4
    4 1 2 3
    3 1 2 4
    4 3 1 2

    if so the logic i can think its,

    Build an two dimensional array like anduril462 say.
    Then do double loop (inner and outer), and the incremented loop will fill the multidimensional array range
    If its failed then using conditional statement inside (inner or outer) loop.


    hm i didnt test your code, im just see it and analyze it. So if i saying wrong dont do it

  3. #18
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by anduril462 View Post
    Read the directions carefully. Good. Now do it again. Now do it 10 more times. Make sure you understand what the assignment is asking. Your program is not supposed to generate the Latin square. It is supposed to check if the matrix the user provided is a Latin square.
    Well I first had to understand how the Latin Square is made. Hmm just got back from a night class so it's time to try again.

  4. #19
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by loserone+_+ View Post
    in the example on top your thread,
    its
    1 2 3
    3 1 2
    2 3 1

    so after the end of the horizontal top number its down to below right?
    another example i assume:
    1 2 3 4
    4 1 2 3
    3 1 2 4
    4 3 1 2

    if so the logic i can think its,

    Build an two dimensional array like anduril462 say.
    Then do double loop (inner and outer), and the incremented loop will fill the multidimensional array range
    If its failed then using conditional statement inside (inner or outer) loop.


    hm i didnt test your code, im just see it and analyze it. So if i saying wrong dont do it
    Well the program isn't supposed to do the Latin Square, it is supposed to check if the user inputted matrix is a latin square.

  5. #20
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    so you're in a right way to victory then.

  6. #21
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by loserone+_+ View Post
    so you're in a right way to victory then.
    Haha thank you! I have 4 more hours to finish this before I have to turn it in.

  7. #22
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    @loserone+_+
    He's (she's?) not (yet) on the right track. They're getting closer though.

    @Cdd101
    It might help you to realize that, for a given n, there may be more than one valid Latin square of that size. For example:
    Code:
    1 2 3 4
    4 1 2 3
    3 4 1 2
    2 3 4 1
    
    and
    
    2 3 4 1
    3 4 1 2
    4 1 2 3
    1 2 3 4
    If you look here: Latin square - Wikipedia, the free encyclopedia, you will see that the number of possible Latin squares grows very rapidly, as n grows.

  8. #23
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Ahh, just saw the 4 hour comment. So, I'll give you a little freebie:

    You need to count occurrences. How many 1's, 2's, etc are in a given row/column?

  9. #24
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    what if the matrix given a bad top horizontal number to check,
    example

    2 6 8 4
    4 2 6 8
    8 4 2 6
    6 8 4 2

    this looks confusing, sure i cant think the logic right now (nb: im very sleepy right now)

  10. #25
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by loserone+_+ View Post
    what if the matrix given a bad top horizontal number to check,
    example

    2 6 8 4
    4 2 6 8
    8 4 2 6
    6 8 4 2

    this looks confusing, sure i cant think the logic right now (nb: im very sleepy right now)
    In general, the symbols don't have to be 1..n. They could be any numbers, letters, punctuation or any other symbol/glyph/thing.

    Perhaps for this assignment, it is intended for the user to only give symbols 1..n. Cdd101 would have to tell us.

  11. #26
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    and when i see the link anduril give,
    i realize something funny in latin square (cool though)

    condition 1:
    1 2 3 4
    4 1 2 3
    3 4 1 2
    2 3 4 1

    condition 2:
    2 3 4 1
    3 4 1 2
    4 1 2 3
    1 2 3 4

    condition 3:
    1 2 3 4
    2 1 4 3
    3 4 1 2
    4 3 2 1

    and many more, with more n though,
    i think you will going to use more than two condition statement,

  12. #27
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by anduril462 View Post
    In general, the symbols don't have to be 1..n. They could be any numbers, letters, punctuation or any other symbol/glyph/thing.

    Perhaps for this assignment, it is intended for the user to only give symbols 1..n. Cdd101 would have to tell us.
    Well I'm assuming that the user is allowed to enter any number and it just has to be a latin square.

    Sorry for the late responses, I'm completely stump at the moment.

    Created an integer called int counter=will be used to count the reoccurences as you stated

    now I'm making a

    Code:
    for (i=0; i <n; i++){
    for (j=0; j <n;j++){
         if...
    Am I on the right track?

  13. #28
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well, if the user can enter anything, perhaps you should consider using a single char as the symbol instead of an int. That will allow 'A', 'b', '!', etc. Also, it will allow another trick for counting character occurrences.

    First, you need an array of counts, so you can count occurrences of all symbols at once. The simple version (assuming values of 1 to n) would be something like:
    Code:
    unsigned char matrix[n][n];
    int symbol_count[UCHAR_MAX + 1];
    ...
    for i from 0 to n-1
        initialize symbol_count to all 0's
        for j from 0 to n-1
            if matrix[i][j] is symbol 0
                symbol_count[0]++
    UCHAR_MAX is probably 255 since an unsigned char typically ranges from 0 to 255. You want to declare UCHAR_MAX+1 so you have 256 elements in the array, having indexes 0 to 255.
    Now, there's a better way than a giant mess of if statements checking each symbol type. You can nest array-like statements
    Code:
    symbol_count[matrix[i][j]]++;
    Basically, that looks up the symbol at matrix[i][j], uses it to index the symbol_count array, and increments that slot in the symbol_count array.

  14. #29
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by anduril462 View Post
    Well, if the user can enter anything, perhaps you should consider using a single char as the symbol instead of an int. That will allow 'A', 'b', '!', etc. Also, it will allow another trick for counting character occurrences.

    First, you need an array of counts, so you can count occurrences of all symbols at once. The simple version (assuming values of 1 to n) would be something like:
    Code:
    unsigned char matrix[n][n];
    int symbol_count[UCHAR_MAX + 1];
    ...
    for i from 0 to n-1
        initialize symbol_count to all 0's
        for j from 0 to n-1
            if matrix[i][j] is symbol 0
                symbol_count[0]++
    UCHAR_MAX is probably 255 since an unsigned char typically ranges from 0 to 255. You want to declare UCHAR_MAX+1 so you have 256 elements in the array, having indexes 0 to 255.
    Now, there's a better way than a giant mess of if statements checking each symbol type. You can nest array-like statements
    Code:
    symbol_count[matrix[i][j]]++;
    Basically, that looks up the symbol at matrix[i][j], uses it to index the symbol_count array, and increments that slot in the symbol_count array.
    I get this error
    Code:
    E:\Desktop\Stilltrying.c||In function 'main':|
    E:\Desktop\Stilltrying.c|11|error: 'UCHAR_MAX' undeclared (first use in this function)|
    E:\Desktop\Stilltrying.c|11|note: each undeclared identifier is reported only once for each function it appears in|
    ||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|
    Code:
    #include <stdio.h>#include <limits.h>
    
    
    int main()
    {
    
    
        int i, j, n;
    
    
        printf("Enter the number of entries:");
        scanf("%d", &n);
        unsigned char matrix[n][n];
        int symbol_count[UCHAR_MAX+1];
    
    
        for (i=0; i<n; i++){
            for (j=0; j<n; j++){
                printf( "Enter Element[%d][%d]: ", i, j);
                scanf("%d",&matrix[i][j]);
            }
        }
        for (i=0;i<n;i++){
            for(j=0;j<n;j++)
            {
            printf("%d ", matrix[i][j]);
            }
            printf("\n");
        }
        for (i=0; i<n;i++){
            symbol_count==0;
            for(j=0; j<n;j++){
                if (matrix[i][j]==symbol_count[0])
                symbol_count[0]++;
                }
                }
    
    
        return(0);
    }
    Last edited by Cdd101; 10-08-2013 at 08:56 PM.

  15. #30
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    fixed the uchar error with limits.h

    Code:
    for (i=0; i<n;i++){        
    symbol_count==0;
            for(j=0; j<n;j++){
                if (matrix[i][j]==symbol_count[0])
                symbol_count[0]++;
                }
                }
        if (symbol_count > 1)
        {
            printf("The Latin Square is: ");
            for(i = 0; i < symbol_count; i++)
                printf("%c ", matrix[i]);
            printf("\n");
        }
        else
            printf("There isn't a correct Latin Square\n");
    Last edited by Cdd101; 10-08-2013 at 09:09 PM.

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