用C ++计算矩阵 [英] Calculate matrix in C++

查看:105
本文介绍了用C ++计算矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个计算乘法2 * 2矩阵的代码,我是用C ++编写的,但现在想要它有类和函数。这是我写的代码:



I want to make a code that calculate the multiplied 2*2 matrix, I made it in C++ but now want it with classes and function. Here is the code I have written:

//this program will find the product of two matrices (2columns X 2rows)...
#include<iostream.h>
void main() 
{
	int a[4][4],b[4][4],c[4][4];
	int i,j,k,l;
	cout<<"Enter the value of the matrix a:\n";
	for(i=0;i<2;i++){
		for(j=0;j<2;j++){
			cin>>a[i][j];
		}
	}
	cout<<endl;
	cout<<"Enter the value of the matrix b:\n";
	for(i=0;i<2;i++){
		for(j=0;j<2;j++){
			cin>>b[i][j];
		}
	}

	cout<<"The multiplied value is:\n";
	for(i=0;i<2;i++){
		for(j=0;j<2;j++){
			c[i][j]=0;
			for(k=0;k<2;k++){
				c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
			}
		}
	}
	for(i=0;i<2;i++){
		for(j=0;j<2;j++){
			cout<<c[i][j]<<"  ";
		}
		cout<<"\n";
	}
}

推荐答案

创建一个带有构造函数或函数的Matrix类,它接受行数和列数并创建数组(通过 new )。添加一个计算产品的函数,并保存值或将其打印出来。如有必要,添加一个打印出结果的函数。为2x2矩阵执行此操作,并查看从那里扩展所需的内容。
Create a Matrix class with a constructor or function that takes the row and column counts and creates the arrays (via new). Add a function which calculates the product and either saves the values or prints them out. If necessary add a function that prints out the results. Do this for a 2x2 matrix and see what is needed to scale up from there.


除了解决方案1之外,您还可以在Boost库中使用Matrix类。



http:// www。 boost.org/doc/libs/1_39_0/libs/numeric/ublas/doc/matrix.htm [ ^ ]
Alternatively to solution 1, you can use the Matrix class in the Boost library.

http://www.boost.org/doc/libs/1_39_0/libs/numeric/ublas/doc/matrix.htm[^]


这篇关于用C ++计算矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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