![]() |
|
#1
|
||||
|
||||
C++ MatrixPlease help me with my matrices program. I can't seem to get it to multiply, addition and subtraction work fine. Here's my code
CPP / C++ / C Code:
Any help will be appreciated. |
|
#2
|
|||
|
|||
Re: C++ MatrixQuote:
Given matrices left, which has n rows and x columns and right, which has x rows and m columns, then if you want product = left times right the product matrix has n rows and m columns Each element in the product matrix is defined by: Code:
Where k is equal to the number of columns of left (and, therefore, is equal to the number of rows of right). Regards, Dave |
|
#3
|
||||
|
||||
Re: C++ MatrixThanks for the tip(it does semi-work now), but my real problem is that when I try to multiply a 3x1 and 1x3, I don't get a 3x3. If you know anyway to fix this, then I think it would work correctly.
|
|
#4
|
|||
|
|||
Re: C++ MatrixQuote:
Either it works or it doesn't work. If it gives expected results for some cases and not for others, then, by my definition: It Doesn't Work. Your original code worked only for two square matrices of the same size. Since you haven't shown what changes you made (if any), how could I guess why it doesn't work now? If you haven't made any changes, then go back and look at my description and check the ranges of the loop variables in the multiplication function. By the way, you have loops that like this: CPP / C++ / C Code:
and CPP / C++ / C Code:
I don't know how you defined your matrix but in "normal" C (and C++) programs I would expect to see them like the following: CPP / C++ / C Code:
CPP / C++ / C Code:
I can't say that yours are wrong, since I don't know where you got the memory from and it is certainly possible to do a little trick with pointers from dynamically allocated arrays so that they can look like that. This is done in the infamous "Numerical Recipes in C" book --- written, apparently by a bunch of old FORTRAN programmers. The stuff actually works pretty much as advertised, but just irritates the hell out of most C programmers that I know, and isn't really satisfying to most of the old FORTRAN programmers that I have known. Anyhow, if you show the code that you used, and give an example of what you tried and what the program gave as a result, maybe someone can help you figure it out. Regards, Dave |
|
#5
|
||||
|
||||
Re: C++ MatrixO.K. Dave, the code is below, my problem is trying to multiply matrices of anything different sizes, which you stated earlier. If you could do me a favor it would be highly appreciated, when reading my code, about my program, don't critique the minor things, look at the big picture.
CPP / C++ / C Code:
Regards, Me ![]() |
|
#6
|
|||
|
|||
Re: C++ MatrixQuote:
OK. Without editorial comment, I will go over the contents of the multiplication function. You got it "almost" right. What is the sizeof ans.mat? It has r rows and b.c columns. So your call to the constructor looks good. Now, you want to calculate ans.mat[i][j] for all rows and all columns of ans. So you have a loop for i and a nested loop for j and finally an inner loop for k that does the calculation for each element of the product matrix. What is the range of i? It should go from 1 to r What is the range of j? It should go from 1 to b.c Finally, the inner loop: for each value of i and j, the inner loop adds up the product of the column elements of the left hand and the corresponding row element of the right hand side. I see that you have corrected the subscripts on the product terms, and you only have to check the loop limit for k. What is the range of k? It should go from 1 to c (or you could say that it should go from 1 to b.r since c is equal to b.r). Code:
Regards, Dave |
|
#7
|
||||
|
||||
Re: C++ MatrixThank you so much, you don't know how much I appreciate your help
P.S. Sorry for being a jerk earlier. |
|
#8
|
|||
|
|||
Re: C++ MatrixQuote:
Did I miss something? I thought I was the jerk. Oh, well... Regards, Dave |
Recent GIDBlog
Halfway done! by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| i need help in C++ PLZ | its_me | C++ Forum | 3 | 04-Dec-2006 21:51 |
| how to initialize a char matrix matrix with empty elements? | swedenguy | C Programming Language | 4 | 22-Aug-2006 11:43 |
| how to initialize a char matrix matrix with empty elements? | swedenguy | C Programming Language | 1 | 22-Aug-2006 11:15 |
| debugging multi-dimensional vector/ matrix class | counterflow | C++ Forum | 1 | 15-Mar-2006 12:14 |
| Combining Vectors and References | Frankg | C++ Forum | 7 | 14-Jan-2006 06:17 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The