打印矩阵行的功能

发布于 2025-01-26 19:24:40 字数 676 浏览 1 评论 0原文

我正在尝试编写一个打印用户选择的矩阵行的函数。它适用于矩阵的第一行,但对其他行不起作用。 这是我的代码。

行:我们要打印的行 代码中的行/列数

N:矩阵矩阵(NXN)

#include <stdio.h>
#define SIZE 50

void prow(float *arr, int row, int n){  
    int i;
    for(i = row * n; i < (row * n) + n; i++){
        printf("%f ", *(arr+i));
    }

}

int main(){
    int n, i, j;
    float matrix[SIZE][SIZE];

    printf("Row / column number: ");
    scanf("%d", &n);

    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            printf("[%d][%d] element: ", i, j);
            scanf("%f", &matrix[i][j]);
        }
    }


    prow(&matrix[0][0], 2, n);

    return 0;
}

I'm trying to write a function that prints a matrix row chosen by user. It works for the first row of the matrix but it doesn't work for other rows.
Here's my code.

row: the row we want to print
n: number of the rows/columns in matrix

Matrix (nxn)

Code:

#include <stdio.h>
#define SIZE 50

void prow(float *arr, int row, int n){  
    int i;
    for(i = row * n; i < (row * n) + n; i++){
        printf("%f ", *(arr+i));
    }

}

int main(){
    int n, i, j;
    float matrix[SIZE][SIZE];

    printf("Row / column number: ");
    scanf("%d", &n);

    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            printf("[%d][%d] element: ", i, j);
            scanf("%f", &matrix[i][j]);
        }
    }


    prow(&matrix[0][0], 2, n);

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2025-02-02 19:24:40

编译器已经知道您的数组是50 x 50,

#define SIZE 50
float matrix[SIZE][SIZE];

除了您(可能是?)的数组一样,别无其他,就像它具有不同的大小一样,因为您输入n作为大小。因此,当您分配条目时,

scanf("%f", &matrix[i][j]);

它们不会放在正确的位置(尽管n是其他东西,但根据编译器仍然是50个条目)。

确保nsize匹配。

The compiler already knows your array is 50 x 50 and nothing else

#define SIZE 50
float matrix[SIZE][SIZE];

but you are (probably?) using the array as if it had a different size, because you input n as size. So when you assign the entries,

scanf("%f", &matrix[i][j]);

they aren't being put in the right places (each row is still 50 entries according to the compiler, despite n being something else).

Make sure n and SIZE match.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文