4

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

Program MATMUL multiplies matrices of fixed size. Make the program deal with any specified sizes.

So below is the code that I have come up with thus far. However I read that all attributes need to be declared before the main function. So how do I get custom sized arrays without declaring them in the main function?

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>

int n, m, i, j, k;

int main(void)
{
    printf("\nEnter:rows for A, columns for A and rows for B, columns for B\n");
    scanf("%i %i %i", &i, &j, &k);
    float A[i][j], B[j][k], C[i][k];    //Not legal, right?

    /*Read in A array*/
    for(n=0; n<i; ++n)
        for(m=0; m<j; ++m)
            scanf("%f", &A[n][m]);

    /*Read in B array*/
    for(n=0; n<j; ++n)
        for(m=0; m<k; ++m)
            scanf("%f", &B[n][m]);

    /*Calculate C array*/
    for(j=0; j<i; ++j)
        for(i=0; i<k; ++i)
        {
            C[i][j] = 0;
            for (k=0; k<j; ++k)
                C[i][j] += A[i][k] * B[k][j];
        }
    for(n=0; n<i; ++n)
         for(m=0; m<k; ++m)
        printf("\n%.2f\t", C[n][m]);

    return 0;
}
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
Ashley
  • 2,256
  • 1
  • 33
  • 62
  • 1
    You should at least compile your code to see what happens before posting here. ? Then you would find out that you did the right thing. (You have other errors in your code, which is how I know you didn't compile it.) – cape1232 Jul 19 '10 at 16:45
  • @cape1232: I did compile the code, but since most of the errors related to `'A' : undeclared identifier` I thought I would fix the array problem. The loops and semi-colons I can deal with. – Ashley Jul 19 '10 at 16:49

2 Answers2

6

float A[i][j], B[j][k], C[i][k]; //Not legal, right?

Your question has been tagged C and VLAs are part of C99, so float A[i][j], B[j][k], C[i][k]; is legal.

EDIT

If your compiler doesn't support C99 then you are left with no option other than dynamic memory allocation.

Example:

  float **A;
  int l;
  A= (float**)malloc(i*sizeof(float*));
  for(l=0;l<i;++l)
     A[l]= (float*)malloc(j*sizeof(float));

Note: Do not forget to free the memory when you are done.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • His problem might be, that a variable declaration is not allowed at this point. Add some `{}` or use a second method ... – tanascius Jul 19 '10 at 16:44
  • IF your compiler conforms to C99. If you need to support a compiler that doesn't (they are still out there) then you should know how to do it both ways (with malloc and without) – A. Levy Jul 19 '10 at 16:45
  • 1
    `If you need to support a compiler that doesn't (they are still out there) ` then I'd tell them to upgrade their compilers. – Prasoon Saurav Jul 19 '10 at 16:46
  • @A Levy: Compiling with Visual C++ Express 2008, is it C99 compliant? – Ashley Jul 19 '10 at 16:50
  • 4
    No, MSVC does not support C99. – Ivo Wetzel Jul 19 '10 at 16:53
  • 1
    If you take Prasoon's advice and upgrade your compiler then use VLAs, make sure you don't allow insanely large dimensions. GCC puts VLAs on the stack. – nmichaels Jul 19 '10 at 17:12
  • I installed CodeBlocks so I can compile with GNU GCC. – Ashley Jul 19 '10 at 18:54
3

You'll probably want to allocate the memory to store the array dynamically. Somewhere along the line of:

float *a;
int siz;    /* store user input here */

/* <snip> */

/* allocate a float array with `siz` elements */
a = (float *) malloc(sizeof(float) * siz);

NOTE: Adjust accordingly for two-dimensional arrays.

Santa
  • 11,381
  • 8
  • 51
  • 64