X_GeQAQ 2021-06-03 00:38 采纳率: 84.6%
浏览 47
已采纳

数据结构第一节课的作业,应该不难,但是我没懂,求大神帮忙做一下

In this assignment you are asked to write a program that reads in two matrixes of size n x m and s x t and then it outputs the resulting multiplication of those two matrixes.   Since sizes of matrices are not known in advance, you need to implement a dynamic memory allocation scheme for matrices. Your program should prompt the user for n, m, s, t, and the elements of each matrix. After that, if the multiplication can be performed on the matrices, your program should output each matrix and the result of the matrix multiplication.

 

Recall that if the matrix A is of size n x m, and matrix B is of size m x t, the resulting matrix C would be of size n x t.  However, if a matrix is n x m and the other is s x t, these matrices cannot be multiplied if m is not equal to s. 

 

If you are not familiar with the matrix multiplication problem, study the following example in order to find a general solution for the multiplication of two-dimensional matrices. In this example, matrices A and B are of size 3 x 3. An entry Xij indicates an  element X[i][j].

 

     A00    A01   A02                                  B00      B01      B02

      A =             A10    A11   A12 B =         B10      B11      B12

A20    A21   A22                                  B20      B21      B22

 

 

Resulting multiplication of the matrices A and B is equal to

 

              A00 * B00 + A01 * B10 + A02 * B20         A00*B01 + A01 * B11 + A02*B21          A00*B02 + A01 *B12 + A02 *B22

C    =           A10 * B00 + A11 * B10 + A12 * B20          A10*B01 + A11 * B11 + A12*B21          A10*B02 + A11 *B12 + A12 *B22

  A20 * B00 + A21 * B10 + A22 * B20          A20*B01 + A21 * B11 + A22*B21          A20*B02 + A21 *B12 + A22 *B22

 

To solve the problem, you need to determine how to obtain an entry, say C[i][k], from the entries of matrices A and B. Once you figure this out, the programming will be extremely easy task. Warning; before you start to program, test your solution.

 

  • 写回答

2条回答 默认 最新

  • qfl_sdu 2021-06-03 01:14
    关注

    矩阵相乘的问题,代码如下,如有帮助,请采纳一下,谢谢

    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int m,n,s,t;
    	int i,j,k;
    	float** arr;
    	float** brr;
    	float** crr;
    	cout << "请输入矩阵A的大小m和n:";
    	cin >> m;
    	cin >> n;
    	cout << "请输入矩阵B的大小s和t:";
    	cin >> s;
    	cin >> t;
    	if(n != s)
    	{
    		cout << "n != s" << endl;
    		return 0;
    	}
    	cout << "请输入矩阵A:" << endl;
    	arr = new float*[m];
    	for (i=0; i < m;i++ )
    	{
    		arr[i] = new float[n];
    		for(j = 0;j < n;j++)
    			cin >> arr[i][j];
    	}
    
    	cout << "请输入矩阵B:" << endl;
    	brr = new float*[s];
    	for (i=0; i < s;i++ )
    	{
    		brr[i] = new float[t];
    		for(j = 0;j < t;j++)
    			cin >> brr[i][j];
    	}
    
    	crr = new float*[m];
    	for (i = 0; i < m;i++)
    	{
    		crr[i] = new float[t];
    		for (j = 0;j < t;j++)
    		{
    			crr[i][j] = 0;
    			for(k = 0; k < n; k++)
    				crr[i][j] += arr[i][k] * brr[k][j];
    		}
    	}
    	cout << "A*B=" << endl;
    	for (i = 0; i < m; i++)
    	{
    		for(j = 0;j < t;j++)
    			cout << crr[i][j] << " ";
    		cout << endl;
    	}
    
    	for (i = 0; i < m;i++)
    	{
    		delete[] arr[i];
    	}
    	delete[] arr;
    	for (i = 0; i < s;i++)
    	{
    		delete[] brr[i];
    	}
    	delete[] brr;
    	for (i = 0; i < m;i++)
    	{
    		delete[] crr[i];
    	}
    	delete[] crr;
    	return 0;
    
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度