繁体   English   中英

C-在结构数组中打印2d数组的元素

[英]C - Printing elements of a 2d array inside a struct array

我试图创建一个设置2D数组的值的函数,然后再创建一个打印这些值的函数。

出于某种原因,在我当前的实现中,如果我创建一个3x3矩阵并将每个值都设置为数字5,它将打印这些值,但是会打印从5开始计数的值,因此在打印567、678、789时会显示我希望它打印我设置的确切值。

这是函数定义-我在做什么错?

结构定义:

    struct matrix{
        char matrixName[50];
        int rows;
        int columns;
        float* data;
    };
    typedef struct matrix matrix_t;

矩阵创建:

int create_matrix(matrix_t* matrixP, int matrixLocation){

  char tempName[50];
  int rows, cols;


  printf("Enter a name for your matrix>\n");
  scanf("%s", tempName);

  printf("Enter the number of rows>\n");
  scanf("%d", &rows);

  printf("Enter the number of cols>\n");
  scanf("%d", &cols);

  float * our_matrix = (float *) malloc(rows * cols * sizeof(float));  

  strcpy(matrixP[matrixLocation].matrixName, tempName);
  matrixP[matrixLocation].rows = rows;
  matrixP[matrixLocation].columns = cols;
  matrixP[matrixLocation].data = our_matrix;

  return 0;

}

设定值:

int setValues(matrix_t* our_matrix, int matrix_index) {
  int counter = 0;
  int row = 0, col = 0;
  for (col = 1; col <= our_matrix[matrix_index].columns; col++) {
    for (row = 1; row <= our_matrix[matrix_index].rows; row++) {
      counter++;

      printf("Enter the value for column number %d of row number %d>\n", col, row);
      scanf("%f", our_matrix[matrix_index].data+(col-1)+(row-1));                
    }
    /* separate rows by newlines */
  }
    return 0;
}

打印:

int printMatrix(matrix_t* our_matrix, int index) {
      int row = 0, col = 0;
      for (col = 1; col <= our_matrix[index].columns; col++) {
        for (row = 1; row <= our_matrix[index].rows; row++) {
      printf(" %2.f ", *our_matrix[index].data+(col-1)+(row-1));   
    }
    printf("\n");
  }
    return 0;
}

如果我不先使用setValues就调用printMatrix()函数,则看起来像这样打印单元格的相对位置:

在此处输入图片说明

但是,当我调用setValues()并将所有值设置为数字5时,它的打印从数字5开始递增,如下所示:

在此处输入图片说明

您的排名计算不正确。 有一个问题和一个“常规符号”问题。

问题是数组下标的计算:

 scanf("%f", our_matrix[matrix_index].data+(col-1)+(row-1)); printf(" %2.f ", *our_matrix[index].data+(col-1)+(row-1)); 

您需要将(row - 1)值乘以our_matrix[matrix_index].cols 使用一些较短的值名称可能会做得更好:

int max_col = our_matrix[matrix_index].columns;
int max_row = our_matrix[matrix_index].rows;
float *data = our_matrix[matrix_index].data;

for (col = 1; col <= max_col; col++)
{
    for (row = 1; row <= max_row; row++)
    {
        printf("Enter the value for column number %d of row number %d>\n", col, row);
        if (scanf("%f", data + (col-1) + (row-1) * max_col) != 1)
        {
            …report error and do not continue…
        }
    }
}

for (col = 1; col <= max_col; col++)
{
    for (row = 1; row <= max_row; row++)
        printf(" %.2f", data[(col-1) + (row-1) * max_col]);
    putchar('\n');
}

那处理了实质性的错误。 注意scanf()上的错误检查。 这在“现实世界”计划中很重要(即使您在课堂上或在线比赛中没有参加该计划也是如此)。 您也可以在对scanf()的调用中明智地使用符号&data[(col-1) + (row-1) * max_cols]代替data + (col-1) + (row-1) * max_cols —会提高代码的一致性。

“常规表示法”的问题是,在C中,数组索引从0而不是1开始,并且您可以通过遵循C约定来避免多个-1项。 该代码段还在需要时声明了循环变量,从而最大程度地减小了范围。 自C99以来,这是C的功能-在某些逆行(但流行且广泛使用)的平台上可能无法使用。

for (int col = 0; col < max_col; col++)
{
    for (int row = 0; row < max_row; row++)
        printf(" %.2f", data[col + row * max_col]);
    putchar('\n');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM