设置动态分配数组的值

时间:2019-05-28 10:27:25

标签: c arrays matrix

我正在尝试在矩阵内创建和存储值,以一维数组表示。

当我尝试设置值时,输出结果错误。

矩阵结构

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

matrix_t our_matrix[MAX_MATRICES];

创建矩阵:

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 * data = (float *) malloc(rows * cols * sizeof(float));  

  int row = 0, col = 0;
  for (row = 1; row <= rows; row++) {
    for (col = 1; col <= cols; col++) {

      data[(row-1) + (col-1) * cols] = 0;
    }
  }

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

  return 0;

}

设置值:

int setValues(matrix_t* our_matrix, int number_of_matrices) {
  int i, matrix_index;
  for(i = 0; i < number_of_matrices; i++){
    printf("Matrix %i\t %i Rows\t %i Columns - Name: %s\n", i+1, our_matrix[i].rows, our_matrix[i].columns, our_matrix[i].matrixName);
  }

  printf("\nWhich matrix would you like to set the values for?\n");
  printf("Select a matrix number from the list above>\n");

  scanf("%d", &matrix_index);

  while(matrix_index < 1 || matrix_index > number_of_matrices){
    printf("Enter a number from the list of available matrices - number must be greater than zero and less than or equal to the number of available matrices:\n");
    scanf("%d", &matrix_index);
  }

  int max;
  matrix_index -= 1;
  max = our_matrix[matrix_index].columns;


  float *data = our_matrix[matrix_index].data;
  int row = 0, col = 0;
  for (row = 0; row < our_matrix[matrix_index].rows; row++) {
    for (col = 0; col < our_matrix[matrix_index].columns; col++) {

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

当我尝试在5 x 2矩阵上调用setValues并将其值1, 2, 3, 4, 5, 6, 7, 8, 9, 10赋予它们时,这些是我得到的值:

enter image description here

老实说,我无法理解它如何存储这些值,而不是我所传递的1:10。

1 个答案:

答案 0 :(得分:2)

您在地址计算方面有些困惑:您将rowcol的乘法求反。

您在setValues中写(我用其值替换了max)

scanf("%f", &data[(row) + (col) * (our_matrix[matrix_index].columns)]);

create_matrix函数相同:

data[(row-1) + (col-1) * cols] = 0;

您应该写:

scanf("%f", &data[(col) + (row) * (our_matrix[matrix_index].columns)]); 

data[(col-1) + (row-1) * cols] = 0;

您可以做个令人信服的数学运算:

如果rows为2且cols为10,则使用您的方法,最大索引为2 + 9 * 10 = 90,对于2x10矩阵来说太大了。


还有一件事:

函数create_matrix可以简化:

int create_matrix(matrix_t* matrixP, int matrixLocation){

  int rows, cols;

  printf("Enter a name for your matrix>\n");
  /* Warning, you should test what scanf returned! */
  scanf("%s", matrixP[matrixLocation].matrixName);

  printf("Enter the number of rows>\n");
  /* Warning, you should test what scanf returned! */
  scanf("%d", &rows);  

  printf("Enter the number of cols>\n");
  /* Warning, you should test what scanf returned! */
  scanf("%d", &cols);

  matrixP[matrixLocation].rows = rows;
  matrixP[matrixLocation].cols = cols;

  /* Warning, you should test what calloc returned! */
  matrixP[matrixLocation].data = calloc(rows * cols, sizeof(float));  

  return 0;

}