c - 数学矩阵程序

标签 c math matrix logic codeblocks

我目前正在创建一个矩阵程序,它可以使用我们的输入显示矩阵、矩阵的轨迹、从原始矩阵创建 2x2 矩阵的能力、矩阵的行列式、子矩阵,并且能够删除矩阵中的行和列,这是我的代码:

#include <stdio.h>

struct matrix
{
    char name;
    int mValues[10][10];
    int nrows;
    int ncols;
};
struct matrix2
{
    char name2;
    int mValues2[10][10];
    int nrows2;
    int ncols2;
};
int main()
{
    struct matrix M1, M2;
    int trace;
    int determinant;

        matrixInput(&M1);
        matrixDisplay(M1);
        matrixTrace(M1, &trace);
        printf("\nThe trace of matrix is %d",trace);

    return 0;
}


void matrixInput(struct matrix *mat)
{
    printf("Please enter a one character name for the matrix, e.g A, B, etc:");
        scanf("%c",&(*mat).name);

    printf("\nEnter # rows of the matrix(<10):\n");
        scanf("%d",&(*mat).nrows);

    printf("Enter # columns of the matrix(<10):\n");
        scanf("%d",&(*mat).ncols);

    printf("\nMatrix %c:\n",(*mat).name);

    FILE *file = fopen("matrix.txt","r");

    for(int x =0; x < (*mat).nrows; x++) {
        for(int y=0; y < (*mat).ncols; y++) {
            fscanf(file, "%d", &(*mat).mValues[x][y]);
        }
    }
    fclose(file);

}

void matrixDisplay(struct matrix mat)
{
    int baris, kolom;

    for(baris = 0; baris < mat.nrows; baris++) {
        printf("Row %d: ", baris);
        for(kolom = 0; kolom < mat.ncols; kolom++) {
            printf("\t%d", mat.mValues[baris][kolom]);
        }
        printf("\n");
    }
    getchar();
    return;
}

void matrixTrace(struct matrix mat, int *trace)
{
    int baris, kolom;
    *trace = 0;
    for(baris = 0; baris < mat.nrows; baris++){
        for(kolom = 0; kolom < mat.ncols; kolom++){
        if(baris == kolom){
    *trace += mat.mValues[baris][kolom];}


    }
    }
}
   void matrixInput2(struct matrix2 *mat2)
{
    printf("Please enter a one character name for the matrix, e.g A, B, etc:");
        scanf("%c",&(*mat2).name2);

    printf("\nEnter row number to start 2x2 matrix, number needs to be between 0 and 2:\n");
        scanf("%d",&(*mat2).nrows2);

    printf("Enter column number to start 2x2 matrix, number needs to be between 0 and 2:\n");
        scanf("%d",&(*mat2).ncols2);
        printf("\nMatrixInput2 %c:\n",(*mat2).name2);
}

这是输出的示例:

Please enter a one character name for the matrix, e.g A, B, etc: A

    Enter # rows of the matrix(<10):
    4
    Enter # columns of the matrix(<10):
    4

    Matrix A:
    Row 0:  1      -15     20     -40
    Row 1:  41     -60     75     -99
    Row 2:  100    -150    2      -14
    Row 3:  21     -39     42     -59

    The trace of matrix is =116

所以矩阵在.txt文件中被调用,我刚刚完成了矩阵显示和迹线的创建,现在我想从原始矩阵创建2x2矩阵,但我无法再次输入?程序在输出后结束,我尝试给它 void 函数但仍然不起作用,任何人都可以帮忙吗?

最佳答案

现在我解决了我的问题,这是更新的代码:

#include <stdio.h>

struct matrix
{
    char name;
    int mValues[10][10];
    int nrows;
    int ncols;
};
struct matrix2
{
    char name2;
    int mValues2[10][10];
    int nrows2;
    int ncols2;
};
int main()
{
    struct matrix m1, *m2;
    int trace;
    int determinant;

        matrixInput(&m1);
        matrixDisplay(m1);
        matrixTrace(m1, &trace);
        printf("\nThe trace of matrix is %d",trace);
        matrixDeterminant(*m2, &determinant);

    return 0;
}


void matrixInput(struct matrix *mat, struct matrix2 *mat2)
{
    printf("Please enter a one character name for the matrix, e.g A, B, etc:");
        scanf("%c",&(*mat).name);

    printf("\nEnter # rows of the matrix(<10):\n");
        scanf("%d",&(*mat).nrows);

    printf("Enter # columns of the matrix(<10):\n");
        scanf("%d",&(*mat).ncols);

    printf("\nMatrix %c:\n",(*mat).name);

    FILE *file = fopen("matrix.txt","r");

    for(int x =0; x < (*mat).nrows; x++) {
        for(int y=0; y < (*mat).ncols; y++) {
            fscanf(file, "%d", &(*mat).mValues[x][y]);
        }
    }
    fclose(file);

}

void matrixDeterminant(struct matrix m1, struct matrix2 *m2,struct matrix2 *mat2, int *determinant)
{

    printf("\n\nFinding the determinant now!");
    printf("\nPlease enter a one character name for the matrix, e.g A, B, etc:");
        scanf("%c",&(*m2).name2);

    printf("\nEnter row number where to start 2x2 matrix, number needs to be between 0 and 2:\n");
        scanf("%d",&(*m2).nrows2);

    printf("Enter column number where to start 2x2 matrix, number needs to be between 0 and 2:\n");
        scanf("%d",&(*m2).ncols2);

    }

void matrixDisplay(struct matrix mat)
{
    int baris, kolom;
    for(baris = 0; baris < mat.nrows; baris++) {
        printf("Row %d: ", baris);
        for(kolom = 0; kolom < mat.ncols; kolom++) {
            printf("\t%d", mat.mValues[baris][kolom]);
        }
        printf("\n");

    }
    getchar();
    return;
}

void matrixTrace(struct matrix mat, int *trace)
{
    int baris, kolom;
    *trace = 0;
    for(baris = 0; baris < mat.nrows; baris++){
        for(kolom = 0; kolom < mat.ncols; kolom++){
        if(baris == kolom){
    *trace += mat.mValues[baris][kolom];}

    }

}
}

另一件事是我想从原始矩阵创建一个2x2矩阵并找到矩阵的行列式,该矩阵是从我创建的txt文件中调用的,我很困惑,接下来我应该输入什么?

我希望输出是这样的:

Please enter a one character name for the matrix, e.g A, B, etc: A

Enter # rows of the matrix(<10):
4
Enter # columns of the matrix(<10):
4

Matrix A:
Row 0:  1      -15     20     -40
Row 1:  41     -60     75     -99
Row 2:  100    -150    2      -14
Row 3:  21     -39     42     -59

The trace of matrix is =116

Finding the determinant now!
Please enter a one character name for the matrix, e.g A, B, etc: B
Enter row number where to start 2x2 matrix, number needs to be between 0 and 2:x

Enter column number where to start 2x2 matrix, number needs to be between 0 and 2:x

The determinant is x for
Matrix B:

Row 0:  x      x  
Row 1:  x      x

新的 2x2 矩阵保存在新的结构中,我该怎么做?

关于c - 数学矩阵程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53576372/

相关文章:

python - 使用 pandas 进行矩阵数学计算

c - 有什么方法可以从文件句柄中获取完整路径 str 吗?

c - 在 C 中释放内存

algorithm - 3D 渐进平面拟合算法

javascript - 使用 Math.random 从 JavaScript 中的有限字符集生成随机集

C++填充矩阵 boost

c - 为什么我应该使用或不使用 MSG_CONFIRM?

c++ - 如何在opencv中访问RGB图像(3 channel 图像)的图像数据

algorithm - 相交线和点阵?

matlab - 如何根据条件替换某些列值?