c++

i wrote a code but take a false result. how i do for its run. i have to use function in class.
its 4x4 matrix multiplication

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 #include<iostream>
 
using namespace std;

int i,j,k,resultt;
float a[10][10],b[10][10];

class result {
	public:
	//	int a[10][10],b[10][10],mlt[10][10],i,j;
		
		float mlt[10][10];
		
		
	float multipication_matrices()
	{
		
 /*	int i,j,k,result,mult;
	 for(i = 0; i < 4; ++i)
       for(j = 0; j < 4; ++j)
        {
        mlt[i][j]=0;
       }	*/
       
		for(i = 0; i < 4; ++i)
        for(j = 0; j < 4; ++j)
            for(k = 0; k < 4; ++k)
            {
			
            resultt = mlt[i][j] = a[i][k] * b[k][j];
              }		
	
		return   resultt;}
	
	
}r;

int main()
{
 	

	
		cout << "Enter elements of matrix 1:" << endl;
    for(i = 0; i < 4; ++i)
        for(j = 0; j < 4; ++j)
        {
            cout << "Enter element a" << i + 1 << j + 1 << " : ";
            cin >> a[i][j];
        }

   
    cout << "Enter elements of matrix 2:" << endl;
    for(i = 0; i < 4; ++i)
        for(j = 0; j < 4; ++j)
        {
            cout << "Enter element b" << i + 1 << j + 1 << " : ";
            cin >> b[i][j];
        }
   
     
    cout << endl << "Output Matrix: " <<  endl;
    for(i = 0; i < 4; ++i)
    for(j = 0; j < 4; ++j)
    { 
        cout << " " << r.multipication_matrices();
        if(j == 4-1)
            cout << endl;
    }
        
	return 0;
	}





Last edited on
There are, unfortunately, a lot of things wrong here... (I’m so sorry).

The point of the matrix class is to encapsulate everything that has to do with a matrix. That way your main function can look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
  cout << "Enter the size of the matrix as rows and columns, followed by the elements.\n";
  cout << "For example, a 2 by 3 matrix could be entered as:\n";
  cout << "  2 3\n";
  cout << "  1 2 3\n";
  cout << "  4 5 6\n";
  cout << "\n";

  matrix A;
  cout << "Please enter matrix A: ";
  cin >> A;

  matrix B;
  cout << "Please enter matrix B: ";
  cin >> B;

  matrix C = A * B;

  cout << "A * B is: " << C << "\n";
}

To get to this happy coolness, you need to do a number of things.


First, the matrix class should have the dimensions and the elements array.

1
2
3
4
5
struct matrix
{
  int rows;
  int columns;
  double elements[MAX_ROWS][MAX_COLUMNS];

You can declare those MAX_ROWS and MAX_COLUMNS constants anywhere. I would make them part of the class.

1
2
3
4
5
6
7
8
struct matrix
{
  constexpr static int MAX_ROWS = 10;
  constexpr static int MAX_COLUMNS = 10;

  int rows;
  int columns;
  double elements[MAX_ROWS][MAX_COLUMNS];



Next, you should have a constructor for your matrix.

1
2
3
4
5
6
7
  matrix( int m, int n ): rows(m), columns(n)
  {
    // I personally recommend you set all the elements of the matrix to zero
    for (int i = 0; i < rows; ++i)
      for (...)
        ...
  }

Setting the elements to zero here in the constructor makes your life easier when you write the multiplication. If you don’t do it here, you must do it there.


The function to multiply matrices can either be a member or non-member function (inside or outside the class). Since multiplying matrices produces a new matrix from two source matrices, I recommend you make it non-member.

1
2
3
4
5
6
7
8
9
10
11
12
13
}; // end of matrix class

matrix operator * ( const matrix& a, const matrix& b )
{
  matrix result( a.rows(), b.columns() );
  // Remember that result must now be a a.rows() by b.columns() matrix with all elements
  // equal to zero. If you didn’t do that in the constructor, you must do it here now.

  // Make sure you do the multiplication correctly. 
  // https://www.mathsisfun.com/algebra/matrix-multiplying.html

  return result;
}

You can make it a member function if you wish. Just be aware that a few things will change

1
2
3
4
  matrix operator * ( const matrix& b )
  {
    matrix result( this->rows(), b.columns() );
    ...



Next you’ll want to create a function to read a matrix from the user. I’ll give it to you as a freebee:

1
2
3
4
5
6
7
8
9
10
istream& operator >> ( istream& ins, matrix& m )
{
  ins >> m.rows >> m.columns;

  for (int i = 0; i < m.rows;    ++i)
  for (int j = 0; j < m.columns; ++j)
    ins >> m.elements[i][j];

  return ins;
}


Finally you’ll want a function to print a matrix.
1
2
3
4
5
6
ostream& operator << ( ostream& outs, const matrix& m )
{
  // You must figure this out yourself. Use 'outs' instead of 'cout'.

  return outs;
}


So far we have ignored a number of important things, like error checking. Personally, I would also add checking to make sure that:

  • the user cannot make a matrix with too many rows and columns
    check this in the constructor and the input function

  • A.columns() == B.rows() when multiplying
    check this in the multiplication function

The simplest way for you to do that is to just throw if something is wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
istream& operator >> ( istream& ins, matrix& m )
{
  ins >> m.rows >> m.columns;

  if ((m.rows > m.MAX_ROWS) || (m.columns > m.MAX_COLUMNS))
    throw "matrix too big";

  for (int i = 0; i < m.rows;    ++i)
  for (int j = 0; j < m.columns; ++j)
    ins >> m.elements[i][j];

  return ins;
}

And you can complain properly to the user by wrapping the code in main with a try..catch block:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
  try {

    // all the normal stuff in main goes here

  }
  catch (const char* msg)
  {
    cerr << msg << "\n";
    return 1;
  }
}


Hope this helps.
thanks for help but " i have to use function in class."
Topic archived. No new replies allowed.