调用2D数组函数?

时间:2017-06-23 06:08:50

标签: c function matrix multidimensional-array call

如果你们中的一些人发现我的问题很愚蠢且容易解决,我首先道歉,但我是“c”的初学者。

我的任务是使用不同的函数创建3x3矩阵的逆。

我现在要做的是告诉用户输入3x3矩阵的值,然后打印它们。我创建了两个读取和打印值的函数,但是我调用它们有问题,因为我无法在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]);
       }
   }
}

4 个答案:

答案 0 :(得分:2)

$(function(){ $(".flash .close").on("click", function(){ $(this).parent().removeClass("active"); }) }); printf中,传递与指针类型匹配的确切格式说明符至关重要。如果格式说明符与提供的参数不匹配,则结果为未定义的行为。

实际上,这个

scanf

需要替换为

scanf("%d", &a[i][j]);

同样适用于scanf("%lf", &a[i][j]); - &gt; printf("Your matrix is: %d", a[i][j]);

此外,在printf("Your matrix is: %lf", a[i][j]);中,您已在内循环中使用了循环变量printMatrix两次。你想要的是

i

编辑:正如@cse在评论中指出的那样,请在for(i = 0; i < SIZE; i++){ for(j = 0; j < SIZE; j++){ printf("%lf ", a[i][j]); printf("\n"); } 中删除此行:

main

由于此时printf("Enter the values for the matrix:\n", i, j); i未初始化,因此它们包含垃圾。

答案 1 :(得分:1)

在printMatrix中,有一个无限循环肯定会导致你的程序没有响应。应该是:

   for(j = 0; j < SIZE; j++){
        printf("Your matrix is: %d", a[i][j]);
}

答案 2 :(得分:1)

以上代码存在以下问题:

  • printf("Your Matrix:%d\n",a[i][j]);函数的行main()中,因为变量ij未初始化,因此它包含垃圾值。因此,请勿在{{1​​}}打印价值,因为它可能导致a[i][j] OR 使用有效值初始化segmentation faulti,即数组j中的有效索引。 您也可以将double a[][]中的行printf("Enter the values for the matrix:\n", i, j);更改为main()。由于此处未使用printf("Enter the values for the matrix:\n");i
  • 功能j的第scanf("%d", &a[i][j]);行。由于您正在阅读void readMatrix(double a[SIZE][SIZE])原始数据类型,因此您应使用double格式化程序而不是%lf。功能%d中的行printf("Your matrix is: %d", a[i][j])也是如此。
  • 功能void printMatrix(double a[SIZE][SIZE])的第for(i = 0; i < SIZE; j++)行。它应该是void readMatrix(double a[SIZE][SIZE]),即内循环中使用的变量应为for(j = 0; j < SIZE; j++)而不是j

您可以在更正代码后找到工作代码here

答案 3 :(得分:1)

要打印输出:您需要实际打印出整个内容而不是一个接一个的值,对吗?

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]

数字当然是数组中的数字。

除非您打算以柱状方式填充矩阵,否则应该在输入函数上切换ij循环。您将矩阵存储在内存转置中。 (此代码假定您不是)

相关问题