Surface area of a tetrahedron
#include <stdio.h>
#include <math.h>
typedef struct {
float x;
float y;
float z;
} point;
typedef struct {
point ver1;
point ver2;
point ver3;
point ver4;
} tetrahedron;
typedef struct {
point v1;
point v2;
point v3;
} triangle;
point Diff(point a, point b) {
point c;
c.x = a.x - b.x;
c.y = a.y - b.y;
c.z = a.z - b.z;
return(c);
}
point Cross(point a, point b) {
point c;
c.x = a.y*b.z - a.z*b.y;
c.y = a.z*b.x - a.x*b.z;
c.z = a.x*b.y - a.y*b.x;
return(c);
}
float Length(point a) {
return(sqrt(a.x*a.x + a.y*a.y + a.z*a.z));
}
float Area(triangle t) {
point a, b, c;
a = Diff(t.v3,t.v1);
b = Diff(t.v2,t.v1);
c = Cross(a,b);
return(Length(c)/2);
}
triangle Side(tetrahedron T,int i){
triangle t;
switch (i) {
case 1: t.v1 = T.ver2;
t.v2 = T.ver3;
t.v3 = T.ver4;
break;
case 2: t.v1 = T.ver1;
t.v2 = T.ver3;
t.v3 = T.ver4;
break;
case 3: t.v1 = T.ver1;
t.v2 = T.ver2;
t.v3 = T.ver4;
break;
case 4: t.v1 = T.ver1;
t.v2 = T.ver2;
t.v3 = T.ver3;
break;
}
return(t);
}
main(){
tetrahedron T;
float area;
int i;
printf("The coordinates of the first point are: ");
scanf("%f %f %f", &T.ver1.x, &T.ver1.y, &T.ver1.z);
printf("The coordinates of the second point are: ");
scanf("%f %f %f", &T.ver2.x, &T.ver2.y, &T.ver2.z);
printf("The coordinates of the third point are: ");
scanf("%f %f %f", &T.ver3.x, &T.ver3.y, &T.ver3.z);
printf("The coordinates of the fourth point are: ");
scanf("%f %f %f", &T.ver4.x, &T.ver4.y, &T.ver4.z);
area = 0;
for (i = 1; i <= 4 ; i++) {
printf("The area of the %d-th side of T is equal to %f\n", i, Area(Side(T,i)));
area = area + Area(Side(T,i));
}
printf("The surface area of the tetrahedron T is equal to %f\n", area);
}
Determinants using cofactor expansion
#include <stdio.h>
#define dim 20
typedef struct {
float coeff[dim][dim];
int size;
} matrix;
matrix cofactor( matrix A, int k) {
matrix B;
int i, j ;
B.size = A.size - 1;
for (j = 0 ; j < B.size ; j++ )
for (i = 0 ; i < B.size ; i++ ){
if ( i < k )
B.coeff[j][i] = A.coeff[j+1][i];
else
B.coeff[j][i] = A.coeff[j+1][i+1];
}
return(B);
}
float Det( matrix A ){
matrix B;
float det;
int i;
det = 0;
if (A.size == 1)
det = A.coeff[0][0];
else
for (i = 0 ; i < A.size ; i++ ){
B = cofactor(A,i);
if (i%2 == 0)
det = det + A.coeff[0][i] * Det(B);
else
det = det - A.coeff[0][i] * Det(B);
}
return(det);
}
int main(){
matrix A;
int i, j;
for ( i = 0 ; i < dim ; i++ ){
for ( j =0 ; j < dim ; j++ )
A.coeff[i][j] = 0;
}
printf("The size of the determinant is: ");
scanf("%d",&A.size);
for ( i = 0 ; i < A.size ; i++ ){
printf("Enter the %dth row of the matrix: ",i+1);
for ( j =0 ; j < A.size ; j++ )
scanf("%f",&A.coeff[i][j]);
printf("\n");
}
printf("The determinant is %f\n",Det(A));
}