954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Homework assistance

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

#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

solomon grundy
Newbie Poster
6 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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 int main() not void main()

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

I'm still having trouble making this code work

#include <iostream>
 
<strong>using</strong> <strong>namespace</strong> std;
 
//Define the Matrix3X3 type.
<strong>typedef</strong> <strong>struct</strong>
<strong>{</strong>
<strong>float</strong> index<strong>[</strong>3<strong>][</strong>3<strong>]</strong>;
<strong>}</strong> Matrix3X3;
 
//Function prototypes (functions are defined below main())
<strong>void</strong> printMatrix<strong>(</strong> Matrix3X3 a <strong>)</strong>;
Matrix3X3 inputMatrix<strong>()</strong>;
Matrix3X3 addMatrices<strong>(</strong> Matrix3X3 a, Matrix3X3 b <strong>)</strong>;
Matrix3X3 subtractMatrices<strong>(</strong> Matrix3X3 a, Matrix3X3 b <strong>)</strong>;
Matrix3X3 scalarMultiply<strong>(</strong> Matrix3X3 a, <strong>float</strong> scale <strong>)</strong>;
 
//main() function
<strong>void</strong> main<strong>()</strong>
<strong>{</strong>
<strong>cout</strong> << "Please enter matrix A:" << endl;
 
Matrix3X3 A = inputMatrix<strong>()</strong>;
 
<strong>cout</strong> << "Matrix A:" << endl;
 
printMatrix<strong>(</strong> A <strong>)</strong>;
 
<strong>cout</strong> << "Please enter matrix B:" << endl;
 
Matrix3X3 B = inputMatrix <strong>()</strong> ;
 
<strong>cout</strong> << "Matrix B:" <
 
printMatrix <strong>(</strong> B <strong>)</strong>;
 
 
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]; 
} 

 
<strong>void</strong> printMatrix<strong>(</strong> Matrix3X3 a <strong>)</strong>
<strong>{</strong>
//loop through rows
<strong>for(</strong> <strong>int</strong> i=0; i < 3; ++i <strong>)</strong>
<strong>{</strong>
//loop through column at each row
<strong>for(</strong> <strong>int</strong> j=0; j < 3; ++j <strong>)</strong>
<strong>{</strong>
//i is the row
//j is the column
<strong>float</strong> temp = a.index<strong>[</strong>i<strong>][</strong>j<strong>]</strong>;
<strong>cout</strong> << temp << " ";
<strong>}</strong>
//go to the next line after each row
<strong>cout</strong> << endl;
<strong>}</strong>
<strong>}</strong>
 
Matrix3X3 inputMatrix<strong>()</strong>
<strong>{</strong>
Matrix3X3 temp;
 
//loop through rows
<strong>for(</strong> <strong>int</strong> i=0; i < 3; ++i <strong>)</strong>
<strong>{</strong>
//loop through column at each row
<strong>for(</strong> <strong>int</strong> j=0; j < 3; ++j <strong>)</strong>
<strong>{</strong>
//i is the row
//j is the column
<strong>cout</strong> << "Enter row " << i << " column " << j << ":" << endl;
<strong>cin</strong> >> temp.index<strong>[</strong>i<strong>][</strong>j<strong>]</strong>;
<strong>}</strong>
<strong>}</strong>
 
<strong>return</strong> temp;
<strong>}</strong>
solomon grundy
Newbie Poster
6 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

Shall I go through with the standard procedure?

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 }



Missing semicolon:

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 ;



Try that, and your code should compile.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

are scalarMultiply and subractMatrices returning anything? ;)

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 
are scalarMultiply and subractMatrices returning anything? ;)


Good point. :cheesy: Another function that has a similar state to the other 2 is inputMatrix .

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

No it's not, and thats the problem, After I Build it and run it. It will input matrix A and then tell me to input Matrix B, after I put in numbers to Matrix B however after that its done. What am I missing?

solomon grundy
Newbie Poster
6 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

how about return result; ?

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You