944,167 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7619
  • C++ RSS
Dec 18th, 2006
0

is this matrix code working?

Expand Post »
Is this code compiling
#include <iostream>
usingnamespace std;
//Define the Matrix3X3 type.
typedefstruct
{
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:" <<endl;
printMatrix ( B );
printMatrix(scalarMultiply(A, 3.1415)); 
} 
 
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]; 
return result;
}
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]; 
return result;
} 
 
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;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
solomon grundy is offline Offline
6 posts
since Nov 2006
Dec 18th, 2006
0

Re: is this matrix code working?

>is this code compiling?

Surely you could answer this yourself.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Dec 19th, 2006
0

Re: is this matrix code working?

don't think so:

Quote ...
usingnamespace std;


Niek
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Looking for help with C++ game of life program. Will pay or make donation for tutor.
Next Thread in C++ Forum Timeline: Assistace please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC