Professional Documents
Culture Documents
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];
}
return 0;
}
/* THIS PROGRAM WILL COMPUTE FOR THE SUM OF TWO DIFFERENT MATRIX GIVEN
THAT THEY HAVE THE SAME DIMENSION */
#include<iostream>
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";
return 0;
}
/* THIS PROGRAM WILL COMPUTE FOR THE DIFFERENCE OF TWO DIFFERENT
MATRIX GIVEN THAT THEY HAVE THE SAME DIMENSION */
#include<iostream>
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";
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>
int i, j, k, n;
float a[10][10] = { 0 }, d;
cin >> n;
if (j == (i + n))
a[i][j] = 1;
{
d = a[i][j];
a[i - 1][j] = d;
if (j != i)
d = a[j][i] / a[i][i];
a[j][k] -= a[i][k] * d;
}
for (i = 1; i <= n; i++)
d = a[i][i];
a[i][j] = a[i][j] / d;
cout << "THE INVERSE OF THE GIVEN MATRIX IS : " << endl;
getch();
return 0;
}
\