| | |
Homework assistance
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2006
Posts: 6
Reputation:
Solved Threads: 0
I need to construct a C++ program where I input from the screen the entries of two 3x3 matrices A and B. My program then computes A-5B. The matrices should be declared as two dimensional arrays.
What I have so far
How do I
1) Use the functions to calculate the result
2) And define the rest of the matrix.
Thanks
What I have so far
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; //Define the Matrix3X3 type. typedef struct { float index[3][3]; } Matrix3X3; //Function prototypes (functions are defined below main()) void printMatrix( Matrix3X3 a ); Matrix3X3 inputMatrix(); Matrix3X3 addMatrices( Matrix3X3 a, Matrix3X3 b ); Matrix3X3 subtractMatrices( Matrix3X3 a, Matrix3X3 b ); Matrix3X3 scalarMultiply( Matrix3X3 a, float scale ); //main() function void main() { cout << "Please enter matrix A:" << endl; Matrix3X3 A = inputMatrix(); cout << "Matrix A:" << endl; printMatrix( A ); cout << "Please enter matrix B:" << endl; Matrix3X3 B = inputMatrix () ; cout << "Matrix B:" < printMatrix ( B ); //The result I want is A - 5B. //I can hold the result in a new matrix and print it out to complete the program. } void printMatrix( Matrix3X3 a ) { //loop through rows for( int i=0; i < 3; ++i ) { //loop through column at each row for( int j=0; j < 3; ++j ) { //i is the row //j is the column float temp = a.index[i][j]; cout << temp << " "; } //go to the next line after each row cout << endl; } } Matrix3X3 inputMatrix() { Matrix3X3 temp; //loop through rows for( int i=0; i < 3; ++i ) { //loop through column at each row for( int j=0; j < 3; ++j ) { //i is the row //j is the column cout << "Enter row " << i << " column " << j << ":" << endl; cin >> temp.index[i][j]; } } return temp; }
How do I
1) Use the functions to calculate the result
2) And define the rest of the matrix.
Thanks
Last edited by ~s.o.s~; Dec 11th, 2006 at 3:06 pm. Reason: Added code tags learn to use them yourself.
1) How would you do this if you were doing it by hand? Probably something like this:
- scale B by 5.
- subtract that matrix from A
2) what do you mean? It looks to me like you need to define subtractMatrices and scalarMultiply, but otherwise I don't understand what you need to define.
3) You do need to do the extra calls in main() to get the A-5B result.
4) You should use [code] tags when you post so we can read your code easily.
PS - it's
- scale B by 5.
- subtract that matrix from A
2) what do you mean? It looks to me like you need to define subtractMatrices and scalarMultiply, but otherwise I don't understand what you need to define.
3) You do need to do the extra calls in main() to get the A-5B result.
4) You should use [code] tags when you post so we can read your code easily.
PS - it's
int main() not void main() Last edited by Infarction; Dec 11th, 2006 at 2:58 pm.
•
•
Join Date: Nov 2006
Posts: 6
Reputation:
Solved Threads: 0
I'm still having trouble making this code work
#include <iostream> using namespace std; //Define the Matrix3X3 type. typedef struct { float index[3][3]; } Matrix3X3; //Function prototypes (functions are defined below main()) void printMatrix( Matrix3X3 a ); Matrix3X3 inputMatrix(); Matrix3X3 addMatrices( Matrix3X3 a, Matrix3X3 b ); Matrix3X3 subtractMatrices( Matrix3X3 a, Matrix3X3 b ); Matrix3X3 scalarMultiply( Matrix3X3 a, float scale ); //main() function void main() { cout << "Please enter matrix A:" << endl; Matrix3X3 A = inputMatrix(); cout << "Matrix A:" << endl; printMatrix( A ); cout << "Please enter matrix B:" << endl; Matrix3X3 B = inputMatrix () ; cout << "Matrix B:" < printMatrix ( B ); Matrix3X3 scalarMultiply( Matrix3X3 a, float scale ) { Matrix3X3 result; for(int i=0;i<3; i++) for(int j=0;j<3; j++) result.index[j][i] = scale * a.index[j][i] } Matrix3X3 subtractMatrices( Matrix3X3 a, Matrix3X3 b ) { Matrix3X3 result; for(int i=0;i<3; i++) for(int j=0;j<3; j++) result.index[j][i] =a.index[j][i] - b.index[j][i]; } void printMatrix( Matrix3X3 a ) { //loop through rows for( int i=0; i < 3; ++i ) { //loop through column at each row for( int j=0; j < 3; ++j ) { //i is the row //j is the column float temp = a.index[i][j]; cout << temp << " "; } //go to the next line after each row cout << endl; } } Matrix3X3 inputMatrix() { Matrix3X3 temp; //loop through rows for( int i=0; i < 3; ++i ) { //loop through column at each row for( int j=0; j < 3; ++j ) { //i is the row //j is the column cout << "Enter row " << i << " column " << j << ":" << endl; cin >> temp.index[i][j]; } } return temp; }
Last edited by WaltP; Dec 13th, 2006 at 2:49 am. Reason: You've been asked to use code tags more than once.
•
•
Join Date: Jul 2005
Posts: 1,761
Reputation:
Solved Threads: 283
And there's some animal screaming bloody murder out my window at the moment too, but without anymore information about the problem I'm just as helpless in either case. Please indicate what the problem is, not just that you have one.
Also, please enclose your code in code tags---read the FAQ at the top of the board if you don't know how to do so.
Also, please enclose your code in code tags---read the FAQ at the top of the board if you don't know how to do so.
Shall I go through with the standard procedure?
Don't use
Did you forget a
Missing semicolon:
Try that, and your code should compile.
Don't use
void main. It's bad for you. (Or rather, for your OS)Did you forget a
<< endl; at the end of your statement? (Also missing closing brace)cout << "Please enter matrix B:" << endl; Matrix3X3 B = inputMatrix () ; cout << "Matrix B:" < printMatrix ( B ); // missing closing }
Matrix3X3 scalarMultiply( Matrix3X3 a, float scale )
{
Matrix3X3 result;
for(int i=0;i<3; i++)
for(int j=0;j<3; j++)
result.index[j][i] = scale * a.index[j][i] // add ; "Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- C++ A little homework help, Thanks very much for the last assistance (C++)
- C++ Homework Assistance... (C++)
Other Threads in the C++ Forum
- Previous Thread: Still need help with opening a file and store the data on it in an array
- Next Thread: I am having some issues with some basic .net c++
Views: 1270 | Replies: 8
| 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 download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






