Like Tree4Likes

short matrix problem

This is a discussion on short matrix problem within the C Programming forums, part of the General Programming Boards category; Think about it. Your multiply routine has 3 loops with 3 index variables (i, d and k) that each go ...

  1. #31
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,326
    Think about it. Your multiply routine has 3 loops with 3 index variables (i, d and k) that each go from 0 to a certain limit (c, m and c) respectively. You fill your product matrix using product[i][d], so you have

    product[0][0]...product[0][m-1]
    ...
    product[c-1][0]...product[c-1][m-1]

    That means that your for loop for printing the product matrix should be
    Code:
    for i from 0 to c
        for d from 0 to m
    The way you currently have it, d stops at k, which (from your multiply loop) remains equal to c. Thus, you are printing c rows and c columns, hence it only working for a square matrix.

    EDIT: And this should be a huge, huge clue to just how awful one-letter variable names are. They are only useful for loop indexes, (x,y) coordinates and very simple math equations. Variable names like matrix1_rows, matrix2_cols, product_rows, product_cols would make this much, much clearer, and keep you from making such mistakes. You have been warned...never post such ridiculous stuff here again, or face utter humiliation .
    Last edited by anduril462; 10-03-2012 at 11:22 PM.
    Salem and Adak like this.

  2. #32
    Registered User
    Join Date
    May 2012
    Posts
    210
    So if I use this loop I end up with the right answer but the dimensions arent right so there are like 2 garbage values in the matrix



    Code:
     for( i = 0 ; i < c ; i++ )
        {
          for( d = 0 ; d < m ; d++ )
    
    
            {
              product[i][d]=0;
               for(k=0;k<m;k++) {
                product[i][d] = product[i][d] + matrix[i][d] * transpose[i][d];
              }
            }
        }
      printf("\nThe Product Of The Two Matrices Is:\n\n");
      for(i=0; i<c; i++) {
        for(d=0; d<k; d++) {
          printf(" %3d ",product[i][d]);
        }
        printf("\n");
      }

  3. #33
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Why do you have 2 threads on the same subject?
    I'm closing the other one, since it's now got the same code in both of them (with the same replies from different people!)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #34
    Registered User
    Join Date
    May 2012
    Posts
    210
    sorry i made the first one before and I didnt think anyone was replying to it

  5. #35
    Registered User
    Join Date
    May 2012
    Posts
    210
    Could you please help me though
    I'm freaking out here pleasee!!

  6. #36
    Registered User
    Join Date
    May 2012
    Posts
    210
    Hey salem I saw what you posted by replacing the k with an m and I've already tried that but it not only gives the wrong dimensions but even the values are wrong. As opposed to what I posted up here which only gives the wrong dimensions but right answer.
    Please help me think of something

  7. #37
    Registered User
    Join Date
    May 2012
    Posts
    210
    Now its not even posting the right answer anymore
    OH CRAP!!

  8. #38
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    How about posting your current code?

    Bye, Andreas

  9. #39
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The only problem I see with your program is the terrible variable names you're using.
    And this should be a huge, huge clue to just how awful one-letter variable names are. They are only useful for loop indexes, (x,y) coordinates and very simple math equations. Variable names like matrix1_rows, matrix2_cols, product_rows, product_cols would make this much, much clearer, and keep you from making such mistakes
    If you use self-evident names, the solution becomes apparent.

  10. #40
    Registered User
    Join Date
    May 2012
    Posts
    210
    My current code:
    Code:
     for( i = 0 ; i < c ; i++ )
        {
          for( d = 0 ; d < m ; d++ )
    
    
            {
              product[i][d]=0;
               for(k=0;k<m;k++) {
                product[i][d] = product[i][d] + matrix[i][k] * transpose[k][d];
              }
            }
        }
      printf("\nThe Product Of The Two Matrices Is:\n\n");
      for(i=0; i<c; i++) {
        for(d=0; d<k; d++) {
          printf(" %3d ",product[i][d]);
        }
        printf("\n");
      }
    what i get when i enter integer 1-15:


    Enter the elements of matrix
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 99


    Here is your matrix:
    1 2 3 4 5
    6 7 8 9 10
    11 12 13 14 15
    Transpose of entered matrix :-
    1 6 11
    2 7 12
    3 8 13
    4 9 14
    5 10 15


    The Product Of The Two Matrices Is:


    55 130 205 -847853949 -574877379
    130 330 530 -1397410638 46177804
    205 530 855 -1946967327 667232987
    you see the 3 by 3 matrix is correct how do i get rid of the other 2 columns

    Please help!

  11. #41
    Registered User
    Join Date
    May 2012
    Posts
    210
    sorry about the variable name guys please bear with me though!

  12. #42
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by kiwi101 View Post
    you see the 3 by 3 matrix is correct how do i get rid of the other 2 columns
    Don't print them.

    Code:
    printf("\nThe Product Of The Two Matrices Is:\n\n");
      for(i=0; i<c; i++) {
        for(d=0; d<k; d++) {
          printf(" %3d ",product[i][d]);
    Why don't you listen to the people here? You have been told several times that your variable names are crap. You are accessing elements in your product-array which have garbage values because you have never set them (and don't need to).

    Bye, Andreas
    Last edited by AndiPersti; 10-04-2012 at 08:01 AM. Reason: wrong color highlighting

  13. #43
    Registered User
    Join Date
    May 2012
    Posts
    210
    im sorry i know my variable names are crap
    how can I not print them?
    i tried commenting it out but then it only shows that.
    please give me details my brian doesnt seem to be processing since this code has stressed me out way too much

  14. #44
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Matrix multiplication - Wikipedia, the free encyclopedia
    If you're multiplying a matrix with it's transposition (X,Y) * (Y,X), then the resultant matrix is going to be square (either X,X or Y,Y)

    Your output result isn't square.

    Which makes me suspect your calculation isn't square either.

    1. Initialise your arrays.
    Code:
      int m, x, n, c = 0, d, k, matrix[10][10] = { { 0 } }, transpose[10][10] = { { 0 } }, product[10][10] = { { 0 } };
    Then the results are
    Code:
    Enter the number of columns of matrix 5
    Enter the elements of matrix 
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 99
    
    Here is your matrix:
      1   2   3   4   5 
      6   7   8   9  10 
     11  12  13  14  15 
    Transpose of entered matrix :-
      1	  6	 11	
      2	  7	 12	
      3	  8	 13	
      4	  9	 14	
      5	 10	 15	
    
    The Product Of The Two Matrices Is:
    
      55  130  205    0    0 
     130  330  530    0    0 
     205  530  855    0    0
    NOW - go through and add some variable names like
    int matRows, matCols;
    int transRows, transCols;
    int multRows, multCols;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #45
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by kiwi101 View Post
    im sorry i know my variable names are crap
    Don't be sorry, just change them.

    Quote Originally Posted by kiwi101 View Post
    how can I not print them?
    Your condition in the inner for-loop is wrong. You don't want to increment "d" up to "k" (and "m" neither). So what's left, considering that the resulting product-array is square?

    Bye, Andreas

Page 3 of 6 FirstFirst 123456 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-01-2012, 10:57 PM
  2. How come short + int = int but short + uint = long?
    By y99q in forum C# Programming
    Replies: 2
    Last Post: 10-29-2011, 11:16 AM
  3. Please help! short while loop tracking problem.
    By matthayzon89 in forum C Programming
    Replies: 7
    Last Post: 04-22-2010, 12:29 PM
  4. Replies: 2
    Last Post: 04-21-2008, 07:43 PM
  5. Replies: 7
    Last Post: 02-08-2008, 05:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21