ポインターを使用して 2 つの行列を乗算する C プログラムを作成します。 C プログラミングでポインターを使用して 2 つの行列を入力して乗算する方法。 C でポインターを使用して 2 つの行列を乗算するロジック。
例
入力
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
出力
Product of matrices is : 300 360 420 660 810 960 1020 1260 1500
必要な知識
関数、配列、ポインター、ポインター、および配列
2 つの行列を乗算する方法
2 つの行列の乗算は、最初の行列の列が 2 番目の行列の行と等しい場合にのみ定義されます。 2 つの行列の乗算は次のように定義されます
ポインタを使用して 2 つの行列を乗算する方法
以前の投稿で、ポインターを使用して多次元配列にアクセスする方法を学びました。この投稿では、さらに学習を続け、ポインターを使用して 2 つの行列を乗算する方法を学習します。
このプログラムのロジックは、配列表記を使用して 2 つの行列を乗算するプログラムと何ら変わりはありません。この投稿では、行列乗算の配列表記をポインター表記に変換する方法を説明します。これは、ポインターの知識を高めるのに役立ちます。
2 つの行列を乗算する配列表記では、sum += A[row][i] * B[i][col];
を使用します ポインター表記では sum += (*(*(A + row) + i)) * (*(*(B + i) + col));
に相当します
ポインタを使用して 2 つの行列を乗算するプログラム?
/**
* 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;
}
}
}
出力
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
おすすめの投稿 - ポインターを使用してある配列を別の配列にコピーする C プログラム
- ポインタを使用して 2 つの配列を交換する C プログラム
- ポインタを使用して配列を反転する C プログラム
- ポインターを使用して配列内の要素を検索する C プログラム
- ポインターを使用して配列をソートする C プログラム
- ポインタを使用して 2 つの行列を加算する C プログラム