编写程序, 输入两个3行3列矩阵,对输入矩阵做相乘操作,用行指针实现。

要求:编写程序, 输入两个3行3列矩阵,对输入矩阵做相乘操作,用行指针实现。
效果:
在这里插入图片描述
代码:

#include "stdio.h"
void multiplication(int (*p)[3],int (*q)[3]);//矩阵相乘
void main()
{ 
 int a[3][3],b[3][3],i,j;  
 int (*p)[3]=a,(*q)[3]=b;
 printf("请输入a矩阵:\n");  
 for( i=0;i<3;i++)  
  for(j=0;j<3;j++)  
  {      
   scanf("%d",&a[i][j]);  
  }          
 printf("请输入b矩阵:\n"); 
 for(i=0;i<3;i++)  
  for(j=0;j<3;j++)  
  {      
   scanf("%d",&b[i][j]);  
  }  
 printf("矩阵a*b:\n"); 
 multiplication(p,q);
}
void multiplication(int (*p)[3],int (*q)[3])
{
 int i,j; 
 int k; 
 int sum; 
 for(i=0;i<3;i++) 
 {  
  for(j=0;j<3;j++)  
  {   
   sum=0;   
   for(k=0;k<3;k++)   
   {    
    sum=sum+(*(p[i]+k))*(*(q[j]+k));   
   }   
   printf("\t%d ",sum);  
  } 
  printf("\t\n"); 
 }
}
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据提供的引用内容,没有直接给出两个指针矩阵相乘的代码。但是可以根据引用中的矩阵读入函数和以下代码,实现两个指针矩阵相乘并输出结果: ```c++ #include <iostream> using namespace std; double** readMatrix(int* rows, int* cols) { // 读入矩阵数和数 scanf("%d %d", rows, cols); // 分配内存空间存储矩阵 double** matrix = (double**)malloc(*rows * sizeof(double*)); for (int i = 0; i < *rows; i++) { matrix[i] = (double*)malloc(*cols * sizeof(double)); } // 读入矩阵的元素 for (int i = 0; i < *rows; i++) { for (int j = 0; j < *cols; j++) { scanf("%lf", &matrix[i][j]); } } return matrix; } void printMatrix(double** matrix, int rows, int cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%lf ", matrix[i][j]); } printf("\n"); } } int main() { int rows1, cols1, rows2, cols2; double** matrix1 = readMatrix(&rows1, &cols1); double** matrix2 = readMatrix(&rows2, &cols2); if (cols1 != rows2) { printf("Error: The number of columns in the first matrix must be equal to the number of rows in the second matrix.\n"); return 0; } double** result = (double**)malloc(rows1 * sizeof(double*)); for (int i = 0; i < rows1; i++) { result[i] = (double*)malloc(cols2 * sizeof(double)); } for (int i = 0; i < rows1; i++) { for (int j = 0; j < cols2; j++) { result[i][j] = 0; for (int k = 0; k < cols1; k++) { result[i][j] += matrix1[i][k] * matrix2[k][j]; } } } printMatrix(result, rows1, cols2); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隔壁de小刘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值