You are on page 1of 29

MATRIX ADDITION

#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<math.h>
main()
{
int m, n, c, d, first[25][25], second[25][25], sum[25][25];
cout << "\n\n\n\n
*ADDITION OF MATRICES*\n\n";
printf("Size of the Matrix:\n");
scanf("%d%d",&m,&n);
cout << "\n\n
_-=.. Elements of first matrix ..=-_\n\n";
printf("Matrix A:\n");
for ( c = 0 ; c <m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
{
printf(" A[%d,%d]:\t",c+1,d+1);
scanf("%d",&first[c][d]);
}
}
cout << "\n
_-=.. Elements of second matrix ..=-_\n\n";
{
printf("Matrix B:\n");
for ( c = 0 ; c <m ;c++ )
{
for ( d = 0 ; d < n ; d++ )
{
printf(" B[%d,%d]:\t",c+1,d+1);
scanf("%d",&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 << "\n\n

_-=.. Sum of entered matrices .. =-_\n\n";

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


{
for ( d = 0 ; d <n ; d++ )
printf(" %d\t",sum[c][d]);
cout << endl;

}
{
printf("\n\n
}
getch();
return 0;
}

INPUT and OUTPUT:

Sample no 1.

<Press Any Key to Exit ...>");

Sample No. 2

MATRIX MULTILICATION
#include <stdio.h>
#include<conio.h>

int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[50][50], second[50][50], multiply[50][50];
printf("\n\t_-=MATRIX MULTIPLICATION=-_\n\n\n");
printf("Number of rows and columns of Matrix A:\n");
scanf("%d%d", &m, &n);

printf("\n\nNumber of rows and columns of Matrix B\n");


scanf("%d%d", &p, &q);

if ( n != p )
printf("\nMatrices with entered orders can't be multiplied with each other.\n");
else
{
printf("\n\nElements of Matrix A:\n");

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


for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);

printf("\n\nElements of Matrix B:\n");

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


for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);

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


{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

printf("\n\nPRODUCT OF MATRICES:\n\n");

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


{
for ( d = 0 ; d < q ; d++ )
printf("%d\t\a", multiply[c][d]);

printf("\n");
}
}

getch();
return 0;
}

INPUT AND OUTPUT:

Sample No. 1

Sample no. 2

INVERSE OF MATRIX
#include<stdio.h>
#include<conio.h>
int main()
{
float matrix[50][50], ratio,a;
int i,j,k, n,m;
printf("\n _-=INVERSE OF MATRIX=-_\n\n");
printf("\n\nEnter the Dimension of matrix:\n");
scanf("%d%d", &n,&m);
printf("\n Your Matrix is %d x %d\n\n",n,m);
printf("\nEnter the elements of matrix: \n");
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++){
scanf("%f", &matrix[i][j]);
}
}
for(i = 0; i < n; i++){
for(j = n; j < 2*n; j++)
{
if(i==(j-n))
matrix[i][j] = 1.0;
else
matrix[i][j] = 0.0;
}
}
for(i = 0; i < n; i++){

for(j = 0; j < n; j++){


if(i!=j)
{
ratio = matrix[j][i]/matrix[i][i];
for(k = 0; k < 2*n; k++)
{
matrix[j][k] -= ratio * matrix[i][k];
}
}
}
}
for(i = 0; i < n; i++){
a = matrix[i][i];
for(j = 0; j < 2*n; j++){
matrix[i][j] /= a;
}
}
printf("\n\nThe inverse matrix is: \n");
for(i = 0; i < n; i++){
for(j = n; j < 2*n; j++){
printf("%0.2f", matrix[i][j]);
printf("\a\t ");
}
printf("\n");
}
getch();
return 0;
}

INPUT and OUTPUT:


Sample no. 1:

Sample no 2.

PLANE FIGURES
#include<stdio.h>
#include<conio.h>

float B,H,Area,x,y,rad;
char fig;
main()
{
printf("Select the shape :\n\n");
printf(" 1 for Square\n");
printf(" 2 for Rectangle\n");
printf(" 3 for Trapezoid\n");
printf(" 4 for Circle\n");
printf(" 5 for Triangle\n\n\n");
scanf("\n%c",&fig);
switch(fig)
{
{
case '1':
printf("\n\nSide of square: ");
scanf("%f",&x);
Area = x*x;
printf("\n\n\aAREA OF THE SQUARE =\t%0.4f",Area);
break;
}

case '2':
{
printf("\n\nLength of the Rectangle: ");
scanf("%f",&x);
printf("\nWidth of the rectangle: ");
scanf("%f",&y);
Area = x*y;
printf("\a\nArea of the rectangle = %0.4f",Area);
break;
}

case '3':
{
printf("\n\nEnter Base 1: ");
scanf("%f",&x);
printf("Enter Base 2: ");
scanf("%f",&y);
printf("Enter Height: ");
scanf("%f",&H);
Area = (x+y)*H*0.5;
printf("\n\n\aArea of the Trapezoid = %0.4f",Area);
break;
}
case '4':
{
printf("\n\nEnter Radius of the Circle:
scanf("%f",&rad);
Area = rad*rad*3.141592654;

");

printf("\n\n\aArea of the Circle = %0.4f", Area);


break;
}
case '5':
{
printf(" \n\nBase of the Triangle: ");
scanf("%f",&B);
printf(" \n\nHeight of the Triangle: ");
scanf("%f",&H);
Area = B*H*0.5;
printf("\n\n\aArea of the Triangle = %0.4f",Area);
break;
}
default:
{
printf("\n\n\a\a\a\a\aINVALID KEYWORD!");
break;
}
}
getch();
return(0);
}

INPUT and OUTPUT


SQUARE

RECTANGLE

TRAPEZOID

CIRCLE

TRIANGLE

GAUSS ELIMINATION
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,i,j,k,temp,x;
float a[25][25],c,d[25]={0};
printf("_-=. .GAUSS ELIMINATION. .=-_\n");
printf("\nNumber of equations(rows) : ");
scanf("%d",&n) ;
printf("\nCo-efficients of the equations :\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("a(%d,%d) = ",i+1,j+1);
scanf("%f",&a[i][j]);
}
printf("d%d = ",i+1);
scanf("%f",&a[i][n]);
}
for(i=n-1;i>0;i--)

// partial pivoting

{
if(a[i-1][0]<a[i][0])
for(j=0;j<=n;j++)

{
c=a[i][j];
a[i][j]=a[i-1][j];
a[i-1][j]=c;
}
}
//*************** DISPLAY MATRIX*************//
printf("\nAUGMENTED MATRIX\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<=n;j++)
printf("%6.1f",a[i][j]);
printf("\n");
}
//********* changing to upper triangular matrix*************//
//********* Forward elimination process**************//
for(k=0;k<n-1;k++)
for(i=k;i<n-1;i++)
{
c= (a[i+1][k]/a[k][k]) ;

for(j=0;j<=n;j++)
a[i+1][j]-=c*a[k][j];
}

// ************DISPLAYING UPPER TRIANGULAE MATRIX***************//


printf("\nUPPER TRIANGLE\n");
for(i=0;i<n;i++)

for(i=0;i<n;i++)
{
for(j=0;j<=n;j++)

printf("%6.2f",a[i][j]);

printf("\n");
}
//***************** Backward Substitution method****************//

for(i=n-1;i>=0;i--)
{
c=0;
for(j=i;j<=n-1;j++)
c=c+a[i][j]*d[j];

d[i]=(a[i][n]-c)/a[i][i];
}
//******** RESULT DISPLAY *********//
for(i=0;i<n;i++)
printf("\nX%d = %0.2f",i+1,d[i]);
cout<<d[i]<<endl;

getch();
return 0;
}

INPUT and OUTPUT

QUADRATIC EQUATION
#include <stdio.h>
#include <iostream.h>
#include <math.h>
#include<conio.h>
// function to evaluate the determinant
double determinant(double arg_a, double arg_b, double arg_c) {
return (arg_b * arg_b) - (4 * arg_a * arg_c);
}
// function to evaluate real roots
void realRoots(double arg_a, double arg_b, double arg_sqrt) {
double firstRoot =
(-arg_b/(2 * arg_a)) +
(arg_sqrt/(2 * arg_a));
double secondRoot = (-arg_b/(2 * arg_a)) (arg_sqrt/(2 * arg_a));
cout << "\nFirst Real Root: \t" << firstRoot << "\n";
cout << "Second Real Root: \t" << secondRoot << "\n";
} // end realRoots
void output1(double first, double second) {
cout << "\nFirst Imaginary Root: \t\n" << first << " + " << second << "i" << "\n";
cout << "\n\nSecond Imaginary Root: \t\n" << first << " - " << second << "i" <<
"\n";
} // end output1()
void output2(double first, double second) {
second = -(second); // change the sign of the second term, for correct output
cout << "\nFirst Imaginary Root: \t" << first << " - " << second << "i" << "\n";
cout << "\nSecond Imaginary Root: \t" << first << " + " << second << "i" << "\n";
} // end output2()
// evaluate imaginary roots
void imaginaryRoots(double arg_a, double arg_b, double arg_imag_sqrt) {
double two_a = 2 * arg_a;
double first_term = (-arg_b)/two_a;
double second_term = (arg_imag_sqrt)/two_a;
if(second_term >= 0) {
//cout << "\nsecond_term is greater than or eaqual to 0: " <<
second_term << "\n";
output1(first_term, second_term);
} // end if
else {
//cout << "\nsecond_term is less than or eaqual to 0: " <<
second_term << "\n";
output2(first_term, second_term);

} // end else
} // end imaginaryRoots
// main function
int main()
{
cout << "\n

_-=. .Quadratic equation in standard form. .=-_ \n\n\n\n

";

// declare variables a, b, and c


double a, b, c;
char n;
do {
cout << "\nEnter the value of 'a': ";
cin >> a;
cout << "\nEnter the value of 'b': ";
cin >> b;
cout << "\nEnter the value of 'c': ";
cin >> c;
double det = determinant(a, b, c);
if (det >= 0) {
realRoots(a, b, sqrt(det)); // roots are real
} // end if
else {
imaginaryRoots(a, b, sqrt(-det)); // roots are imaginary
} // end else
cout << "\nSolve another one?";
cout << "\nEnter 'y' for 'Yes' -- 'n' for 'No': ";
cin >> n;
cout << "\n";
} while (n == 'y'); // end do-while loop
getch();
return 0;
}

INPUT and OUTPUT

CHOLESKY METHOD
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main()
{
int n,i,k,j,p;
float a[10][10],l[10][10]={0},u[10][10]={0},sum,b[10],z[10]={0},x[10]={0};

cout<<"\n

_-=. .CHOLESKY METHOD. .=-_ \n"<<endl;

cout<<"Enter the elements in the equations(rows): ";


cin>>n;
cout<<endl<<"Elements of Matrix (from equation): ";
for(i=1;i<=n;i++)
{
cout<<"\n\nRow "<<i<<endl;
for(j=1;j<=n;j++)
cin>>a[i][j];
}
cout<<"\n\nElements of b matrix(ANSWERS)"<<endl;
for(i=1;i<=n;i++)
cin>>b[i];
//********** LU decomposition *****//
for(k=1;k<=n;k++)
{
u[k][k]=1;
for(i=k;i<=n;i++)

{
sum=0;
for(p=1;p<=k-1;p++)
sum+=l[i][p]*u[p][k];
l[i][k]=a[i][k]-sum;
}

for(j=k+1;j<=n;j++)
{
sum=0;
for(p=1;p<=k-1;p++)
sum+=l[k][p]*u[p][j];
u[k][j]=(a[k][j]-sum)/l[k][k];
}
}
//******** Displaying L matrix**********//
cout<<endl<<endl<<" L MATRIX "<<endl<<endl;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
cout<<l[i][j]<<"\t";
cout<<endl;
}
//******** Displaying U matrix**********//
cout<<endl<<endl<<" U MATRIX "<<endl<<endl;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)

cout<<u[i][j]<<"\t";
cout<<endl;
}
//***** FINDING b'; Lb'= b *********//

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

//forward subtitution method


sum=0;
for(p=1;p<i;p++)
sum+=l[i][p]*z[p];
z[i]=(b[i]-sum)/l[i][i];

}
//********** FINDING X; Ux = b ***********//

for(i=n;i>0;i--)
{
sum=0;
for(p=n;p>i;p--)
sum+=u[i][p]*x[p];
x[i]=(z[i]-sum)/u[i][i];
}
//*********** DISPLAYING SOLUTION**************//
cout<<endl<<endl<<"_-=. .Values for X. .=-_ \n"<<endl<<endl;
for(i=1;i<=n;i++)
printf("X[%d]= %4.2f\n",i+1-1,x[i]);
{
printf("\n <Press Any Key to Exit ...>");
}

getch();
return 0;
}
INPUT and OUTPUT
Sample no. 1

Sample no 2.

CE 520
Computer Aided Analysis

Compilation of Computer Program


(C++ Programs)
March 13, 2014

BRYAN U. GALACIO
091-0409

ENGR. LEONARDO V.SURIO,M.ENGG


Professor

You might also like