实现矩阵乘法:#include <stdio.h>
main(){
int i,j,m,k,a[3][4]={1,2,3,4,2,3,4,5,3,4,5,6},b[4][3]={1,2,3,2,3,4,3,4,5,4,5,6},c[3][3];
//请书写你的代码
for(i=0; i<3; i++){
for(j=0; j<3; j++)
printf("%5d",c[i][j]);
printf("\n");
}
}
c
#include <stdio.h>
int main() {
int i, j, m, k;
// 定义矩阵a
int a[3][4] = {
{1, 2, 3, 4},
{2, 3, 4, 5},
{3, 4, 5, 6}
};
// 定义矩阵b
int b[4][3] = {
{1, 2, 3},
{2, 3, 4},
{3, 4, 5},
{4, 5, 6}
};
// 矩阵乘法结果矩阵c
int c[3][3];
// 矩阵乘法实现
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
c[i][j] = 0;
for (k = 0; k < 4; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
// 输出结果矩阵c
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
}
#include <stdio.h>
int main()
{
int m, n, p, q, i, j, k, sum;
int a[10][10], b[10][10], c[10][10];
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the elements of first matrix\n");
for (i=0;i<m;i++)
for (j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d",&p,&q);
if (n!=p)
printf("Matrices cannot be multiplied\n");
else
{
printf("Enter the elements of second matrix\n");
for (i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
// Matrix multiplication
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
printf("Product of the matrices:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
return 0;
}
#include <stdio.h>
int main() {
int i, j, k, m;
int a[3][4] = {{1, 2, 3, 4},
{2, 3, 4, 5},
{3, 4, 5, 6}};
int b[4][3] = {{1, 2, 3},
{2, 3, 4},
{3, 4, 5},
{4, 5, 6}};
int c[3][3] = {0};
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
m = 0;
for (k = 0; k < 4; k++) {
m += a[i][k] * b[k][j];
}
c[i][j] = m;
printf("%5d", c[i][j]);
}
printf("\n");
}
return 0;
}
为你优化代码,希望对你有帮助,望采纳
int main()
{
int a = 0;
int b = 0;
int c = 0;
int ciao = 0;//ciao 是笔者随意取的一个中间变量
scanf(“%d %d %d”, &a, &b, &c);
if (a < b || a ==b)
{
ciao = a;
a = b;
b = ciao;
}
if (a < c || a == c)
{
ciao = a;
a = c;
c = ciao;
}
if (b < c || b == c)
{
ciao = b;
b = c;
c = ciao;
printf(“%d %d %d”, a, b, c);
}
else
{
printf(“%d %d %d”, a, b, c);
}
return 0;`
实现矩阵乘法需要以下几个步骤:
以下是示例C语言代码:
#include <stdio.h>
#define ROW 2
#define COL 3
void matrixMultiply(int A[ROW][COL], int B[COL][ROW], int C[ROW][ROW])
{
int i,j,k;
for(i=0; i<ROW; i++)
{
for(j=0; j<ROW; j++)
{
int sum = 0;
for(k=0; k<COL; k++)
{
sum += A[i][k] * B[k][j];
}
C[i][j] = sum;
}
}
}
void printMatrix(int M[ROW][ROW])
{
int i,j;
for(i=0; i<ROW; i++)
{
for(j=0; j<ROW; j++)
{
printf("%d ", M[i][j]);
}
printf("\n");
}
}
int main()
{
int A[ROW][COL] = {{1,2,3}, {4,5,6}};
int B[COL][ROW] = {{7,8}, {9,10}, {11,12}};
int C[ROW][ROW];
matrixMultiply(A, B, C);
printf("Result Matrix:\n");
printMatrix(C);
return 0;
}
在以上代码中,我们首先定义了两个矩阵A和B,然后根据矩阵乘法的公式,定义了结果矩阵C。函数matrixMultiply就是计算C的函数,函数printMatrix则是打印矩阵的函数。在主函数中,我们调用了matrixMultiply计算结果矩阵C,并打印出来。
运行结果如下:
Result Matrix:
58 64
139 154
其中,矩阵A为:
1 2 3
4 5 6
矩阵B为:
7 8
9 10
11 12
最终结果矩阵C为:
58 64
139 154
至此,我们已经成功实现了矩阵乘法的代码。