-1

I have two 2 dimensional arrays:

#define MAXSIZE 10
/* ... */
int A[MAXSIZE][MAXSIZE], B[MAXSIZE][MAXSIZE];

I'm reading in values from a file:

1 1 2
2 2 -6 4 5 6

On each line, the first two numbers are the row and column sizes for the array, after which are enough (arbitrary) values to fill up an array using those sizes. What I want to do is, after assigning those values to the arrays, check if the dimensions of arrays A and B are the same so I can do matrix arithmetic with them (addition, scalar multiplication, etc).

Alex Mohr
  • 709
  • 4
  • 9
  • 25
  • By "fill up an array" I assume you mean in row-major order (at least I would hope so). That said. *you read the dimensions from a file*. Assuming you read them into variables, is there something preventing you from comparing those variables? – WhozCraig Sep 25 '13 at 21:49
  • The requirements for the procedure `DimCheck(int A[][], int B[][])` for this assignment restrict what I can use as parameters. In this case it want's the arrays themselves as parameters – Alex Mohr Sep 25 '13 at 22:05

2 Answers2

0

Why don't you store the row/column sizes for each line into their variables?

int Arow, Acol, Brow, Bcol;

Normally, this would work:

int Acol = sizeof(A[0]);
int Arow = sizeof(A) / Acol;

But your arrays are initialized to fixed sizes.

Have you thought about using malloc to dynamically allocated A and B?

prgao
  • 1,767
  • 14
  • 16
-1

If you simply want to know if the two are == sized:

#define MAXSIZE 10
/* ... */
int A[MAXSIZE][MAXSIZE], B[MAXSIZE][MAXSIZE];

int main(void)
{
    int sizea = sizeof(A);
    int sizeb = sizeof(B);
    int result = (sizea == sizeb) ? (1) : (0);
    return 0;

}

Even if you do not explicitly write to each location, the matrices, the way you have them defined, will be the same size.

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • Wouldn't `sizeof(A)` and `sizeof(B)` return the same thing, since they are initialized with `MAXSIZE`? – Alex Mohr Sep 25 '13 at 21:55
  • Yes - `int A[MAXSIZE][MAXSIZE], B[MAXSIZE][MAXSIZE];` guarantees it. So, what is your real question then? – ryyker Sep 25 '13 at 21:58
  • In my example, A would be a 1x1 matrix and B would be a 2x2 matrix. I want to check whether the dimensions of A and B are the same or not (in this case they are not). The way this assignment is set up is really stupid (they always are) but I have to work with it this way. I wouldn't code like this if I could help it. – Alex Mohr Sep 25 '13 at 22:02
  • There is an ***[answer here](http://stackoverflow.com/a/15957714/645128)*** that may help you – ryyker Sep 25 '13 at 22:43
  • Assuming the down vote here is because I returned result from main()? Deserved it. :) – ryyker Sep 26 '13 at 00:27