打印矩阵行的功能
我正在尝试编写一个打印用户选择的矩阵行的函数。它适用于矩阵的第一行,但对其他行不起作用。 这是我的代码。
行:我们要打印的行 代码中的行/列数
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编译器已经知道您的数组是50 x 50,
除了您(可能是?)的数组一样,别无其他,就像它具有不同的大小一样,因为您输入
n
作为大小。因此,当您分配条目时,它们不会放在正确的位置(尽管
n
是其他东西,但根据编译器仍然是50个条目)。确保
n
和size
匹配。The compiler already knows your array is 50 x 50 and nothing else
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,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
andSIZE
match.