| | |
I would liike to multiply two matrices but my code is not working..help me out!
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2006
Posts: 6
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <fstream.h> main(int argc, char *argv[]) { //definition of the variables. int mtx1[20][20], mtx2[20][20], mtx3[20][20]; int n, i, j, k; //exit if the number of arguments is not 2. if(argc != 3) { cerr << "Usage: mm <filename1> <filename2>\n"; return 1; } //open the input file 1. exit if an error occurs. ifstream fin1(argv[1]); if(!fin1) { cerr << "Can't open file1!\n"; return 1; } //read the input file 1. fin1 >> n; //read the number of rows and columns. for(j=1; j<=n; j++) { for(i=1; i<=n; i++) { fin1 >> mtx1[i][j]; //read the elements of the matrix. } } fin1.close(); //close the file 1. //open the input file 2. exit if an error occurs. ifstream fin2(argv[2]); if(!fin2) { cerr << "Can't open file2!\n"; return 1; } fin2 >> n; //read the number of rows and columns. for(j=1; j<=n; j++) { for(i=1; i<=n; i++) { fin2 >> mtx2[i][j]; //read the elements of the matrix. } } fin2.close(); //close the file 2. //fill the matrix 3 for the result with zero. for(j=1; j<=n; j++) { for(i=1; i<=n; i++) { mtx3[i][j] = 0; } } //multiply the matrices. for(j=1; j<=n; j++) { for(i=1; i<=n; i++) { for(k=1; k<=n; k++) { mtx3[i][j] += mtx2[i][k] * mtx1[k][j]; } } } //output the result. cout << n << "\n"; for(j=1; j<=n; j++) { for(i=1; i<=n; i++) { cout << mtx3[i][j]; if(i < n) { cout << "\t"; } } cout << "\n"; } return 0; } //end of the program.
Last edited by Salem; Nov 4th, 2006 at 4:52 am. Reason: Added code tags - learn to use them yourself!!!
Re: I would liike to multiply two matrices but my code is not working..help me out!
0
#2 Nov 4th, 2006
A fat lot of good that did!
Added code tags, but it's still unindented crap.
>
I would liike to multiply two matrices but my code is not working..help me out!
Post a proper question.
Don't just dump the code and hope someone will figure it out.
Does it compile?
Does it run?
What symptoms are there (error messages etc)
What input do you give and what answers do you get / expect ?
Added code tags, but it's still unindented crap.
>
I would liike to multiply two matrices but my code is not working..help me out!
Post a proper question.
Don't just dump the code and hope someone will figure it out.
Does it compile?
Does it run?
What symptoms are there (error messages etc)
What input do you give and what answers do you get / expect ?
![]() |
Similar Threads
- matrix-chain multiplication (C++)
- adding matrices in c++ (C++)
- my html code isnt working please help (JavaScript / DHTML / AJAX)
- Code is written, but not working. (Java)
- Unary Operators (C++)
Other Threads in the C++ Forum
- Previous Thread: pass by help
- Next Thread: Standard Code Indentation
Views: 4083 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






