I am trying to create and store values inside a matrix, represented as a 1 dimensional array.

When I try to set the values, my output comes out wrong.

Matrix Struct

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

matrix_t our_matrix[MAX_MATRICES];

Creating the matrix:

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;

}

Setting Values:

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

When I try call setValues on a 5 x 2 matrix and I give it the values 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 these are the values that I get:

enter image description here

I honestly can't fathom how it stores those values and not the 1:10 that i'm passing it.

1 Answers

2
Mathieu On Best Solutions

You have one little confusion in address computation: You inverted row ans col in multiplication.

You write (I replaced max by its value) in setValues

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

the same in create_matrix function:

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

You should have written:

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

and

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

You can do the math to be convinced:

If rows is 2 and cols is 10, with your method, the max index is 2 + 9 * 10 = 90, it's too big for a 2x10 matrix.


One more thing:

The function create_matrix can be simplified:

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;

}