2

For my assignment I have to do mathematical operations on array I'm reading in from a text file. And it has to be dynamically allocated because we don't know the size of the matrix although we do know that it will be square.So I've managed to read in the array and I've put in the code so that it knows the rows and columns at run time. But my professor wants us to use contiguous arrays and allocate the matrix like this

float* A = (float*) malloc( n * m * sizeof(float) ); 

But I don't know how to read in the values from the text file into this array. What would I do use a for loop or something? And how would I access the elements in the array later on?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
alex
  • 27
  • 4

4 Answers4

5

If you must "fold" a 2D matrix into a "flat" array, use the allocation line that your professor suggested (after removing the unnecessary cast) and make a pair of functions for accessing the matrix:

float get(const float *m, size_t width, size_t r, size_t c) {
    return m[r*width+c];
}
void set(float *m, size_t width, size_t r, size_t c, float val) {
    m[r*width+c] = val;
}

Note: The above illustrates packing the matrix in row-major order; column-major order is another possibility.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Given an allocation of the old-school form

float* A = (float*) malloc( n * m * sizeof(float) ); 

To read data into it

if (A) {
  for (size_t ni = 0; ni < n; ni++) {
    for (size_t mi = 0; mi < m; mi++) {
      if (fscanf(inf, "%f", A + ni*m + mi) != 1) Handle_Bad_Read();
      // or 
      if (fscanf(inf, "%f", &A[ni*m + mi]) != 1) Handle_Bad_Read();
    } 
  }

To print it

  for (size_t ni = 0; ni < n; ni++) {
    for (size_t mi = 0; mi < m; mi++) {
      printf(inf, " %g", A[ni*m + mi]);
    }
    putc('\n'); 
  }
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

@dasblinkenlight's answer is a good one. Just to elaborate a little on how you might have figured out the math in that answer on your own: when I need to determine how to represent the relationship between two quantities mathematically, I often find it helpful to draw a picture. Excel is your friend in this case. I'll start out with a specific example. For instance, suppose I have an array with five rows and four columns. As @dasblinkenlight pointed out, there are two clear ways you might number the cells in such an array: row-major order or column-major order. I'd number them manually in my specific example:

A specific example

Then I'll look at what I've drawn and try to figure out the patterns. In the first image you can see that cell numbers increase by one as you move from one column to the next, and by four (the number of columns) as you move from one row to the next. In the second image it's the opposite. So then I'll try to write a generalized example, with M rows and N columns:

Generalized

Compare what I've found in my highlighted cells to @dasblinkenlight's code.

You won't always need to draw out everything by hand in order to infer the answer—the more experience you accumulate as a developer, the easier it will be to see these patterns in your head—but it's a useful approach to problems like these.

Joe Farrell
  • 3,502
  • 1
  • 15
  • 25
0

The first part of your question is a bit unclear. As for the second part , if you are allocating a 2D matrix using contiguous allocation , you can access the values stored in it like :

 matrix[ i * <no_of_columns> + j]

where "no_of_columns" is the width of the 2D matrix , which corresponds to

 matrix[i][j] 

if put simply.

Varad Bhatnagar
  • 599
  • 1
  • 7
  • 19