Thread: Latin Square program

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    196

    Latin Square program

    Hello guys, sorry for always asking for help.
    These are the instructions:
    Latin Square program-png
    This is where my code is at the moment.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        int i,j,k=1,n;
        int range[n];
        printf("Enter the number for the Latin Square: ");
        scanf("%d",&n);
        printf("Enter the numbers from 1-n: \n");
        for(i=0;i<n;++i){
            printf("Enter: ");
            scanf("%d", &range[n]);
            }
        for (i=1;i<=n;i++){
            printf("\n");
            for (j=1;j<=n;j++)
            {
            printf(" %d",k);
        if (k==n)
        k=1;
        else
            k++;
        }
            k++;
        }
    return(0);
    }
    This is what it outputs
    Latin Square program-abs-png

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    A few problems off the bat:

    • You don't need math.h, you aren't using any functions from there.
    • This requires a square (i.e. 2-d) array/matrix.
    • The user is to fill the entire matrix. Then you print the matrix with the data the user entered. Don't just print the number from 1 to n in whatever pattern you feel like.
    • If you insist on using variable length arrays (VLAs), don't declare the VLA until after you have a valid value for the size of the array. Like I said in your other thread, you must read a value into n before you declare range[n]. Otherwise, you have an array of unknown size, which is bad.


    Read your class notes, textbooks, online tutorials, etc on arrays, particularly 2-d arrays.

    Study the assignment carefully. You are misunderstanding what it is asking. If you don't understand the problem, or can't solve it yourself with paper and pencil, then you can't program a computer to do it. Once you know how to solve it, come up with a rough plan (pseudo code perhaps), then implement that. Work in small steps, testing as you go. Don't move on to the next step until all previous steps are complete and work correctly. Thankfully your teacher already broke the problem down into 5 different steps for you. Work through the steps in the assignment one at a time. First, complete step 1, making your program do something simple like
    Code:
    Enter the size of the Latin square: 5
    You want a Latin square of size 5
    Then, on to step 2 (which you may combine with step 3, since they are very similar)
    Code:
    Enter the size of the Latin square: 2
    You want a Latin square of size 2
    Enter element [0][0]: 1
    Enter element [0][1]: 2
    Enter element [1][0]: 7
    Enter element [1][1]: 8
    
    The matrix you entered is:
    1 2
    7 8
    Then work on steps 4 and 5.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by anduril462 View Post
    A few problems off the bat:

    • You don't need math.h, you aren't using any functions from there.
    • This requires a square (i.e. 2-d) array/matrix.
    • The user is to fill the entire matrix. Then you print the matrix with the data the user entered. Don't just print the number from 1 to n in whatever pattern you feel like.
    • If you insist on using variable length arrays (VLAs), don't declare the VLA until after you have a valid value for the size of the array. Like I said in your other thread, you must read a value into n before you declare range[n]. Otherwise, you have an array of unknown size, which is bad.


    Read your class notes, textbooks, online tutorials, etc on arrays, particularly 2-d arrays.

    Study the assignment carefully. You are misunderstanding what it is asking. If you don't understand the problem, or can't solve it yourself with paper and pencil, then you can't program a computer to do it. Once you know how to solve it, come up with a rough plan (pseudo code perhaps), then implement that. Work in small steps, testing as you go. Don't move on to the next step until all previous steps are complete and work correctly. Thankfully your teacher already broke the problem down into 5 different steps for you. Work through the steps in the assignment one at a time. First, complete step 1, making your program do something simple like
    Code:
    Enter the size of the Latin square: 5
    You want a Latin square of size 5
    Then, on to step 2 (which you may combine with step 3, since they are very similar)
    Code:
    Enter the size of the Latin square: 2
    You want a Latin square of size 2
    Enter element [0][0]: 1
    Enter element [0][1]: 2
    Enter element [1][0]: 7
    Enter element [1][1]: 8
    
    The matrix you entered is:
    1 2
    7 8
    Then work on steps 4 and 5.
    Doing this little by little as you suggested.
    Code:
    #include <stdio.h>
    
    int main()
    {
    
    
         int arr[3][3], i, j, k=1;
         for(i=0;i<3;i++){
            for(j=0;j<3;j++){
                printf("Enter Element[%d][%d]: ", i, j);
                scanf("%d",&arr[i][j]);
    
    
        }
        }
        return(0);
    }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That looks good...but you skipped step 1!!!!!!!!

    Also, you should never use magic numbers (e.g. 3). Always use a constant/#define, even for such a simple example.

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by anduril462 View Post
    That looks good...but you skipped step 1!!!!!!!!

    Also, you should never use magic numbers (e.g. 3). Always use a constant/#define, even for such a simple example.
    So something like this? Did some changes.
    Code:
    #include <stdio.h>
    #define ROW 10
    #define COL 10
    
      int main() {
            int myarray1[ROW][COL], square[COL];
            int i, j, n;
           
            printf("Enter the number of entries:");
            scanf("%d", &n);
    
            for (i = 0; i < n; i++) {
                    printf("Enter the elements[%d][%d]: ", i);
                    scanf("%d", &square[i]);
            }

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Code:
    #include <stdio.h>
    #define ROW 10
    #define COL 10
    
    
    
    
      int main() {
            int myarray1[ROW][COL], square[COL];
            int i, j, n;
            int k=1;
    
    
    
    
            printf("Enter the number of entries:");
            scanf("%d", &n);
    
    
            for (i = 0; i < ROW; i++) {
                    printf("Enter the elements[%d][%d]: ", i);
                    scanf("%d", &square[i]);
            }
    
    
    
    
            for (i = 0; i < ROW; i++) {
                    for (j = 0; j < COL; j++) {
                            myarray1[i][j] = square[j];
                    }
            }
    
    
    
    
            for (i=1; i<=n; i++){
                printf("\n");
                    for (j=1; j<=n; j++)
                {
                printf(" %d",k);
            if (k==n)
                k=1;
            else
                k++;
                }
                k++;
            }
        return(0);
    }
    Latin Square program-asd-png

    Feel like I'm heading to the right direction.

    Spoke to soon.
    Last edited by Cdd101; 10-08-2013 at 04:48 PM. Reason: Spoke to soon.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I told you your first attempt was good, but it was missing something. That doesn't mean change what's there.

    Did you test your most recent code? It should give warnings when you compile, particularly the printf on line 13. You specify two numbers to print (two %d), but only provide 1. The first attempt was better. Since it's a square matrix, you don't need ROW and COL though. In fact, since you are reading the size from the user, just use n when you declare matrix
    Code:
    #include <stdio.h>
     
    int main() {
        int i, j, n;
            
        printf("Enter the number of entries:");
        scanf("%d", &n);
    
        int matrix[n][n];  // declare matrix to be a two dimensional, n-by-n array  DO THIS AFTER YOU READ n
    
        for i from 0 to n-1
            for j from 0 to n-1
                print "Enter Element[%d][%d]", i, j
                read input into matrix[i][j]
            }
        }
    
    
        return(0);
    
    }
    Now work on printing that matrix out

  8. #8
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by anduril462 View Post
    I told you your first attempt was good, but it was missing something. That doesn't mean change what's there.

    Did you test your most recent code? It should give warnings when you compile, particularly the printf on line 13. You specify two numbers to print (two %d), but only provide 1. The first attempt was better. Since it's a square matrix, you don't need ROW and COL though. In fact, since you are reading the size from the user, just use n when you declare matrix
    Code:
    #include <stdio.h>
     
    int main() {
        int i, j, n;
            
        printf("Enter the number of entries:");
        scanf("%d", &n);
    
        int matrix[n][n];  // declare matrix to be a two dimensional, n-by-n array  DO THIS AFTER YOU READ n
    
        for i from 0 to n-1
            for j from 0 to n-1
                print "Enter Element[%d][%d]", i, j
                read input into matrix[i][j]
            }
        }
    
    
        return(0);
    
    }
    Now work on printing that matrix out
    is that n-1 in the psuedo code mean negative one? Or you saying subtract from n?

  9. #9
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Code:
    #include <stdio.h>
    
    int main() {
        int i, j, n;
    
    
        printf("Enter the number of entries:");
        scanf("%d", &n);
    
    
        int matrix[n][n];
    
    
        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");
        }
    
    
        return(0);
    }
    Latin Square program-asdf-png

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Looks good. Try with non-repeating data to be sure: 1 2 3 4 5 6 7 8 9. That way, you make sure your input and print code doesn't do something silly like write to or print from the same row over and over again.

    EDIT: No need to post the result, just check for yourself. If it works, move on. And I think you figured it out, but n-1 means "one less than n" or "subtract 1 from n".

  11. #11
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by anduril462 View Post
    Looks good. Try with non-repeating data to be sure: 1 2 3 4 5 6 7 8 9. That way, you make sure your input and print code doesn't do something silly like write to or print from the same row over and over again.

    EDIT: No need to post the result, just check for yourself. If it works, move on. And I think you figured it out, but n-1 means "one less than n" or "subtract 1 from n".
    It displays the table correctly. So the only area I need to fix is to make n--?

  12. #12
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    So now on the following part I'm assuming I have to use an IF statement.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    More than just one if statement though. Draw out some example matrices on paper, and work through the algorithm by hand. If you can't do it yourself, you can't program a computer to do it. Once you figure that out, work on translating it to code.

  14. #14
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Well I understand how the Latin square works

    Let's say we input 1-6


    1 2 n 3 n-1 4






    The table would be


    1 2 n 3 n-1 4
    2
    3
    4
    5
    6


    We then increment each column by 1 so the table would become


    1 2 n 3 n-1 4
    2 3
    3 4
    4 5
    5 6
    6 1


    If we continue plug in the numbers on top And then begin incrementing by 1


    1 2 6 3 5 4
    2 3 1 4 6 5
    3 4 2 5 1 6
    4 5 3 6 2 1
    5 6 4 1 3 2
    6 1 5 2 4 3

    That would be the answer.

    So each if statement would be incrementing the input by 1. So I guess there'd be a few if statements.

    Now to turn this into c I can't think of a way.

  15. #15
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Would it be something like

    Code:
    if (i>n)
    {
    n++;
    }
    
    Else if (i<n)
    {
    n--;
    }

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