hi. can you guys help me ?

i am supposed to represent matrices to solve simutaneous equations, add them, find inverse etc.
i am trying to help myself , but i am stuck.all i have managed to do so far is understand how to display a matrix. i am using borland C++. can someone show me how to add 2 matrices.

# include <iostream.h>
# include <math.h>
#include <conio.h>
void main()
{



int a,b,c,d,e,f,g,h,l ;


cout << "Enter a:" << endl;
cin >> a;
cout << "Enter b:" << endl;
cin >>b;
cout << "Enter c:" << endl;
cin >>c;
cout << "Enter d:" << endl;
cin >>d;
cout << "Enter e:" << endl;
cin >>e;
cout << "Enter f:" << endl;
cin >> f;
cout << "Enter g:" << endl;
cin >> g;
cout << "Enter h" << endl;
cin >> h;
cout << "Enter l" << endl;
cin >> l; // if you were to accidentally leave out one of the cins it would say possible l without definition...


int table[3][3] ={{a,b,c},{d,e,f},{g,h,l}};



for(int i =0; i<3; i++)
{

for(int j = 0; j<3; j++)
{
cout<<table[i][j]<<"\t";

}

cout<< endl;
}

getch();

}
closed account (D80DSL3A)
Adding 2 matrices is easy. I see you can assign values to the elements ( as you did with table[][] by prompting the user for the values ). If you have 2 3x3 matrices M1 and M2 then you can add them to find the sum M3 = M1 + M2 by adding the corresponding elements.
1
2
3
4
5
6
7
8
int M1[3][3] = {{a,b,c},{d,e,f},{g,h,l}};// if M1 and M2 are given
int M2[3][3] = {{j,k,l},{m,n,o},{p,q,r}};

// find the sum
int M3[3][3];
for(int i =0; i<3; i++)// note that the braces may be omitted if there is only 1 instruction in the loop
    for(int j = 0; j<3; j++)    
        M3[i][j] = M1[i][j] + M2[i][j];

Simple as that!
Code to add 2 matrices of 3 x 3 order(for 2 x 2matrix, change the values of '3' to '2'):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
using namespace std;
int main()
{
double A[3][3], B[3][3], C[3][3];

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<"Enter a"<<i<<j<<endl;
cin>>A[i][j];
cout<<endl;
}
}

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<"Enter b"<<i<<j<<endl;
cin>>B[i][j];
cout<<endl;
}
}

for (int i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
C[i][j] = A[i][j] + B[i][j];  //addition stored in another matrix; C=A+B
}
}
}

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<"C"<<i<<j<<" "<<C[i][j]<<'\t';
}
cout<<endl;
}


NOTE: I'm a matrix specialist, can send you any code related to matrix, like addition, subtraction, multiplication, inverse(by the method you want), solving system of 2(or 3) equations, etc
Topic archived. No new replies allowed.