0
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define ROWS 2
#define COLS 3
#define ROW 3
#define COL 4
int main(void) 
{

    int e, f;

    srand((unsigned)time(NULL));

    int A[ROWS][COLS];

    int a; int b;
    for (a = 0; a < ROWS; ++a)
    {
        for (b = 0; b < COLS; ++b)
        {
            printf("%d ", A[a][b] = rand() % 9 + 1);

        }
        printf("\n");
    }

    printf("-------------------------\n");

    int B[ROW][COL];

    int c; int d;
    for (c = 0; c < ROW; ++c)
    {
        for (d = 0; d < COL; ++d)
        {
            printf("%d ", B[c][d] = rand() % 9 + 1);
        }

        printf("\n");
   }

   return 0;
}

this code using rand function. make 2d array. [int A, int B]

I want multiplication using random value in 2d array.

i want to make another 2d array. and int A * int B ex) int C= int A*int B (2d array)

hoon
  • 5
  • 4
  • sorry.. i want make int A(2d array) * int B( 2d array) – hoon Oct 09 '18 at 14:13
  • Are you asking how to perform a 2D matrix multiplication? (i.e. _[AxB](https://www.mathsisfun.com/algebra/matrix-multiplying.html)_ ?) – ryyker Oct 09 '18 at 14:18
  • yes. i'm sorry, be bad at communicating – hoon Oct 09 '18 at 14:21
  • Read the link in my last comment. It illustrates matrix multiplication, with examples. Just use the algorithms there to write your C code. There is no standard C library for matrix multiplication, although there are _[these](https://stackoverflow.com/questions/4501322/c-libraries-for-mathematical-matrix-operations)_. – ryyker Oct 09 '18 at 14:30

2 Answers2

0

You can find a huge amount of C examples for matrix multiplication on the net, so my first advice is to do some scouting before!

Anyhow a simple solution would be:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define ROWS 2
#define COLS 3
#define ROW 3
#define COL 4
int main(void) 
{
    int A[ROWS][COLS];
    int B[ROW][COL];
    int C[ROWS][COL];
    int i, j, k;

    srand((unsigned)time(NULL));

    for (i = 0; i < ROWS; ++i)
    {
        for (j = 0; j < COLS; ++j)
        {
            printf("%d ", A[i][j] = rand() % 9 + 1);
        }
        printf("\n");
    }

    printf("-------------------------\n");

    for (i = 0; i < ROW; ++i)
    {
        for (j = 0; j < COL; ++j)
        {
            printf("%d ", B[i][j] = rand() % 9 + 1);
        }
        printf("\n");
    }

    printf("-------------------------\n");

    if (COLS != ROW) {
        printf("Unable to multiple the 2 matrixes!\n");
        return -1;
    }

    for (i = 0; i < ROWS; i++) {
        for (j = 0; j < COL; j++) {
            int tmp = 0;
            for (k = 0; k < COLS; k++) {
                tmp += A[i][k] * B[k][j];
            }
            printf("%d ", C[i][j] = tmp);
        }
        printf("\n");
    }

    return 0;
}

Of course it is required to check that the number of columns of the first matrix matches the number of rows of the second.

I've also simplified some variables that you used since they were not necessary.

0

From this page describing matrix multiplication, there is this illustration for a 2D matrix: (you will need to extrapolate for additional orders of matrix dimensions)

enter image description here

In C code, a straight forward implementation of matrix multiplication follows: (assuming A and B exist, and are populated similar to those in your code example, but limit to 2X2 matrix order.)

int C[2][2];

C[0][0] = A[0][0]*B[0][0]+
          A[0][1]*B[1][0]
C[0][1] = A[0][0]*B[0][1]+
          A[0][1]*B[1][1]
C[1][0] = A[1][0]*B[0][0]+
          A[1][1]*B[1][0]
C[1][1] = A[1][0]*B[0][1]+
          A[1][1]*B[1][1]  

This implementation includes no loops, and it is not a solution for the general case. However, the indices for A & B when arranged in this fashion reveal patterns that are suggestive, and may help to see ways of creating steps for a general solution for 2D matrix multiplication.

ryyker
  • 22,849
  • 3
  • 43
  • 87