Help with this task

Nov 13, 2020 at 8:55pm
I can't understand this task:
for two square matrices, create a third matrix, the elements of which should be equal to the product of the elements of the corresponding row of the first matrix and the largest element in the column of the second matrix with the same number as the row of the first matrix.
Nov 13, 2020 at 10:32pm
Hello chebyrek,

It is not easy to rearrange the instructions to make more sense, but what I get would translate to something like:
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
constexpr int MAXROW{ 4 }, MAXCOL{ 4 };

int matrix1[MAXROW][MAXCOL]
{
	{1, 2, 3, 4},
	{5, 6, 7, 8},
	{9, 10, 11, 12},
	{13, 14, 15, 16}
};

int matrix2[MAXROW][MAXCOL]
{
	{1, 4, 3, 2},
	{8, 6, 7, 5},
	{9, 12, 11, 10},
	{13, 14, 16, 15}
};

int matrix3[MAXROW][MAXCOL]{};

matrix3[0][0] = matrix1[0][0] + matrix2[0][1];

// Or maybe it means:

matrix3[0][0] = matrix1[0][0] + matrix2[3][0];

// And the next would be:

matrix3[0][0] = matrix1[0][1] + matrix2[3][1];


Just an idea for now. I will have to think about more.

Andy
Nov 13, 2020 at 11:03pm
Thanks for the help, Bro!
Nov 13, 2020 at 11:45pm
you need to use the row from the first matrix and multiply by the largest number from the column from the second matrix the result is the first row of the third matrix. so you need to do as many times as many rows in the first matrix
Nov 14, 2020 at 9:45am
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
#include <iostream>
#include <valarray>
#include <string>
using namespace std;

using matrix = valarray< valarray<double> >;

void print( string title, const matrix &M )
{
   cout << title << '\n';
   for ( const auto &row : M )
   {
      for ( auto e : row ) cout << e << '\t';
      cout << '\n';
   }
}

int main()
{
   matrix A = { { 1,  2, 3 }, {  4, 5,  6 }, { 7, 8, 9 } };
   matrix B = { { 1, -1, 4 }, { 14, 0, -2 }, { 3, 6, 1 } };
   print( "\nA:", A );
   print( "\nB:", B );
   
   matrix C = A;
   for ( int r = 0; r < A.size(); r++ )
   {
      double mx = B[0][r];
      for ( int i = 1; i < B.size(); i++ ) if ( mx < B[i][r] ) mx = B[i][r];
      C[r] *= mx;
   }
   print( "\nC:", C ); 
}


A:
1	2	3	
4	5	6	
7	8	9	

B:
1	-1	4	
14	0	-2	
3	6	1	

C:
14	28	42	
24	30	36	
28	32	36	
Topic archived. No new replies allowed.