【问题标题】:Issue reading input data from 2D array从二维数组读取输入数据的问题
【发布时间】:2014-04-12 19:05:59
【问题描述】:

我正在尝试制作一个矩阵代数程序,所以我从一个预定义的 3x3 矩阵开始。遇到了一些元素没有被正确读取到二维数组中的问题,所以我隔离了一个。问题是一行中的最后一个数字被打印为下一行的第一个元素。即 array[1][2] 被打印为 array [2][0] 是什么。

同样,如果我有一个 3x3 矩阵,我们从 1 到 9,并且我想要第 1 行第 3 列的值(即 3),它会给我 4。第 2 行第 3 列也是如此;值是 6,但它给了我 7。

我已经输入了一个打印语句来显示输入前的计数器值,它们是正确的。我猜scanf出了点问题,但我真的很难过。任何帮助将不胜感激!

#include <stdio.h>

int matrix1[2][2];

void main(void)
{
    int i = 0, j=0;    //Row and column subscripts for the first matrix 

    //======================================================================================================//
    //First Matrix
    printf("This is for the first matrix");
    for(i=0; i < 3; i++) //Start with row
    {
        for(j = 0; j < 3; j++)
        {
            printf("\nInsert the value at row %d, column %d: ", i+1,j+1); //+1 since array's begin at 0
            printf("\ni is %d, j is %d", i,j); //Shows what elements the input will go into
            scanf("%d", &matrix1[i][j]); //Enter value into row i column j
        }

    }
    printf("\n\n%d", matrix1[1][2]); //test to see what's in the element
} 

【问题讨论】:

  • first ,int matrix1[2][2]; --> int matrix1[3][3];
  • 那会给我一个 4x4 不是吗?
  • int matrix1[3][3]; 给 3x3。矩阵1[0..2][0..2]。尝试测试printf("%d\n", (int)sizeof(matrix1)/sizeof(int));
  • 没有。在 C 中,大小为 n 的数组的数组索引从 0 到 n - 1 运行。如果您想要一个 3 x 3 矩阵,则需要定义 int matrix[3][3],它的每个维度的索引从 0 到 2。
  • 好的,我明白了,谢谢大家!现在可以了。我猜我最后一个元素是零零?刚刚在 C 中弄湿了我的脚,感谢所有帮助。

标签: c matrix multidimensional-array


【解决方案1】:
#include <stdio.h>

#define ROWS 3
#define COLS 3

int matrix1[ROWS][COLS];

void main(void)
   {
   int i, j;    //Row and column subscripts for the first matrix 

   //================================================================================
   //First Matrix
   printf("This is for the first matrix");
   for(i=0; i < ROWS; i++) //Start with row
      {
      for(j = 0; j < COLS; j++)
         {
         printf("\nInsert the value at row %d, column %d: ", i+1,j+1); 
         printf("\ni is %d, j is %d", i,j); 
         scanf("%d", &matrix1[i][j]);
         }
      }

   printf("\n\n%d", matrix1[1][2]); //test to see what's in the element
   } 

【讨论】:

    【解决方案2】:

    我认为您对数组中的数组索引号元素总数感到困惑。

    例如,当您有 5 个整数的数组时。

       int ary[5] = {11,22,33,44,55};
    
     `------------IN Memory-----------
      | 11 || 22 || 33 | | 44 | | 55 |      
       -------------------------------  `
        0     1     2       3      4  <--------These are called Index numbers.
                                               They always start with 0,Hence 
                                   they always end with(Total Number of elements - 1)
    

    因此从上面的解释中你要么想要改变数组的大小

    int matrix[2][2]int matrix[3][3];

    要么将您的 for() 循环更改为

     for(i=0; i < 2; i++) //changed i<3 to i<2 
     {
         for(j = 0; j < 2; j++) //changed i<3 to i<2
        {
          // your Code.....  
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-10
      • 1970-01-01
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      • 2015-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多