You are on page 1of 19

COMPUTER AIDED STRUCTURAL ANALYSIS

CS 520A

PROGRAMMING NO. 2
ADDITION/ SUBTRACTION
INVERSE
MULTIPLICATION

SUBMITTED BY:
GONZALES, JOHN CARLO B.

SUBMITTED TO:
ENGR. LEONARDO V. SURIO M. ENG.
DEAN- CEDE
/*THIS PROGRAM WILL COMPUTE FOR THE UNKNOWN USING AUGMENTED MATRIX
METHOD. THE USER IS REQUIRED TO INPUT THE EQUATIONS IN MATRIX FORM.
THE ELEMENTS OF THE MATRIX SHALL BE INPUTED ROW WISE */
#include<iostream.h>
#include<conio.h>
class row{
int i,j,k,n,a;
float A[100][100],c,x[100],sum;
public:
row(){

sum=0.0;
cout<<"This Program will solve for the unknown using augmented
matrix method";
cout<<"\nHow many unknowns do you want to solve: ";
cin>>n;
a=n;
cout<<"\nEnter the elements of augmented matrix(ROW WISE):\n\n";
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
if(a<j){cout<<"\nEnter the Value of Constant: :\n ";}
cout<<"A["<<i<<"]["<<j<<"] : ";
cin>>A[i][j];

}
}
}

void operation() {
for(j=1; j<=n; j++)
{
for(i=1; i<=n; i++)
{
if(i>j)
{
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]-c*A[j][k];
}
}
}
}

x[n]=A[n][n+1]/A[n][n];
for(i=n-1; i>=1; i--)
{
sum=0;
for(j=i+1; j<=n; j++)
{
sum=sum+A[i][j]*x[j];
}
x[i]=(A[i][n+1]-sum)/A[i][i];
}
}

void result(){
cout<<"\nThe solution is: \n";
for(i=1; i<=n; i++)
{
cout<<"\nx"<<i<<" = "<<x[i]<<"\t";
}
}

void display()
{
cout<<"\n The Matrix is :-\n ";
for(int i=1;i<=n;i++){
cout<<endl;
for(int j=1;j<=n+1;j++){
cout<<A[i][j]<<" ";
}}
}

};
main()
{

row obj;
obj.operation();

obj.result();
getch();

}
/* THE PROGRAM IS DESIGN TO MULTIPLY THE MATRIX */
#include <iostream>
using namespace std;

int main()
{
int a[100][100], b[100][100], mult[100][100], r1, c1, r2, c2, i,
j, k;
cout<<"This program will compute the Product of two matrix";
cout
<<"\n________________________________________________________________"
;
cout << "\nEnter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout
<<"\n_______________________________________________________________";
cout << "\nEnter rows and columns for second matrix: ";
cin >> r2 >> c2;

while (c1!=r2)
{
cout << "\nError! THE COLUMN OF THE FIRST MATRIX SHOULD BE
EQUAL TO THE ROW OF THE SECOND MATRIX.Try AGAIN !!!!!!!";
cout
<<"\n_______________________________________________________________";
cout << "\nEnter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout
<<"\n_______________________________________________________________";
cout << "\nEnter rows and columns for second matrix: ";
cin >> r2 >> c2;
}

cout << endl << "Enter elements of the FIRST MATRIX(Row-wise):" <<
endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c1; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}

cout << endl << "Enter elements of the SECOND MATRIX(Row-wise):"


<< endl;
for(i = 0; i < r2; ++i)
for(j = 0; j < c2; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}

for(i = 0; i < r1; ++i)


for(j = 0; j < c2; ++j)
{
mult[i][j]=0;
}

for(i = 0; i < r1; ++i)


for(j = 0; j < c2; ++j)
for(k = 0; k < c1; ++k)
{
mult[i][j] += a[i][k] * b[k][j];
}

cout << endl << "ANSWER: " << endl;


for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{
cout << " " << mult[i][j];
if(j == c2-1)
cout << endl;
}

return 0;
}
/* THIS PROGRAM WILL COMPUTE FOR THE SUM OF TWO DIFFERENT MATRIX GIVEN
THAT THEY HAVE THE SAME DIMENSION */
#include<iostream>

using namespace std;

main()
{
int m, n, c, d, first[100][100], second[100][100], sum[100][100];
cout<<"THIS PROGRAM WILL COMPUTE FOR THE SUM OF THE TWO MATRIX\n";
cout<<"________________________________________________________\n";
cout << "Enter the dimension of the matrix (row-column)";
cin >> m >> n;
cout << "INPUT THE ELEMENTS OF THE FIRST MATRIX (ROW WISE)\n";
if(m==n){
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
cin >> first[c][d];

cout << "INPUT THE ELEMENTS OF THE SECOND MATRIX (ROW WISE)\n";

for ( c = 0 ; c < m ;c++ )


for ( d = 0 ; d < n ; d++ )
cin >> second[c][d];

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];

cout << "THE SUM OF THE INPUTED MATRICES IS:\n";

for ( c = 0 ; c < m ; c++ )


{
for ( d = 0 ; d < n ; d++ )
cout << sum[c][d] << "\t";
cout << endl;
}
}
else {
cout<<"\n The dimension of the matrix you have entered is
wrong\n";
}

