调用二维数组函数?
Calling a 2D array function?
如果你们中的一些人认为我的问题愚蠢且容易解决,我首先道歉,但我是 "c" 的初学者。
我的任务是使用不同的函数创建 3x3 矩阵的逆矩阵。
我现在要做的是告诉用户输入 3x3 矩阵的值,然后打印它们。我制作了 2 个读取和打印值的函数,但调用它们时遇到问题,因为我无法直接调用 printf
.
中的数组
现在我可以 运行 程序,输入值并打印错误结果,这会导致 不响应 程序。
#include <stdio.h>
#include <stdlib.h>
#define SIZE 3 //defining the size of the matrix (3x3)
//prototyping the functions used to calculate the inverse of the matrix
void readMatrix(double a[SIZE][SIZE]);
void printMatrix(double a[SIZE][SIZE]);
main()
{
double a[SIZE][SIZE];
int i,j;
printf("Enter the values for the matrix:\n", i, j);
readMatrix(a);
printf("Your Matrix:%d\n",a[i][j]);
printMatrix(a);
return 0;
}
//function 1
//letting the user to enter a matrix
void readMatrix(double a[SIZE][SIZE]){
int i,j;
for(i = 0; i < SIZE; i++){
for(j = 0; j < SIZE; j++){
scanf("%d", &a[i][j]);
}
}
}
//function 2
//outputing the given matrix
void printMatrix(double a[SIZE][SIZE]){
int i,j;
for(i = 0; i < SIZE; i++){
for(i = 0; i < SIZE; j++){
printf("Your matrix is: %d", a[i][j]);
}
}
}
在 printf
和 scanf
中,传递与变量指针类型匹配的准确格式说明符至关重要。如果您的格式说明符与提供的参数不匹配,结果是未定义的行为。
实际上,这
scanf("%d", &a[i][j]);
需要替换为
scanf("%lf", &a[i][j]);
printf("Your matrix is: %d", a[i][j]);
-> printf("Your matrix is: %lf", a[i][j]);
也一样
此外,在 printMatrix
中,您在内循环中使用了循环变量 i
两次。你要的是
for(i = 0; i < SIZE; i++){
for(j = 0; j < SIZE; j++){
printf("%lf ", a[i][j]);
printf("\n");
}
编辑:正如@cse在评论中指出的那样,删除main
中的这一行:
printf("Enter the values for the matrix:\n", i, j);
由于此时 i
和 j
尚未初始化,它们将包含垃圾。
在printMatrix中,有一个死循环,肯定会导致你的程序无响应。应该是:
for(j = 0; j < SIZE; j++){
printf("Your matrix is: %d", a[i][j]);
}
以上代码存在以下问题:
- 在
main()
函数的第printf("Your Matrix:%d\n",a[i][j]);
行,由于变量i
和j
没有初始化,所以它包含垃圾值。所以不要打印 a[i][j]
处的值,因为它可能会导致 segmentation fault
。 OR 用有效值初始化 i
和 j
,即数组 double a[][]
中的有效索引。 您还可以将 main()
中的行 printf("Enter the values for the matrix:\n", i, j);
更改为 printf("Enter the values for the matrix:\n");
。因为这里没有使用i
和j
.
- 在函数
void readMatrix(double a[SIZE][SIZE])
的行 scanf("%d", &a[i][j]);
中。由于您正在读取 double
原始数据类型,因此您应该使用 %lf
格式化程序而不是 %d
。与函数 void printMatrix(double a[SIZE][SIZE])
. 中的行 printf("Your matrix is: %d", a[i][j])
相同
- 在函数
void readMatrix(double a[SIZE][SIZE])
的行 for(i = 0; i < SIZE; j++)
中。它应该是 for(j = 0; j < SIZE; j++)
即在内部循环中使用的变量应该是 j
而不是 i
.
您可以找到工作代码 here 更正代码后的代码。
对于打印输出:您实际上想要打印出整个内容而不是一个接一个地打印,对吗?
printf ("matrix is:\n")
char outstr[64]; //arbitrary but plenty big enough
char * pout; //points to a point in the string
for(j = 0; j < SIZE; j++){
pout = outstr;
for(i = 0; i < SIZE; i++){
pout += sprintf (pout, "%lf," a [i][j]);
*(--pout) = 0; //end the string one char earlier (dangling ',')
printf ("[%s]\n", outstr);
}
将打印:
matrix is:
[1,2,3]
[4,5,6]
[7,8,9]
其中数字当然是数组中的数字。
此外,除非您打算以柱状方式填充矩阵,否则您应该切换输入函数的 i
和 j
循环。您正在将矩阵存储在转置的内存中。 (此代码假定您不是)
如果你们中的一些人认为我的问题愚蠢且容易解决,我首先道歉,但我是 "c" 的初学者。
我的任务是使用不同的函数创建 3x3 矩阵的逆矩阵。
我现在要做的是告诉用户输入 3x3 矩阵的值,然后打印它们。我制作了 2 个读取和打印值的函数,但调用它们时遇到问题,因为我无法直接调用 printf
.
现在我可以 运行 程序,输入值并打印错误结果,这会导致 不响应 程序。
#include <stdio.h>
#include <stdlib.h>
#define SIZE 3 //defining the size of the matrix (3x3)
//prototyping the functions used to calculate the inverse of the matrix
void readMatrix(double a[SIZE][SIZE]);
void printMatrix(double a[SIZE][SIZE]);
main()
{
double a[SIZE][SIZE];
int i,j;
printf("Enter the values for the matrix:\n", i, j);
readMatrix(a);
printf("Your Matrix:%d\n",a[i][j]);
printMatrix(a);
return 0;
}
//function 1
//letting the user to enter a matrix
void readMatrix(double a[SIZE][SIZE]){
int i,j;
for(i = 0; i < SIZE; i++){
for(j = 0; j < SIZE; j++){
scanf("%d", &a[i][j]);
}
}
}
//function 2
//outputing the given matrix
void printMatrix(double a[SIZE][SIZE]){
int i,j;
for(i = 0; i < SIZE; i++){
for(i = 0; i < SIZE; j++){
printf("Your matrix is: %d", a[i][j]);
}
}
}
在 printf
和 scanf
中,传递与变量指针类型匹配的准确格式说明符至关重要。如果您的格式说明符与提供的参数不匹配,结果是未定义的行为。
实际上,这
scanf("%d", &a[i][j]);
需要替换为
scanf("%lf", &a[i][j]);
printf("Your matrix is: %d", a[i][j]);
-> printf("Your matrix is: %lf", a[i][j]);
此外,在 printMatrix
中,您在内循环中使用了循环变量 i
两次。你要的是
for(i = 0; i < SIZE; i++){
for(j = 0; j < SIZE; j++){
printf("%lf ", a[i][j]);
printf("\n");
}
编辑:正如@cse在评论中指出的那样,删除main
中的这一行:
printf("Enter the values for the matrix:\n", i, j);
由于此时 i
和 j
尚未初始化,它们将包含垃圾。
在printMatrix中,有一个死循环,肯定会导致你的程序无响应。应该是:
for(j = 0; j < SIZE; j++){
printf("Your matrix is: %d", a[i][j]);
}
以上代码存在以下问题:
- 在
main()
函数的第printf("Your Matrix:%d\n",a[i][j]);
行,由于变量i
和j
没有初始化,所以它包含垃圾值。所以不要打印a[i][j]
处的值,因为它可能会导致segmentation fault
。 OR 用有效值初始化i
和j
,即数组double a[][]
中的有效索引。 您还可以将main()
中的行printf("Enter the values for the matrix:\n", i, j);
更改为printf("Enter the values for the matrix:\n");
。因为这里没有使用i
和j
. - 在函数
void readMatrix(double a[SIZE][SIZE])
的行scanf("%d", &a[i][j]);
中。由于您正在读取double
原始数据类型,因此您应该使用%lf
格式化程序而不是%d
。与函数void printMatrix(double a[SIZE][SIZE])
. 中的行 - 在函数
void readMatrix(double a[SIZE][SIZE])
的行for(i = 0; i < SIZE; j++)
中。它应该是for(j = 0; j < SIZE; j++)
即在内部循环中使用的变量应该是j
而不是i
.
printf("Your matrix is: %d", a[i][j])
相同
您可以找到工作代码 here 更正代码后的代码。
对于打印输出:您实际上想要打印出整个内容而不是一个接一个地打印,对吗?
printf ("matrix is:\n")
char outstr[64]; //arbitrary but plenty big enough
char * pout; //points to a point in the string
for(j = 0; j < SIZE; j++){
pout = outstr;
for(i = 0; i < SIZE; i++){
pout += sprintf (pout, "%lf," a [i][j]);
*(--pout) = 0; //end the string one char earlier (dangling ',')
printf ("[%s]\n", outstr);
}
将打印:
matrix is:
[1,2,3]
[4,5,6]
[7,8,9]
其中数字当然是数组中的数字。
此外,除非您打算以柱状方式填充矩阵,否则您应该切换输入函数的 i
和 j
循环。您正在将矩阵存储在转置的内存中。 (此代码假定您不是)