# short matrix problem

Show 80 post(s) from this thread on one page
Page 3 of 6 First 123456 Last
• 10-03-2012
anduril462
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 :p.
• 10-04-2012
kiwi101
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");   }```
• 10-04-2012
Salem
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!)
• 10-04-2012
kiwi101
sorry i made the first one before and I didnt think anyone was replying to it :(
• 10-04-2012
kiwi101
• 10-04-2012
kiwi101
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.
• 10-04-2012
kiwi101
Now its not even posting the right answer anymore
OH CRAP!!
• 10-04-2012
AndiPersti

Bye, Andreas
• 10-04-2012
The only problem I see with your program is the terrible variable names you're using.
Quote:

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-04-2012
kiwi101
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

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

• 10-04-2012
kiwi101
sorry about the variable name guys please bear with me though! :)
• 10-04-2012
AndiPersti
Quote:

Originally Posted by kiwi101
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
• 10-04-2012
kiwi101
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
• 10-04-2012
Salem
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)

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

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;
• 10-04-2012
AndiPersti
Quote:

Originally Posted by kiwi101
im sorry i know my variable names are crap

Don't be sorry, just change them.

Quote:

Originally Posted by kiwi101
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
Show 80 post(s) from this thread on one page
Page 3 of 6 First 123456 Last