使用多维数组在c ++中创建2 * 2矩阵乘法程序? [英] Creating a 2*2 matrix multiplication program in c++ using Multidimensional arrays?

查看:134
本文介绍了使用多维数组在c ++中创建2 * 2矩阵乘法程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用c ++创建一个2 * 2矩阵乘法程序,我尝试了以下方法。虽然我的代码给了我正确的结果,但我不相信我的代码是一个很好的代码,我觉得它是一种非常天真的编写2 * 2矩阵乘法程序的方法。有没有其他方法可以尝试完成我的程序?请建议我!谢谢



I was trying to create a 2*2 matrix multiplication program in c++ and i tried the following approach. Though my code gives me correct results but i am not convinced that my code is a good code and i feel that its a very naive way of writing a 2*2 matrix multiplication program. Is there any other way i can try to accomplish my program? Please suggest me! Thanks

#include <iostream>

using namespace std;

int main()
{
  int a[2] [2];
  int b[2] [2];
  int c[2] [2];
 
  int i,j;
  int k = 0;

  for( i = 0; i <2; i ++)
  {
    for( j = 0; j<2; j++)
    {
      cout<<"Enter your values"<<endl;
      cin>>a[i][j];
    }
  }

  for( i = 0; i <2; i ++)
  {
    for( j = 0; j<2; j++)
    {
      cout<<a[i][j];
    }
    cout<<endl;
  }

  for( i = 0; i <2; i ++)
  {
    for( j = 0; j<2; j++)
    {
      cout<<"Enter your values"<<endl;
      cin>>b[i][j];
    }
  }

  for( i = 0; i <2; i ++)
  {
    for( j = 0; j<2; j++)
    {
      cout<<b[i][j];
    }
    cout<<endl;
  }

  for(i = 0; i<2; i++)
  {
    for(int k= 0; k<2; k++)
    {
      if(i==0 && k == 0)
      {
        c[0] [0] = a[0] [0] * b[0] [0] + a[0] [1] * b [1][0] ;
      }

      if( i==0 && k==1)
      {
        c[0] [1] = a[0] [0] * b[0] [1] + a[0][1] *b[1] [1] ;
      }

      if(i==1 && k ==0)
      {
        c[1][0] = a[1] [0] * b[0] [0] + a[1] [1] * b[1] [0] ;
      }

      if(i == 1 && k == 1 )
      {
        c[1] [1] = a[1] [0] * b[0] [1]  + a[1] [1] * b[1] [1] ;
      } 
    }
  } 

  for(int i = 0; i <2; i ++)
  {
    for(int j = 0; j<2; j++)
    {
      cout<<c[i][j];
    }
    cout<<endl;
  }
   
  system("pause");
}

推荐答案

我在对这个问题的评论中解释了我的建议。将它抽象到某些类中,最重要的是,从代码中删除任何I / O是第一件事。

还有什么?那么,对矩阵/向量元素使用整数类型几乎没有用处。将它更改为 double ,或者更好的是,使用C ++模板抽象出元素类型。



换句话说,您需要从纯ad-hoc代码迁移到更通用的库。它会实用吗?是的,当然。即使您只有一个应用程序进行此类计算。 (我没有考虑使用一些可用的数字库;这是一个完全不同的故事。)



-SA
I explained my advice in my comment to the question. Abstracting it out to some classes and, most importantly, get rid of any I/O from the code is the first thing.
What else? Well, using integer type for matrix/vector elements has very little use. Change it to double or, even better, abstract out the element type using C++ templates.

In other words, you need to migrate from pure ad-hoc code to some much more universal library. Would it be practical? Yes, most certainly. Even if you have only one application for this type of calculations. (I did not consider using some available numeric library; this is a whole different story.)

—SA


你的程序中的主要缺陷是你结合了两种解决问题的方法:



a)嵌套循环

b)拼写每一次操作都需要



以上其中一种就足够了。例如,取出嵌套循环,你将得到:

The main flaw in your program is that you combine two approaches to the problem:

a) nested loops
b) spelling out every single operation

One of the above would be sufficient. For example, take the nested loops away and you will have:
c[0][0] = a[0][0] * b[0][0] + a[0][1] * b[1][0];
c[0][1] = a[0][0] * b[0][1] + a[0][1] * b[1][1];
c[1][0] = a[1][0] * b[0][0] + a[1][1] * b[1][0];
c[1][1] = a[1][0] * b[0][1] + a[1][1] * b[1][1];



一切你需要的。事实上,只要你知道你总是处理一个二维数组甚至是最快的方法,并且许多库例程都以类似的方式编写。



另一种选择是使用嵌套循环并通过在另一个嵌套循环中对产品求和来计算内循环c [i] [j]。试试看吧。这也是一个有效的解决方案。它可以很容易地扩展到更大的矩阵。


which does all you need. In fact, as long as you know that you are always dealing with a 2-dimensional array that is even the fastest approach and many library routines are written in a similar way.

The other alternative is use a nested loop and calculate in the inner loop c[i][j] by summing up the products in a further nested loop. Just try it as an exercise. That's also a valid solution. It can easily be extended to larger matrices.


其他解决方案中的建议是正确的。

你应该学习使用对象和类,所以课程描述了你想要的东西,这次是一个矩阵和方法,例如Multiply。



另外,使用这个网站:http://www.cplusplus.com



The suggestions in other solutions is correct.
You should learn about using objects and classes, so the class describes the thing you want, this time a Matrix and method e.g Multiply.

Also, use this site: http://www.cplusplus.com

class Matrix
{
public:
    Matrix(double A[][], int m, int n)
    {
//initialise
        width = m;
        height = n;

        // http://www.cplusplus.com/forum/articles/7459/
        ppValues = new double*[HEIGHT];
        for (int i = 0; i < HEIGHT; ++i)
            ppValues[i] = new double[WIDTH];
    }

    ~Matrix(){}

// return 0 if valid, else 1
    int Multiply(Matrix B, Matrix &C)
    {

    }

private:
    int width,height;
    double **ppValues;
};


这篇关于使用多维数组在c ++中创建2 * 2矩阵乘法程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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