how can I define the C-language \'s Struct and how can I make a source using the
ID: 3894824 • Letter: H
Question
how can I define the C-language 's Struct and how can I make a source using the struct and complex number of matrix that can do calculations ( plus, minus, multiple, devide) Screen -> ************************************************************** * this is N*N square calcultation program * *You can calcultate either float matrix and complex number matrix * ************************************************************** select the calculation type ------------------------------------------------- 1. Inverse matrix 2. Sum matrix of two matrix 3. subtraction of two matrix 4. multiplication of two matrix 0. exit ------------------------------------------------ select the menu: 2 ------------------------------------------------ input of matrix size (NxN) ----------------------------------------------- input: 1 ----------------------------------------------- choose the first matrix type : ----------------------------------------------- 1. float type 2. complex number type (a+jb) ----------------------------------------------- input: 2 input of first matrix's components(input the each of real, imaginery part) (1,1)==>11, 13 ----------------------------------------------------------- choose the second matrix type : ----------------------------------------------- 1. float type 2. complex number type (a+jb) ----------------------------------------------- input: 2 input of second matrix's components(input the each of real, imaginery part) (1,1)==>11, 14 ============================================================== ====================result of calculation============================= ============================================================== 22+j27 ============================================================== ************************************************************** * this is N*N square calcultation program * *You can calcultate either float matrix and complex number matrix * ************************************************************** select the calculation type ------------------------------------------------- 1. Inverse matrix 2. Sum matrix of two matrix 3. subtraction of two matrix 4. multiplication of two matrix 0. exit ------------------------------------------------ select the menu: 1 ------------------------------------------------ input of matrix size (NxN) ----------------------------------------------- input: 2 ----------------------------------------------- choose the first matrix type : ----------------------------------------------- 1. float type 2. complex number type (a+jb) ----------------------------------------------- input: 2 input of first matrix's components(input the each of real, imaginery part) (1,1)==>11 ,13 (1,2)==>11, 14 (2,1)==>22, 23 (2,2)==>33, 34 ============================================================== ==================result of calculation============================= ============================================================== 0.141057+j-0.135781 -0.0580381+j0.0452147 -0.0954154+j0.0905148 0.0539059+j-0.0452318 ============================================================== how can I define the C-language 's Struct and how can I make a source using the struct and complex number of matrix that can do calculations ( plus, minus, multiple, devide) Screen -> ************************************************************** * this is N*N square calcultation program * *You can calcultate either float matrix and complex number matrix * ************************************************************** select the calculation type ------------------------------------------------- 1. Inverse matrix 2. Sum matrix of two matrix 3. subtraction of two matrix 4. multiplication of two matrix 0. exit ------------------------------------------------ select the menu: 2 ------------------------------------------------ input of matrix size (NxN) ----------------------------------------------- input: 1 ----------------------------------------------- choose the first matrix type : ----------------------------------------------- 1. float type 2. complex number type (a+jb) ----------------------------------------------- input: 2 input of first matrix's components(input the each of real, imaginery part) (1,1)==>11, 13 ----------------------------------------------------------- choose the second matrix type : ----------------------------------------------- 1. float type 2. complex number type (a+jb) ----------------------------------------------- input: 2 input of second matrix's components(input the each of real, imaginery part) (1,1)==>11, 14 ============================================================== ====================result of calculation============================= ============================================================== 22+j27 ============================================================== ************************************************************** * this is N*N square calcultation program * *You can calcultate either float matrix and complex number matrix * ************************************************************** select the calculation type ------------------------------------------------- 1. Inverse matrix 2. Sum matrix of two matrix 3. subtraction of two matrix 4. multiplication of two matrix 0. exit ------------------------------------------------ select the menu: 1 ------------------------------------------------ input of matrix size (NxN) ----------------------------------------------- input: 2 ----------------------------------------------- choose the first matrix type : ----------------------------------------------- 1. float type 2. complex number type (a+jb) ----------------------------------------------- input: 2 input of first matrix's components(input the each of real, imaginery part) (1,1)==>11 ,13 (1,2)==>11, 14 (2,1)==>22, 23 (2,2)==>33, 34 ============================================================== ==================result of calculation============================= ============================================================== 0.141057+j-0.135781 -0.0580381+j0.0452147 -0.0954154+j0.0905148 0.0539059+j-0.0452318 ============================================================== how can I define the C-language 's Struct and how can I make a source using the struct and complex number of matrix that can do calculations ( plus, minus, multiple, devide) Screen -> ************************************************************** * this is N*N square calcultation program * *You can calcultate either float matrix and complex number matrix * ************************************************************** select the calculation type ------------------------------------------------- 1. Inverse matrix 2. Sum matrix of two matrix 3. subtraction of two matrix 4. multiplication of two matrix 0. exit ------------------------------------------------ select the menu: 2 ------------------------------------------------ input of matrix size (NxN) ----------------------------------------------- input: 1 ----------------------------------------------- choose the first matrix type : ----------------------------------------------- 1. float type 2. complex number type (a+jb) ----------------------------------------------- input: 2 input of first matrix's components(input the each of real, imaginery part) (1,1)==>11, 13 ----------------------------------------------------------- choose the second matrix type : ----------------------------------------------- 1. float type 2. complex number type (a+jb) ----------------------------------------------- input: 2 input of second matrix's components(input the each of real, imaginery part) (1,1)==>11, 14 ============================================================== ====================result of calculation============================= ============================================================== 22+j27 ============================================================== ************************************************************** * this is N*N square calcultation program * *You can calcultate either float matrix and complex number matrix * ************************************************************** select the calculation type ------------------------------------------------- 1. Inverse matrix 2. Sum matrix of two matrix 3. subtraction of two matrix 4. multiplication of two matrix 0. exit ------------------------------------------------ select the menu: 1 ------------------------------------------------ input of matrix size (NxN) ----------------------------------------------- input: 2 ----------------------------------------------- choose the first matrix type : ----------------------------------------------- 1. float type 2. complex number type (a+jb) ----------------------------------------------- input: 2 input of first matrix's components(input the each of real, imaginery part) (1,1)==>11 ,13 (1,2)==>11, 14 (2,1)==>22, 23 (2,2)==>33, 34 ============================================================== ==================result of calculation============================= ============================================================== 0.141057+j-0.135781 -0.0580381+j0.0452147 -0.0954154+j0.0905148 0.0539059+j-0.0452318 ==============================================================Explanation / Answer
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define MAXR 10
#define MAXC 10
struct complex
{
float real, img;
};
typedef struct complex Complex;
int OperationMenu()
{
int ch;
printf(" Select the calculation type: ");
printf(" -------------------------------------------------");
printf(" 1. Inverse matrix 2. Sum matrix of two matrix. ");
printf(" 3. Subtraction of two matrix 4. Multiplication of two matrix");
printf(" 0. Exit.");
printf(" ------------------------------------------------");
printf(" Select the menu: ");
printf(" ------------------------------------------------");
scanf("%d", &ch);
return ch;
}
int TypeMenu()
{
int ch;
printf(" Choose the first matrix type : ");
printf(" -----------------------------------------------");
printf(" 1. Float type.");
printf(" 2. complex number type (a + jb).");
printf(" -----------------------------------------------");
scanf("%d", &ch);
return ch;
}
void accetpFloat(float mat[][MAXC], int size)
{
int r, c;
for(r = 0; r < size; ++r)
{
for(c = 0; c < size; ++c)
{
printf("(%d, %d) ==> ", (r+1), (c+1));
scanf("%d", &mat[r][c]);
}
}
}
// For calculating Determinant of the Matrix
float determinant(float a[MAXR][MAXC], float k)
{
float s = 1, det = 0, b[MAXR][MAXC];
int i, j, m, n, c;
if (k == 1)
return (a[0][0]);
else
{
det = 0;
for (c = 0; c < k; c++)
{
m = 0;
n = 0;
for (i = 0;i < k; i++)
{
for (j = 0 ;j < k; j++)
{
b[i][j] = 0;
if (i != 0 && j != c)
{
b[m][n] = a[i][j];
if (n < (k - 2))
n++;
else
{
n = 0;
m++;
}
}
}
}
det = det + s * (a[0][c] * determinant(b, k - 1));
s = -1 * s;
}
}
return (det);
}
// Finding transpose of matrix
void transpose(float num[MAXR][MAXC], float fac[MAXR][MAXC], float r)
{
int i, j;
float b[25][25], inverse[25][25], d;
for (i = 0;i < r; i++)
for (j = 0;j < r; j++)
b[i][j] = fac[j][i];
d = determinant(num, r);
for (i = 0;i < r; i++)
for (j = 0;j < r; j++)
inverse[i][j] = b[i][j] / d;
printf(" The inverse of matrix is : ");
for (i = 0;i < r; i++)
{
for (j = 0;j < r; j++)
{
printf(" %f", inverse[i][j]);
}
printf(" ");
}
}
void cofactor(float num[][MAXC], float f)
{
float b[MAXR][MAXC], fac[MAXR][MAXC];
int p, q, m, n, i, j;
for (q = 0;q < f; q++)
{
for (p = 0;p < f; p++)
{
m = 0;
n = 0;
for (i = 0;i < f; i++)
{
for (j = 0;j < f; j++)
{
if (i != q && j != p)
{
b[m][n] = num[i][j];
if (n < (f - 2))
n++;
else
{
n = 0;
m++;
}
}
}
}
fac[q][p] = pow(-1, q + p) * determinant(b, f - 1);
}
}
transpose(num, fac, f);
}
void addFloat(float mat1[][MAXC], float mat2[][MAXC], float mat3[][MAXC], int size)
{
int r, c;
for(r = 0; r < size; r++)
for(c = 0; c < size; c++)
mat3[r][c] = mat1[r][c] + mat2[r][c];
}
void subFloat(float mat1[][MAXC], float mat2[][MAXC], float mat3[][MAXC], int size)
{
int r, c;
for(r = 0; r < size; r++)
for(c = 0; c < size; c++)
mat3[r][c] = mat1[r][c] - mat2[r][c];
}
void mulFloat(float mat1[][MAXC], float mat2[][MAXC], float mat3[][MAXC], int size)
{
int r, c, inn;
float sum;
for (r = 0; r != size; ++r)
{
for (c = 0; c != size; ++c)
{
sum = 0;
for (inn = 0; inn != size; ++inn)
{
sum += mat1[r][inn] * mat2[inn][c];
}
mat3[r][c] = sum;
}
}
}
void displayFloat(float mat[][MAXC], int size)
{
int r, c;
for(r = 0; r < size; r++)
for(c = 0; c < size; c++)
printf("%5d", mat[r][c]);
}
void accetpComplex(Complex mat[][MAXC], int size)
{
int r, c;
for(r = 0; r < size; ++r)
{
for(c = 0; c < size; ++c)
{
printf("(%d, %d) ==> ", (r+1), (c+1));
scanf("%d%d", &mat[r][c].real, &mat[r][c].img);
}
}
}
void addComplex(Complex a[MAXR][MAXC], Complex b[MAXR][MAXC], Complex p[MAXR][MAXC], int size)
{
int r, c;
for(r = 0; r < size; ++r)
for(c = 0; c < size; ++c)
{
p[r][c].real = a[r][c].real + b[r][c].real;
p[r][c].img = a[r][c].img + b[r][c].img;
}
}
void subComplex(Complex a[MAXR][MAXC], Complex b[MAXR][MAXC], Complex p[MAXR][MAXC], int size)
{
int r, c;
for(r = 0; r < size; ++r)
for(c = 0; c < size; ++c)
{
p[r][c].real = a[r][c].real - b[r][c].real;
p[r][c].img = a[r][c].img - b[r][c].img;
}
}
void mulComplex(Complex a[MAXR][MAXC], Complex b[MAXR][MAXC], Complex p[MAXR][MAXC], int size)
{
int i,j, n;
for(i=0;i<size;++i)
for( j=0;j<size;++j)
{
p[i][j].real=p[i][j].img=0;
for(n=0;n<size;++n)
{
p[i][j].real+=(a[i][n].real * b[n][j].real)-(a[i][n].img * b[n][j].img);
p[i][j].img+=(a[i][n].real * b[n][j].img)+(a[i][n].img * b[n][j].real);
}
}
}
void displayComplex(Complex a[MAXR][MAXC], int size)
{
int r, c;
for(r = 0; r < size; ++r)
{
for(c = 0; c < size; ++c)
printf("%6.2f +i(%6.2f)", a[r][c].real, a[r][c].img);
printf(" ");
}
}
int main()
{
Complex comMat1[MAXR][MAXC], comMat2[MAXR][MAXC], comMat3[MAXR][MAXC];
float floatMat1[MAXR][MAXC], floatMat2[MAXR][MAXC], floatMat3[MAXR][MAXC];
int size;
float d;
do
{
int op = OperationMenu();
if(op != 0)
{
printf(" Input of matrix size (N x N): ");
printf(" -----------------------------------------------");
scanf("%d", &size);
printf(" -----------------------------------------------");
}
int type = TypeMenu();
if(type == 1)
{
switch(op)
{
case 0:
exit(0);
case 1:
printf(" Input of first matrix's components.");
accetpFloat(floatMat1, size);
d = determinant(floatMat1, size);
if (d == 0)
printf(" Inverse of Entered Matrix is not possible ");
else
cofactor(floatMat1, size);
break;
case 2:
printf(" Input of first matrix's components.");
accetpFloat(floatMat1, size);
printf(" Input of second matrix's components.");
accetpFloat(floatMat2, size);
addFloat(floatMat1, floatMat2, floatMat3, size);
printf(" First Matrix: ");
displayFloat(floatMat1, size);
printf(" Second Matrix: ");
displayFloat(floatMat2, size);
printf(" Third Matrix: ");
displayFloat(floatMat3, size);
break;
case 3:
printf(" Input of first matrix's components.");
accetpFloat(floatMat1, size);
printf(" Input of second matrix's components.");
accetpFloat(floatMat2, size);
subFloat(floatMat1, floatMat2, floatMat3, size);
printf(" First Matrix: ");
displayFloat(floatMat1, size);
printf(" Second Matrix: ");
displayFloat(floatMat2, size);
printf(" Third Matrix: ");
displayFloat(floatMat3, size);
break;
case 4:
printf(" Input of first matrix's components.");
accetpFloat(floatMat1, size);
printf(" Input of second matrix's components.");
accetpFloat(floatMat2, size);
mulFloat(floatMat1, floatMat2, floatMat3, size);
printf(" First Matrix: ");
displayFloat(floatMat1, size);
printf(" Second Matrix: ");
displayFloat(floatMat2, size);
printf(" Third Matrix: ");
displayFloat(floatMat3, size);
break;
default:
printf(" Invalid Choice. Try again.");
}
}
else if(type == 2)
{
switch(op)
{
case 0:
exit(0);
case 1:
printf(" Input of first matrix's components(input the each of real, imaginary part.)");
accetpComplex(comMat1, size);
/*d = determinant(floatMat1, size);
if (d == 0)
printf(" Inverse of Entered Matrix is not possible ");
else
cofactor(floatMat1, size);*/
break;
case 2:
printf(" Input of first matrix's components(input the each of real, imaginary part.)");
accetpComplex(comMat1, size);
printf(" Input of second matrix's components(input the each of real, imaginary part.)");
accetpComplex(comMat2, size);
addComplex(comMat1, comMat2, comMat3, size);
printf(" First Matrix: ");
displayComplex(comMat1, size);
printf(" Second Matrix: ");
displayComplex(comMat2, size);
printf(" Third Matrix: ");
displayComplex(comMat3, size);
break;
case 3:
printf(" Input of first matrix's components(input the each of real, imaginary part.)");
accetpComplex(comMat1, size);
printf(" Input of second matrix's components(input the each of real, imaginary part.)");
accetpComplex(comMat2, size);
subComplex(comMat1, comMat2, comMat3, size);
printf(" First Matrix: ");
displayComplex(comMat1, size);
printf(" Second Matrix: ");
displayComplex(comMat2, size);
printf(" Third Matrix: ");
displayComplex(comMat3, size);
break;
case 4:
printf(" Input of first matrix's components(input the each of real, imaginary part.)");
accetpComplex(comMat1, size);
printf(" Input of second matrix's components(input the each of real, imaginary part.)");
accetpComplex(comMat2, size);
mulComplex(comMat1, comMat2, comMat3, size);
printf(" First Matrix: ");
displayComplex(comMat1, size);
printf(" Second Matrix: ");
displayComplex(comMat2, size);
printf(" Third Matrix: ");
displayComplex(comMat3, size);
break;
default:
printf(" Invalid Choice. Try again.");
}
}
else
printf(" Invalid Choice.");
}while(1);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.