| | |
adding matrices in c++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2006
Posts: 3
Reputation:
Solved Threads: 0
Hi again, all---
I'm trying to write a function that will add two matrices and output the resulting matrix.
I've got two matrices already and everything, now i just need to write the function that i can use to call in my switch loop (The program must read two matrices, and the user can then input if they want to add, subtract, or multiply them, and then see their resultant matrix.)
So i've just pasted the function definition i'm trying to use.
In my little command window, it shows me an answer but it's completely wrong!! i don't know how to make a new matrix that adds and i'm kind of lost and frustrated...if someone could give me tips or a skeleton of a function that might work, i'd be much obliged! thanks!
Actually, I'll show you the entire code here, it's incomplete, but as soon as I know how to add (and therefore subtract and multiply) matrices I can fill it all in.
I'm trying to write a function that will add two matrices and output the resulting matrix.
I've got two matrices already and everything, now i just need to write the function that i can use to call in my switch loop (The program must read two matrices, and the user can then input if they want to add, subtract, or multiply them, and then see their resultant matrix.)
So i've just pasted the function definition i'm trying to use.
C++ Syntax (Toggle Plain Text)
double AddMatrix(double x[][5], double y[][5]) { double matsum[5][5]; int row, col; for(row=0; row<5; row++) for(col=0; col<5; col++) { matsum[5][5]=(x[row][col]+y[row][col]); } return matsum[5][5]; }
In my little command window, it shows me an answer but it's completely wrong!! i don't know how to make a new matrix that adds and i'm kind of lost and frustrated...if someone could give me tips or a skeleton of a function that might work, i'd be much obliged! thanks!
Actually, I'll show you the entire code here, it's incomplete, but as soon as I know how to add (and therefore subtract and multiply) matrices I can fill it all in.
C++ Syntax (Toggle Plain Text)
/*--------------------------------------------------------------------*/ /* Final Exam- Matrix Operations */ /* */ /* This program will take two data files, open them, read them, save */ /* their data in arrays, then let the user choose if they want to */ /* add, subtract, or multiply the matrices as many times as they want*/ /* until they choose the "quit" option, and show the results. */ /* */ #include <iostream> //Include the necessary libraries. #include <cstdlib> #include <fstream> #include <string> using namespace std; double AddMatrix(double x[][5], double y[][5]); // Function Prototype(s) Will Go Here //Begin main function int main() { const int nrows=5; //Define the necessary constants const int ncols=5; //Define variables double Matrix1Info[nrows][ncols]; double Matrix2Info[nrows][ncols]; char ch; int row, col; ifstream matrix, matrix2; string filename, filename2; // Ask user for name of input file 1. cout << "Enter first input file: " <<endl; cin >> filename; // Open file and read the info. matrix.open(filename.c_str()); if( matrix.fail() ) { cout << "Error opening input file\n"; } else { for (row=0; row<nrows; row++) for (col=0; col<ncols; col++) matrix >> Matrix1Info[row][col]; //Stores info into first matrix array for(int r=0; r<5; r++) { for(int c=0; c<5; c++) { cout << Matrix1Info[r][c] << ' '; } cout << endl; } cout << "Enter second input file: " <<endl; //Gets 2nd matrix file cin >> filename2; matrix2.open(filename2.c_str()); //Opens 2nd file if( matrix2.fail() ) { cout << "Error opening input file\n"; } else { for (row=0; row<nrows; row++) for(col=0; col<ncols; col++) matrix2 >> Matrix2Info[row][col]; //Stores info into 2nd matrix array for(int f=0; f<5; f++) { for(int g=0; g<5; g++) { cout << Matrix2Info[f][g] << ' '; } cout << endl; } cout << "Enter A for addition, S for subtraction, M for multiplication, or Q to quit: " << endl; cin >> ch; while(ch != 'Q') { switch(ch) { case 'A': cout << AddMatrix << endl; cout << "Enter A for addition, S for subtraction, M for multiplication, or Q to quit: " <<endl; case 'S': //function for subtraction goes here case 'M': //function for multiplication goes here break; default: cout << "Invalid operation choice." << endl; cout << "Enter A for addition, S for subtraction, M for multiplication, or Q to quit: " <<endl; }//end switch cin >> ch; }//end while loop } } //End of main file. return 0; } double AddMatrix(double x[][5], double y[][5]) { double matsum[5][5]; int row, col; const int nrows=5; const int ncols=5; for(row=0; row<5; row++) for(col=0; col<5; col++) { matsum[nrows][ncols]=(x[row][col]+y[row][col]); } return matsum[5][5]; }
Last edited by amethystglow; Jul 5th, 2006 at 4:23 pm.
Try doing it bit by bit and not all at one time.
Placing a few couts here and there should help debugging.
I'd probably create three matrices. One from matrix one, another for the second matrix and a third for where the result will be stored.
Matrix addition and subtraction is trivial. You may need more time to think about matrix multiplication however, there are so many example already on the net you should have no problem.
Placing a few couts here and there should help debugging.
I'd probably create three matrices. One from matrix one, another for the second matrix and a third for where the result will be stored.
Matrix addition and subtraction is trivial. You may need more time to think about matrix multiplication however, there are so many example already on the net you should have no problem.
*Voted best profile in the world*
Also be careful how you pass an array back to main. The syntax is slightly tricky.
And you may have to test if you are reading the information from your file into your 2d array properly.
Try changing this:-
to this:-
And you may have to test if you are reading the information from your file into your 2d array properly.
Try changing this:-
C++ Syntax (Toggle Plain Text)
matsum[5][5]=(x[row][col]+y[row][col]);
to this:-
C++ Syntax (Toggle Plain Text)
matsum[row][col]=(x[row][col]+y[row][col]);
Last edited by iamthwee; Jul 5th, 2006 at 6:21 pm.
*Voted best profile in the world*
![]() |
Similar Threads
- Forum moderators & adding phpbb mods (Web Development Job Offers)
- Adding controls into a ListView (Visual Basic 4 / 5 / 6)
- adding quick reply to a phpbb board (Social Media and Online Communities)
- Adding Totals from Listbox DB (DAO) (Visual Basic 4 / 5 / 6)
- My ie keeps adding favorites w/out my permission and . . . (Web Browsers)
- adding totals in listboxes (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: inheritance, polymorphism, and other features
- Next Thread: Baffling Unresolved External Symbol
| Thread Tools | Search this Thread |
Tag cloud for C++
api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg search simple spoonfeeding string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






