在 C 中打印和显示单位矩阵的程序

Program to print and display identity matrix in C

我在编写打印矩阵的程序时遇到问题,然后我生成单位矩阵。下面是我的代码,如有任何帮助,我们将不胜感激。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int PrintMatrix(int dim, double matrix[dim][dim]);
int main()

int PrintMatrix(int dim, double matrix[dim][dim]) {
int aa, bb;
for (aa = 0; aa <= dim; aa++) {
    for (bb = 0; bb <= dim; bb++) {
        printf("%lf ", matrix[aa][bb]);
    }
    printf("\n");
}
}

double TestMatrix[7][7] = {
{1,0,0,0,0,0,0},
{0,1,0,0,0,0,0},
{0,0,1,0,0,0,0},
{0,0,0,1,0,0,0},
{0,0,0,0,1,0,0},
{0,0,0,0,0,1,0},
{0,0,0,0,0,0,1}
};
    PrintMatrix(7, TestMatrix);
return 0;
  1. 您的代码无法成功编译。
  2. main后没有左大括号。
  3. 您在 main 中定义函数,这是一个问题。
  4. 检查整个代码中的括号。
  5. 修复了循环控件从 <=<

修改后的代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int PrintMatrix(int dim, double matrix[dim][dim]);
int main()
{
    double TestMatrix[7][7] = {
        {1,0,0,0,0,0,0},
        {0,1,0,0,0,0,0},
        {0,0,1,0,0,0,0},
        {0,0,0,1,0,0,0},
        {0,0,0,0,1,0,0},
        {0,0,0,0,0,1,0},
        {0,0,0,0,0,0,1},
    };
    PrintMatrix(7, TestMatrix);
    return 0;
}

int PrintMatrix(int dim, double matrix[dim][dim]) {
    int aa, bb;
    for (aa = 0; aa < dim; aa++) {
        for (bb = 0; bb < dim; bb++) {
            printf("%lf ", matrix[aa][bb]);
        }
        printf("\n");
    }
}

问题中的代码是一个可怕的非编译混乱。其中一条评论是:

It still isn't returning the identity for dim = 2 up to 7; any thoughts?

BluePixy 一样,如果您对编译器撒谎关于函数输入矩阵的大小,例如通过传递一个 7x7 矩阵但告诉它有一个 3x3 矩阵,它会报复从你想要的打印不同的信息。不要欺骗编译器!

如果要从 7x7 矩阵打印大小为 1..7 的单位矩阵,请告诉编译器(函数)矩阵的实际大小和要打印的大小。对于单位矩阵,您实际上不需要原始矩阵——您可以合成数据。

#include <stdio.h>

static void printIdentityMatrix(int size)
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
            printf("%4.1f", (i == j) ? 1.0 : 0.0);
        putchar('\n');
    }
}

int main(void)
{
    for (int i = 1; i < 8; i++)
        printIdentityMatrix(i);
    return 0;
}

要打印任意大小的方阵的左上角子集,您必须同时传递要打印的数据大小和矩阵的实际大小。

#include <assert.h>
#include <stdio.h>

static void PrintMatrix(int size, int dim, double matrix[dim][dim])
{
    assert(size <= dim);
    for (int aa = 0; aa < size; aa++)
    {
        for (int bb = 0; bb < size; bb++)
            printf("%lf ", matrix[aa][bb]);
        putchar('\n');
    }
}

int main(void)
{
    double TestMatrix[7][7] =
    {
        {1,0,0,0,0,0,0},
        {0,1,0,0,0,0,0},
        {0,0,1,0,0,0,0},
        {0,0,0,1,0,0,0},
        {0,0,0,0,1,0,0},
        {0,0,0,0,0,1,0},
        {0,0,0,0,0,0,1},
    };

    for (int i = 1; i < 8; i++)
    {
        PrintMatrix(i, 7, TestMatrix);
        putchar('\n');
    }
    return 0;
}

