User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,107 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,097 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 987 | Replies: 8
Reply
Join Date: Nov 2006
Posts: 6
Reputation: solomon grundy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
solomon grundy solomon grundy is offline Offline
Newbie Poster

Homework assistance

  #1  
Dec 11th, 2006
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
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. //Define the Matrix3X3 type.
  6. typedef struct
  7. {
  8. float index[3][3];
  9. } Matrix3X3;
  10.  
  11. //Function prototypes (functions are defined below main())
  12. void printMatrix( Matrix3X3 a );
  13. Matrix3X3 inputMatrix();
  14. Matrix3X3 addMatrices( Matrix3X3 a, Matrix3X3 b );
  15. Matrix3X3 subtractMatrices( Matrix3X3 a, Matrix3X3 b );
  16. Matrix3X3 scalarMultiply( Matrix3X3 a, float scale );
  17.  
  18. //main() function
  19. void main()
  20. {
  21. cout << "Please enter matrix A:" << endl;
  22.  
  23. Matrix3X3 A = inputMatrix();
  24.  
  25. cout << "Matrix A:" << endl;
  26.  
  27. printMatrix( A );
  28.  
  29. cout << "Please enter matrix B:" << endl;
  30.  
  31. Matrix3X3 B = inputMatrix () ;
  32.  
  33. cout << "Matrix B:" <
  34.  
  35. printMatrix ( B );
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. //The result I want is A - 5B.
  43. //I can hold the result in a new matrix and print it out to complete the program.
  44.  
  45. }
  46.  
  47. void printMatrix( Matrix3X3 a )
  48. {
  49. //loop through rows
  50. for( int i=0; i < 3; ++i )
  51. {
  52. //loop through column at each row
  53. for( int j=0; j < 3; ++j )
  54. {
  55. //i is the row
  56. //j is the column
  57. float temp = a.index[i][j];
  58. cout << temp << " ";
  59. }
  60. //go to the next line after each row
  61. cout << endl;
  62. }
  63. }
  64.  
  65. Matrix3X3 inputMatrix()
  66. {
  67. Matrix3X3 temp;
  68.  
  69. //loop through rows
  70. for( int i=0; i < 3; ++i )
  71. {
  72. //loop through column at each row
  73. for( int j=0; j < 3; ++j )
  74. {
  75. //i is the row
  76. //j is the column
  77. cout << "Enter row " << i << " column " << j << ":" << endl;
  78. cin >> temp.index[i][j];
  79. }
  80. }
  81.  
  82. return temp;
  83. }
  84.  

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 2:06 pm. Reason: Added code tags learn to use them yourself.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Location: Bellevue, WA
Posts: 1,548
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Rep Power: 8
Solved Threads: 51
Sponsor
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Homework assistance

  #2  
Dec 11th, 2006
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()
Last edited by Infarction : Dec 11th, 2006 at 1:58 pm.
Reply With Quote  
Join Date: Nov 2006
Posts: 6
Reputation: solomon grundy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
solomon grundy solomon grundy is offline Offline
Newbie Poster

Re: Homework assistance

  #3  
Dec 12th, 2006
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 1:49 am. Reason: You've been asked to use code tags more than once.
Reply With Quote  
Join Date: Jul 2005
Posts: 1,157
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 9
Solved Threads: 152
Lerner Lerner is offline Offline
Veteran Poster

Re: Homework assistance

  #4  
Dec 12th, 2006
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.
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,504
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 17
Solved Threads: 275
Moderator
Featured Blogger
John A's Avatar
John A John A is online now Online
Vampirical Moderator

Re: Homework assistance

  #5  
Dec 12th, 2006
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.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: May 2006
Location: Bellevue, WA
Posts: 1,548
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Rep Power: 8
Solved Threads: 51
Sponsor
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Homework assistance

  #6  
Dec 12th, 2006
are scalarMultiply and subractMatrices returning anything?
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,504
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 17
Solved Threads: 275
Moderator
Featured Blogger
John A's Avatar
John A John A is online now Online
Vampirical Moderator

Re: Homework assistance

  #7  
Dec 12th, 2006
Originally Posted by Infarction View Post
are scalarMultiply and subractMatrices returning anything?
Good point. :cheesy: Another function that has a similar state to the other 2 is inputMatrix.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Nov 2006
Posts: 6
Reputation: solomon grundy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
solomon grundy solomon grundy is offline Offline
Newbie Poster

Re: Homework assistance

  #8  
Dec 13th, 2006
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?
Reply With Quote  
Join Date: May 2006
Location: Bellevue, WA
Posts: 1,548
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Rep Power: 8
Solved Threads: 51
Sponsor
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Homework assistance

  #9  
Dec 13th, 2006
how about return result;?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:57 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC