Professional Documents
Culture Documents
#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
}
{
printf("\n\n
}
getch();
return 0;
}
Sample no 1.
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);
if ( n != p )
printf("\nMatrices with entered orders can't be multiplied with each other.\n");
else
{
printf("\n\nElements of Matrix A:\n");
multiply[c][d] = sum;
sum = 0;
}
}
printf("\n\nPRODUCT OF MATRICES:\n\n");
printf("\n");
}
}
getch();
return 0;
}
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++){
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;
");
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];
}
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;
}
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
";
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
{
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++)
{
}
//********** 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
BRYAN U. GALACIO
091-0409