代码之家  ›  专栏  ›  技术社区  ›  Davide Lorino

设置动态分配数组的值

  •  0
  • Davide Lorino  · 技术社区  · 6 年前

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

    矩阵结构

    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;
    }
    

    1, 2, 3, 4, 5, 6, 7, 8, 9, 10 以下是我得到的值:

    enter image description here

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

    0 回复  |  直到 6 年前
        1
  •  2
  •   Mathieu Shrikanth M D    6 年前

    你在地址计算中有一个小小的困惑:你倒过来了 row 答案 col 在乘法中。

    您在中写入(我用它的值替换max) setValues

    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


    还有一件事:

    创建矩阵的功能可以简化:

    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;
    
    }