打印任意大小的矩形矩阵的任意矩形子矩阵需要更多函数参数(如果我没记错的话是 7 个:

void PrintSubMatrix(int x_off, int y_off, int x_len, int y_len, int x_size, int y_size,
                    double matrix[x_size][y_size]);

那是在您指定要写入的文件流之前。

#include <assert.h>
#include <stdio.h>

static void PrintSubMatrix(int x_off, int y_off, int x_len, int y_len, int x_size, int y_size,
                        double matrix[x_size][y_size])
{
    assert(x_off >= 0 && x_off < x_size && x_off + x_len <= x_size);
    assert(y_off >= 0 && y_off < y_size && y_off + y_len <= y_size);
    printf("SubMatrix size %dx%d at (%d,%d) in M[%d][%d]\n",
           x_len, y_len, x_off, y_off, x_size, y_size);
    for (int x = x_off; x < x_off + x_len; x++)
    {
        for (int y = y_off; y < y_off + y_len; y++)
            printf("%4.1f ", matrix[x][y]);
        putchar('\n');
    }
    putchar('\n');
}

int main(void)
{
    double TestMatrix[7][9] =
    {
        { 1, 2, 3, 4, 3, 2, 1, 2, 3 },
        { 2, 1, 9, 8, 4, 6, 0, 0, 1 },
        { 3, 0, 8, 7, 5, 5, 0, 0, 1 },
        { 4, 0, 5, 6, 6, 8, 4, 4, 4 },
        { 5, 0, 1, 4, 7, 9, 0, 0, 1 },
        { 6, 0, 1, 0, 8, 1, 0, 0, 1 },
        { 7, 0, 0, 0, 9, 0, 1, 0, 1 },
    };

    PrintSubMatrix(0, 0, 7, 9, 7, 9, TestMatrix);

    for (int i = 1; i < 4; i++)
    {
        for (int j = 2; j < 4; j++)
            PrintSubMatrix(i, j, 3 + j - i, i + j, 7, 9, TestMatrix);
    }
    return 0;
}

样本运行:

SubMatrix size 7x9 at (0,0) in M[7][9]
 1.0  2.0  3.0  4.0  3.0  2.0  1.0  2.0  3.0 
 2.0  1.0  9.0  8.0  4.0  6.0  0.0  0.0  1.0 
 3.0  0.0  8.0  7.0  5.0  5.0  0.0  0.0  1.0 
 4.0  0.0  5.0  6.0  6.0  8.0  4.0  4.0  4.0 
 5.0  0.0  1.0  4.0  7.0  9.0  0.0  0.0  1.0 
 6.0  0.0  1.0  0.0  8.0  1.0  0.0  0.0  1.0 
 7.0  0.0  0.0  0.0  9.0  0.0  1.0  0.0  1.0 

SubMatrix size 4x3 at (1,2) in M[7][9]
 9.0  8.0  4.0 
 8.0  7.0  5.0 
 5.0  6.0  6.0 
 1.0  4.0  7.0 

SubMatrix size 5x4 at (1,3) in M[7][9]
 8.0  4.0  6.0  0.0 
 7.0  5.0  5.0  0.0 
 6.0  6.0  8.0  4.0 
 4.0  7.0  9.0  0.0 
 0.0  8.0  1.0  0.0 

SubMatrix size 3x4 at (2,2) in M[7][9]
 8.0  7.0  5.0  5.0 
 5.0  6.0  6.0  8.0 
 1.0  4.0  7.0  9.0 

SubMatrix size 4x5 at (2,3) in M[7][9]
 7.0  5.0  5.0  0.0  0.0 
 6.0  6.0  8.0  4.0  4.0 
 4.0  7.0  9.0  0.0  0.0 
 0.0  8.0  1.0  0.0  0.0 

SubMatrix size 2x5 at (3,2) in M[7][9]
 5.0  6.0  6.0  8.0  4.0 
 1.0  4.0  7.0  9.0  0.0 

SubMatrix size 3x6 at (3,3) in M[7][9]
 6.0  6.0  8.0  4.0  4.0  4.0 
 4.0  7.0  9.0  0.0  0.0  1.0 
 0.0  8.0  1.0  0.0  0.0  1.0 

如果代码固定在每行末尾不打印空白就更好了;这留作 reader.

的练习