return 0;
}
/* THIS PROGRAM WILL COMPUTE FOR THE DIFFERENCE OF TWO DIFFERENT
MATRIX GIVEN THAT THEY HAVE THE SAME DIMENSION */
#include<iostream>

using namespace std;

main()
{
int m, n, c, d, first[100][100], second[100][100],diff[100][100];
cout<<"THIS PROGRAM WILL COMPUTE FOR THE DIFFERENCE OF THE TWO
MATRIX\n";
cout<<"________________________________________________________\n";
cout << "Enter the dimension of the matrix (row-column):";
cin >> m >> n;
cout << "INPUT THE ELEMENTS OF THE FIRST MATRIX (ROW WISE):\n";
if(m==n){
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
cin >> first[c][d];

cout << "INPUT THE ELEMENTS OF THE SECOND MATRIX (ROW WISE):\n";

for ( c = 0 ; c < m ;c++ )


for ( d = 0 ; d < n ; d++ )
cin >> second[c][d];

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
diff[c][d] = first[c][d] - second[c][d];

cout << "THE DIFFERENCE OF THE INPUTED MATRICES IS:\n";

for ( c = 0 ; c < m ; c++ )


{
for ( d = 0 ; d < n ; d++ )
cout << diff[c][d] << "\t";

cout << endl;


}
}
else {
cout<<"\n The dimension of the matrix you have entered is
wrong\n";
}

return 0;
}
/*THIS PROGRAM WILL COMPUTE FOR THE DETERMINANT OF THE GIVEN MATRIX */
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
int n,i,j,k;
cout.precision(4);
cout.setf(ios::fixed);
cout<<"\nEnter the order of the matrix(n): \n";
cin>>n;
float a[n][n];
double det=1;
int flag=0;
cout<<"\nEnter the elements of the matrix (row-wise):\n";
for (i=0;i<n;i++)
for (j=0;j<n;j++)
cin>>a[i][j];
for (i=0;i<n;i++)
for (k=i+1;k<n;k++)
if (abs(a[i][i])<abs(a[k][i])){
flag++;
for (j=0;j<n;j++){
double temp=a[i][j];
a[i][j]=a[k][j];
a[k][j]=temp;
}
}
cout<<"\nThe matrix after Pivotisation is:\n";
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
cout<<a[i][j]<<setw(16);
cout<<"\n";
}
for (i=0;i<n-1;i++)
for (k=i+1;k<n;k++)
{
double t=a[k][i]/a[i][i];
for (j=0;j<n;j++)
a[k][j]=a[k][j]-t*a[i][j];

}
cout<<"\n\nThe matrix after Gauss-elimination is as follows:\n";
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
cout<<a[i][j]<<setw(16);
cout<<"\n";
}
for(i=0;i<n;i++){
det=det*a[i][i];
}if (flag%2==0){
det=det;
}else{
det=-det;
}cout<<"\n\n The determinant of the given matrix is: "<<det;
return 0;
}
/* THIS PROGRAM WILL COMPUTE FOR THE INVERSE OF THE INPUTTED MATRIX*/
#include<iostream>

#include<conio.h>

using namespace std;


int main()

int i, j, k, n;

float a[10][10] = { 0 }, d;

cout << "ENTER THE DIMENSION OF THE MATRIX ";

cin >> n;

cout << "INPUT THE MATRIX(ROW -WISE) " << endl;

for (i = 1; i <= n; i++)

for (j = 1; j <= n; j++)

cin >> a[i][j];

for (i = 1; i <= n; i++)

for (j = 1; j <= 2 * n; j++)

if (j == (i + n))

a[i][j] = 1;

for (i = n; i > 1; i--)

if (a[i - 1][1] < a[i][1])

for (j = 1; j <= n * 2; j++)

{
d = a[i][j];

a[i][j] = a[i - 1][j];

a[i - 1][j] = d;

cout << "PIVOTED OUTPUT: " << endl;

for (i = 1; i <= n; i++)

for (j = 1; j <= n * 2; j++)

cout << a[i][j] << " ";

cout << endl;

for (i = 1; i <= n; i++)

for (j = 1; j <= n * 2; j++)

if (j != i)

d = a[j][i] / a[i][i];

for (k = 1; k <= n * 2; k++)

a[j][k] -= a[i][k] * d;

}
for (i = 1; i <= n; i++)

d = a[i][i];

for (j = 1; j <= n * 2; j++)

a[i][j] = a[i][j] / d;

cout << "THE INVERSE OF THE GIVEN MATRIX IS : " << endl;

for (i = 1; i <= n; i++)

for (j = n + 1; j <= n * 2; j++)

cout << a[i][j] << " ";

cout << endl;

getch();

return 0;

}
\

You might also like