Skip to main content

Posts

To check matrix commutation between two matrices

/* Matrix Commutation test */ #include <stdio.h> #include <math.h> #include <stdlib.h> int main() { int i,j,n,m,p,k,a[20][20],b[20][20],e[20][20],g[20][20], flag=1; printf( "Enter the order of the matrix A:\n" ); scanf( "%d%d" , &m ,&n); printf( "Enter the order of the matrix B:\n" ); scanf( "%d%d" , &n, &p); printf( "Enter the elements of the matrix A:\n" ); for (i=0;i<m;i++) { for (j=0;j<n;j++) scanf( "%d" , &a[i][j]); printf( "\n" ); } printf( "Enter the elements of the matrix B:\n" ); for (i=0;i<n;i++) { for (j=0;j<p;j++) scanf( "%d" , &b[i][j]); printf( "\n" ); } /*matrix multiplication AB */ for (i=0;i<m;i++) { for (j=0;j<p;j++) { e[i][j]=0; for (k=0;k<n;k++) e[i][j]=e[i][j]+a[i][k]*b[k][j]; } } printf( "Product matrix AB...

Integration of a function by Monte carlo random dot method

/* Monte carlo random dot method */ #include <stdio.h> #include <stdlib.h> #include <math.h> #define a 1 #define b 2 #define cycle 5000 int main() { int counter=0, r1, r2, ymax, i, y; float f, A; ymax=3*2*2; for (i=0;i<cycle;i++) { r1=a+rand()%ymax; y=3*r1*r2; r2=rand()%ymax; if (y>=r2) counter++; } f=( float )counter/( float )cycle; A=f*( float )ymax*(( float )b-( float )a); printf( "The required integration is %f" , A); return (0); }

To find the area of a unit circle by Monte Carlo random dot method

/* To find the area of a unit circle by Monte Carlo random dot method */ #include <stdio.h> #include <math.h> int main() { float x, y, r, sqr, ratio; float xmax=2, ymax=2, h=0.0005, k=0.0005, area, rad=1; float cnt_tot=0, cnt_acc=0, cnt_rej=0; for (x=0;x<=ymax;x+=h) { for (y=0;y<=ymax;y+=k) { sqr=(x-1)*(x-1)+(y-1)*(y-1); r=sqrt(sqr); if (r<=rad) { cnt_acc+=1; } else { cnt_rej+=1; } } } cnt_tot=cnt_acc+cnt_rej; printf( "Total random number of dots generated=%f" , cnt_tot); ratio=cnt_acc/cnt_tot; printf( "\nRatio=%f" , ratio); area=ratio*ymax*xmax; printf( "\nTotal area=%f" , ymax*xmax); printf( "\nArea of the circle = (Ratio) X (Total area)" ); printf( "\nArea of the circle = %f" ,area); return (0); }

To Determine Multiplication of two matices

/* Multiplication of two matices */ #include <stdio.h> #include <math.h> int main() { int i, j, n, m, p, k, a[20] [20], b[20] [20], e[20] [20]; printf( "Enter order of the matrix A(mxn):\n" ); scanf( "%d%d" , &m, &n); printf( "Enter order of the matrix B(nxp):\n" ); scanf( "%d%d" , &n, &p); printf( "Enter the elements of matrix A:\n" ); for (i=0;i<m;i++) { for (j=0;j<n;j++) scanf( "%d" , &a[i] [j]); printf( "\n" ); }     printf( "Entered Matrix A:\n" );     for (i=0;i<m;i++) { for (j=0;j<n;j++) printf( "%10d" ,a[i] [j]); printf( "\n" ); } printf( "\nEnter the elements of matrix B:\n" ); for (i=0;i<n;i++) {     for (j=0;j<p;j++) scanf( "%d" , &b[i] [j]); printf( "\n" ); } printf( "Entered Matrix B:\n" );     for (i=0;i...

To determine Trace of a matrix

/* Trace of a matrix */ #include <stdio.h> #include <math.h> int main() { int i, j, n, m,  a[20] [20], sum=0; printf( "Enter the order of matrix (mxn):\n" ); scanf( "%d%d" , &m, &n); printf( "Enter the elements of the matrix:\n" ); for (i=0;i<m;i++) { for (j=0;j<n;j++)        { scanf( "%d" , &a[i] [j]);        } } printf( "your matrix:\n" ); for (i=0;i<m;i++) {     for (j=0;j<n;j++)     { printf( "%20d" ,a[i][j]); } printf( "\n" ); } for (i=0;i<m;i++) { for (j=0;j<n;j++) { if (i==j) sum=sum+a[i][j];         }     }     printf( "\n\ntrace of the matrix=%d\n" ,sum); return (0); }

To Determine Transpose of a given matrix

/* Transpose of a matrix */ #include <stdio.h> #include <math.h> int main() { int matrix[10][10], transpose[10][10]; int m, n, c, r; printf( "Enter the number of rows and columns of matrix:\n" ); scanf( "%d%d" , &m, &n); printf( "Enter the %d elements of matrix:\n" , m*n); for (r=0;r<m;r++) { for (c=0;c<n;c++) { printf( "\nEnter value in %dth row and %dth column:" , r+1, c+1); scanf( "%d" , &matrix[r][c]); } } printf( "your matrix:\n" ); for (r=0;r<m;r++) { for (c=0;c<n;c++) { printf( "%10d" , matrix[r][c]); } printf( "\n\n" ); } for (r=0;r<m;r++) { for (c=0;c<n;c++) { transpose[c][r]=matrix[r][c]; } } printf( "Transpose of entered matrix :\n" ); for (r=0;r<n;r++) { for (c=0;c<m;c++) { printf( "%10d" , transpose[r][c]); } printf( ...

To Perform Addition and subtraction of two matrices

/* Addition and subtraction of two matrices */ #include <stdio.h> #include <math.h> int main() { int i, j, n, m, a[20] [20], b[20] [20], c[20] [20], d[20] [20]; printf( "Enter order of the matrices:\n" ); scanf( "%d%d" , &m, &n); printf( "Enter the elements of matrix A:\n" ); for (i=0;i<m;i++) { for (j=0;j<n;j++) scanf( "%d" , &a[i] [j]); printf( "\n" ); }     printf( "Entered Matrix A:\n" );     for (i=0;i<m;i++) { for (j=0;j<n;j++) printf( "%10d" ,a[i] [j]); printf( "\n" ); } printf( "\nEnter the elements of matrix B:\n" ); for (i=0;i<m;i++) {     for (j=0;j<n;j++) scanf( "%d" , &b[i] [j]); printf( "\n" ); } printf( "Entered Matrix B:\n" );     for (i=0;i<m;i++) { for (j=0;j<n;j++) printf( "%10d" ,b[i] [j]); printf( ...