Programa en C para multiplicar dos matrices usando punteros

Programa en C para multiplicar dos matrices usando punteros

Escriba un programa en C para multiplicar dos matrices usando punteros. Cómo ingresar y multiplicar dos matrices usando un puntero en programación C. Lógica para multiplicar dos matrices usando puntero en C.

Ejemplo

Entrada

Input elements of matrix1:
10 20 30
40 50 60
70 80 90
Input elements of matrix2:
1 2 3
4 5 6
7 8 9

Salida

Product of matrices is :
300 360 420
660 810 960
1020 1260 1500

Conocimiento requerido

Funciones, Matriz, Punteros, Puntero y Matrices

¿Cómo multiplicar dos matrices?

La multiplicación de dos matrices se define solo si las columnas de la primera matriz son iguales a las filas de la segunda matriz. La multiplicación de dos matrices se define por

¿Cómo multiplicar dos matrices usando punteros?

En publicaciones anteriores, aprendimos a acceder a una matriz multidimensional usando un puntero. Aquí, en esta publicación, continuaremos nuestro aprendizaje y aprenderemos a multiplicar dos matrices usando punteros.

La lógica de este programa no será diferente del programa para multiplicar dos matrices usando notación de matriz. En esta publicación, explicaré cómo convertir la notación de matriz de la multiplicación de matriz a la notación de puntero. Lo que ayudará a aumentar su conocimiento del puntero.

En notación de matriz para multiplicar dos matrices usamos sum += A[row][i] * B[i][col]; que en notación de puntero es equivalente a sum += (*(*(A + row) + i)) * (*(*(B + i) + col));

¿Programa para multiplicar dos matrices usando punteros?

/**
 * C program to multiply two matrix using pointers
 */

#include <stdio.h>

#define ROW 3
#define COL 3


/* Function declarations */
void matrixInput(int mat[][COL]);
void matrixPrint(int mat[][COL]);
void matrixMultiply(int mat1[][COL], int mat2[][COL], int res[][COL]);



int main()
{
    int mat1[ROW][COL];
    int mat2[ROW][COL];
    int product[ROW][COL];


    /*
     * Input elements in matrices.
     */
    printf("Enter elements in first matrix of size %dx%d\n", ROW, COL);
    matrixInput(mat1);

    printf("Enter elements in second matrix of size %dx%d\n", ROW, COL);
    matrixInput(mat2);


    // Call function to multiply both matrices
    matrixMultiply(mat1, mat2, product);


    // Print product of both matrix
    printf("Product of both matrices is : \n");
    matrixPrint(product);

    return 0;
}



/**
 * Function to input elements in matrix from user.
 *
 * @mat     Two-dimensional array to store user input.
 */
void matrixInput(int mat[][COL])
{
    int row, col;

    for (row = 0; row < ROW; row++)
    {
        for (col = 0; col < COL; col++)
        {
            scanf("%d", (*(mat + row) + col));
        }
    }
}




/**
 * Function to print elements in a two-dimensional array.
 *
 * @mat     Two-dimensional array to print.
 */
void matrixPrint(int mat[][COL])
{
    int row, col;

    for (row = 0; row < ROW; row++)
    {
        for (col = 0; col < COL; col++)
        {
            printf("%d ", *(*(mat + row) + col));
        }

        printf("\n");
    }
}




/**
 * Function to multiply two matrices.
 *
 * @mat1    First matrix
 * @mat2    Second matrix
 * @res     Resultant matrix to store product of both matrices.
 */
void matrixMultiply(int mat1[][COL], int mat2[][COL], int res[][COL])
{
    int row, col, i;
    int sum;


    for (row = 0; row < ROW; row++)
    {
        for (col = 0; col < COL; col++)
        {
            sum = 0;

            /*
             * Find sum of product of each elements of 
             * rows of first matrix and columns of second 
             * matrix.
             */
            for (i = 0; i < COL; i++)
            {
                sum += (*(*(mat1 + row) + i)) * (*(*(mat2 + i) + col));
            }


            /* 
             * Store sum of product of row of first matrix 
             * and column of second matrix to resultant matrix.
             */
            *(*(res + row) + col) = sum;
        }
    }
}

Salida

Enter elements in first matrix of size 3x3
10 20 30
40 50 60
70 80 90
Enter elements in second matrix of size 3x3
1 2 3
4 5 6
7 8 9
Product of both matrices is :
300 360 420
660 810 960
1020 1260 1500

Publicaciones recomendadas

  • Programa en C para copiar una matriz a otra usando punteros.
  • Programa en C para intercambiar dos arreglos usando un puntero.
  • Programa en C para invertir una matriz usando punteros.
  • Programa en C para buscar un elemento en una matriz usando punteros.
  • Programa en C para ordenar arreglos usando punteros.
  • Programa en C para sumar dos matrices usando punteros.