c语言:输入矩阵2行3列的矩阵A和矩阵3行4列的矩阵B,计算矩阵的乘积
1个回答
展开全部
#include <stdio.h>
#include <stdlib.h>
#define L 2
#define M 3
#define N 4
void genArr(double *Arr, int row, int col)
{
int i = 0;
int n = row * col;
srand(time(NULL));
for (i = 0; i < n; ++i)
{
Arr[i] = rand() % 10;
}
}
void printMatrix(double *Arr, int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; ++i)
{
for (j = 0; j < col; ++j)
{
printf("%lf\t", *(Arr + i * col + j));
}
printf("\n");
}
return;
}
int main(void)
{
double A[L][M] = {{0}};
double B[M][N] = {{0}};
double C[L][N] = {{0}};
int i = 0, j = 0, k = 0;
printf("\nGenerate A[%d][%d]:\n", L, M);
genArr(&A[0][0], L, M);
printMatrix(&A[0][0], L, M);
printf("\nGenerate B[%d][%d]:\n", M, N);
genArr(&B[0][0], M, N);
printMatrix(&B[0][0], M, N);
for (i = 0; i < L; ++i)
{
for (k = 0; k < N; ++k)
{
C[i][k] = 0;
for (j = 0; j < M; ++j)
{
C[i][k] += A[i][j] * B[j][k];
}
}
}
printf("\nCalc C[%d][%d]:\n", L, N);
printMatrix(&C[0][0], L, N);
return 0;
}
#include <stdlib.h>
#define L 2
#define M 3
#define N 4
void genArr(double *Arr, int row, int col)
{
int i = 0;
int n = row * col;
srand(time(NULL));
for (i = 0; i < n; ++i)
{
Arr[i] = rand() % 10;
}
}
void printMatrix(double *Arr, int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; ++i)
{
for (j = 0; j < col; ++j)
{
printf("%lf\t", *(Arr + i * col + j));
}
printf("\n");
}
return;
}
int main(void)
{
double A[L][M] = {{0}};
double B[M][N] = {{0}};
double C[L][N] = {{0}};
int i = 0, j = 0, k = 0;
printf("\nGenerate A[%d][%d]:\n", L, M);
genArr(&A[0][0], L, M);
printMatrix(&A[0][0], L, M);
printf("\nGenerate B[%d][%d]:\n", M, N);
genArr(&B[0][0], M, N);
printMatrix(&B[0][0], M, N);
for (i = 0; i < L; ++i)
{
for (k = 0; k < N; ++k)
{
C[i][k] = 0;
for (j = 0; j < M; ++j)
{
C[i][k] += A[i][j] * B[j][k];
}
}
}
printf("\nCalc C[%d][%d]:\n", L, N);
printMatrix(&C[0][0], L, N);
return 0;
}
追问
可以短一点吗
追答
不可以的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询