User defined array sizes in C -


I am reading through "Illustrated C" and first exercise asks the question:

< P> The program MATMUL enhances the fixed size matrix. Program deal with any specified size.

The code given below has come so far this way. However, I have read that all the features need to be declared before the main function. So how do I get the custom arrays of arrays without being declared in the main function?

#define _CRT_SECURE_NO_DEPRECATE #include & lt; Stdio.h & gt; Int n, m, i, j, k; Int main (zero) {printf ("Enter \ n: Rows for A, rows for columns A and B, columns for B \ n"); Scan F ("% i% i% i", & amp; i, & amp; j, & amp; k); Float a [i] [j], b [j] [k], c [ii] [k]; // Not legal, right? / * (M = 0; M & lt; j; ++ m) read in A array * / in scanf ("% f", and a [n] [/ a] (n = 0; n & Lt; i; ++ n) [m]); For read / write in array for B * / (n = 0; n & lt; j; ++ n) (m = 0; m & lt; k; ++ m) scanf ("% f", & Amp; b [n] [m]); For calculation / for the C array, for (j = 0; j & lt; i; ++ j) (i = 0; i & lt; k; ++ i) {c [i] [j] ] = 0; For (k = 0; k & lt; j; ++ k) c [i] [j] + = a [i] [k] * b [k] [j]; } For (n = 0; n & lt; i; ++ n) (m = 0; m & lt; k; ++ m) printf ("\ n% .2f \ t", c [n] ] [M]); Return 0; }

float a [i] [j], b [j] [K], c [ii] [k]; // Not legal, right?

Your question has been tagged C and is part of C99, so float a [i] [ja], b [j] ], C [ii] [k]; is legal.

Edit

If your compiler does not support C99 then you have not left any option other than dynamic memory allocation is.

Example:

  float ** A; Int l; A = (float **) malloc (i * sizeof (float *)); For (L = 0; L; LT; I; ++ L) A [L] = (Float *) Molec (J * size (float));  

Note : Remember > forget about forgetting .


Comments