怎么做这个编程 [英] How to do this programming

查看:97
本文介绍了怎么做这个编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计算矩阵运算



给出在主函数中添加和乘以2个矩阵的代码

1.运行代码

2.调用Assign函数启动矩阵A作为随机整数[-3,3]

3.调用显示函数显示矩阵A

4 。调用Assign函数启动矩阵B作为随机整数[-4,4]

5.调用显示函数显示矩阵B

6.计算矩阵C = A +矩阵加法函数中的B

7.在显示功能中显示矩阵C

8.在主函数中:分配并显示矩阵D(i,j) = ij;

9.在矩阵乘法函数中计算矩阵E = CD

10.在显示功能中显示矩阵E



我尝试了什么:



To compute matrix operation

Given the code to add and multiply 2 matrices in the main function
1. Run the code
2. Call Assign function to initiate matrix A as random integers [-3, 3]
3. Call Display function display matrix A
4. Call Assign function to initiate matrix B as random integers [-4, 4]
5. Call Display function display matrix B
6. Compute the matrix C=A+B in an Matrix Addition function
7. Display the matrix C in a Display function
8. In the main function: Assign and display the matrix D(i, j) = i-j;
9. Compute the matrix E = CD in an Matrix Multiplication function
10. Display the matrix E in a Display function

What I have tried:

#include<iostream>
#include<time.h>
using namespace std;
#define m 4
#define n 5
#define p 6
/*To operate on an array of 2D matrix
/*To display indexes of a 2D matrix*/
/*To introduce a random number generator */

void main()
{	/*To declare and initialize an array A*/
	int A[m][n], B[m][n], C[m][n], D[n][p], E[m][p];

	clock_t wait;
		/*To assign matrix A as random integers [-3, 3] */
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
			A[i][j] = (rand() % 5) - 3;
	}
	/*To display an array of matrix A*/
	cout << "matrix A \n";
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout << A[i][j] << "\t";
			wait = clock();
			while (clock() < wait + 200);
		}	cout << "\n";
	}cout << "\n";

	/*To assign matrix B as random intergers [-4,4] */
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
			B[i][j] = (rand()%5)-4;
	}
	/*To display an array of matrix B*/
	cout << "matrix B \n"; 
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout << B[i][j] << "\t";
			wait = clock();
			while (clock() < wait + 200);
		}	cout << "\n";
	}cout << "\n";


	/*To compute matrix addition C = A+B,
	C(i,j) = dotproduct(row i of A,column j of B) */
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			C[i][j] = A[i][j] + B[i][j];
		}
	}
	/*To display an array of matrix C*/
	cout << "matrix C \n"; 
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cout << C[i][j] << "\t";
			wait = clock();
			while (clock() < wait + 200);
		}	cout << "\n";
	}cout << "\n";

	/*To assign matrix D(i,j)=i-j using for loop*/
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < p; j++)
			D[i][j] = i - j;
	}
	/*To display an array of matrix D*/
	cout << "matrix D \n"; 
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < p; j++)
		{
			cout << D[i][j] << "\t";
			wait = clock();
			while (clock() < wait + 200);
		}	cout << "\n";
	}cout << "\n";

	/*To compute matrix multiplication E = CD,
	E(i,j) = dotproduct(row i of C,column j of D) */
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < p; j++)
		{
			E[i][j] = 0;
			for (int k = 0; k < n; k++)
				E[i][j] = E[i][j] + A[i][k] * D[k][j];
		}
	}
	/*To display an array of matrix E*/
	cout << "matrix E \n"; 
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < p; j++)
		{
			cout << E[i][j] << "\t";
			wait = clock();
			while (clock() < wait + 200);
		}	cout << "\n";
	}cout << "\n";
	system("PAUSE");
}

推荐答案

我试过的是为了解释你实际做了什么,除了试着找一个网站为我做功课。



我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但是我们不会为你做这一切!
"What I have tried" is intended for you to explain what you have actually done, other than "try to find a website to do my homework for me".

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


你有一个非常清晰的任务列表,该怎么做。和你的任务一样2.

You have a very clear task list, what to do. Like your task 2.
int A[m][n], B[m][n], C[m][n], D[n][p], E[m][p];// must be global for now
void AssignMatrix3();// above and outside main (prototype)


主要实施后的



after main the implementation

void AssignMatrix3()
{
  //your init of the matrix
}

如果你需要更多的知识,可以深入了解学习C ++ 教程。

提示:进行一些输出并使用调试器单步模式。

If you need some more knowledge that take a dive into the Learn C++ tutorial.
Tip: make some output and use the debugger single step mode.


这篇关于怎么做这个编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