0
 int main()
{
int n = 0;
int i = 0;
int j = 0;
int k = 0;
int **x;
int **y;
int **z;


printf("Enter size of matrix - (N x N):");
scanf("%d", &n);

x = malloc(n * sizeof(int *));
y = malloc(n * sizeof(int *));
z = malloc(n * sizeof(int *));  


for(i = 0; i < n; i++)
{
    x[i] = malloc(n * sizeof(int));
    y[i] = malloc(n * sizeof(int));
    z[i] = malloc(n * sizeof(int));
}


for(i = 0; i < n; i++)
{
    for(j = 0; j < n; j++)
    {
        x[i][j] = (rand()% 10) + 1;
        y[i][j] = (rand()% 10) + 1;
    }
}


for(i = 0; i < n; i++)
{
    printf("\n");
    for(j = 0; j < n; j++)
    {
        printf("%2d ", x[i][j]);
        printf("%2d ", y[i][j]);
    }
}


for(i = 0; i < n; i++)
{
    printf("\n\n");
    for(j = 0; j < n; j++)
    {
        for(k = 0; k < n; k++)
        {
            z[i][j] += x[i][k] * y[k][j];
        }
    printf("%2d ", z[i][j]);
    }
}
}

So I'm trying to do a matrix multiply where the 2 matrices are always 2x2 or 3x3 etc. I want to fill the 2 matrices with random values and do a multiply. My issue is that my third matrix which is storing the answer is producing wrong values. For example, my first 2 matricies (for 2x2) hold values [4, 7, 4, 6] and [8, 6, 7, 3] and my output is [76, 48, 70, 45] when really it should be [81, 45, 74, 42]. Dont know what is going wrong?

Ryan
  • 29
  • 1
  • 8
  • 1
    Have a look at http://stackoverflow.com/q/1083658 for some guidance on how to properly set up multi-dimensional/jagged arrays in C. See also https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm – Robert Harvey Feb 02 '17 at 18:04
  • @RobertHarvey, how does any of that apply when the dimensions of the array are not known until runtime, as in the the OP's case? Or at least, what do you think he's doing wrong? – John Bollinger Feb 02 '17 at 18:07
  • 1
    Don't use pseudo arrays that are simulated with pointers to pointers. C has genuin support for real 2D matrices. Just do `double (*A)[n] = malloc(sizeof(double[n][n]));` – Jens Gustedt Feb 02 '17 at 18:24

1 Answers1

2

When you allocate memory with malloc(), the space you get is not automatically initialized. It can contain any values. You fill your x and y matrices with random values, but you use the elements of z just as they come from malloc(). You want to set them to zero before the main part of your multiplication. You can do that by allocating them with calloc() in the first place, or you could just set them to zero in the same loop nest where you set the initial x and y values.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Additionally, the format in which you output the X and Y matrices looks extremely confusing. It would be worth your while to make it clearer, such as by printing X and Y separately, in matrix-like form. – John Bollinger Feb 02 '17 at 18:23
  • No, I don't think so. z array is being assigned in the last nested `for` loop. – Robert Harvey Feb 02 '17 at 18:24
  • @RobertHarvey, no, not exactly. The `z` array is being *plus*signed in the last loop nest (`z[i][j] += x[i][k] * y[k][j];`). The values in `z` before the multiplication loop therefore matter. – John Bollinger Feb 02 '17 at 18:36
  • In any case, the OP's code works for me after I fix the initialization problem I described. – John Bollinger Feb 02 '17 at 18:39
  • can you should me how your initializing it? @JohnBollinger – Ryan Feb 02 '17 at 18:55
  • @JohnBollinger i initialize z by z[i][j] = 0; in the loop with the others and it still does not work? – Ryan Feb 02 '17 at 19:43
  • @Ryan it works for me. Consider fixing the output of your X and Y matrices as I suggested in my first comment on this answer. It may be that you're being confused by the output format, and the matrices you're starting with are not what you think they are. – John Bollinger Feb 02 '17 at 19:52