xtryingx
xtryingx

Reputation: 152

Multiply two 2d array of different sizes

I have two 2d arrays and they must be arrays, not vectors. They are declared:

double x[4][6];  //size can be any value but the row and column
double y[6][4];  //must be the opposite of the other array

I need to be able to multiply the rows of x by the rows of y but the first row of x must be ignored and the first column of y must be ignored.

The data may look like this:

x = { { 1, 2, 3, 4, 5, 6 },
      { 5.5, 6.7, 3.3, 47.0, 1.2, 0.5 },
      { 1.2, 34.3, 66.7, 0.2 2.0, 3.7 },
      { 5.6, 6.5, 7.5, 5.7, 4.0, 1.1 } };

y = { { 1, 6.6, 3.2, 0.0 },
      { 2, 1.2, 1.1, 8.8 },
      { 3, 5.6, 4.3, 5.5 },
      { 4, 2.3, 4.1, 3.4 },
      { 5, 8.5, 1.9, 6.8 },
      { 6, 5.2, 5.3, 1 } };

So I would need to skip the first row of the x array and multiply rows x by the columns of y. Something like:

5.5*6.6 -> 6.7*1.2 -> 3.3*5.6...

I have tried many things but I keep coming back to this loop:

for (int i = 1; i < 6; i++)    //I have also tried it backwards
{                           //like i = 0; i < 4 and j = 1 etc
    for (int j = 0; j < 4; j++)
    {
        cout << x[i][j] * y[j][i];
    }
}    //I have also tried adding a third int value for indexing
     //And I could not figure that out either

This usually results in the wrong values getting multiplied, something like:

5.5*6.6 -> 6.7*3.2 -> 3.3*0.0... I have tried modifying this loop in many ways including a double nested loop with a while loop. Nothing works I sometimes end up with the integer values getting multiplied.

I need to skip row one from x[][] and column one from y[][]. Then multiply the row from x[][] to the corresponding column from y[][]. But skip the first column of y[][].

Upvotes: 1

Views: 452

Answers (1)

drompix
drompix

Reputation: 566

Here is the correct loop

for (int i = 1; i < 4; i++) {
    for (int j = 0; j < 6; j++) {
        cout << "(" << x[i][j] << " * " << y[j][i] << ") ";
    }
    cout << endl;
}

Output

(5.5 * 6.6) (6.7 * 1.2) (3.3 * 5.6) (47 * 2.3) (1.2 * 8.5) (0.5 * 5.2) 
(1.2 * 3.2) (34.3 * 1.1) (66.7 * 4.3) (0.2 * 4.1) (2 * 1.9) (3.7 * 5.3) 
(5.6 * 0) (6.5 * 8.8) (7.5 * 5.5) (5.7 * 3.4) (4 * 6.8) (1.1 * 1)

Demo

UPD1:The question was updated

UPD2: but then moved to original statement

Here is the code for an updated question

#include <iostream>

using namespace std;

#define ROW 4
#define COL 6

double x[ROW][COL] = { { 1, 2, 3, 4, 5, 6 },
                       { 5.5, 6.7, 3.3, 47.0, 1.2, 0.5 },
                       { 1.2, 34.3, 66.7, 0.2, 2.0, 3.7 },
                       { 5.6, 6.5, 7.5, 5.7, 4.0, 1.1 } };

double y[COL][ROW] = { { 1, 6.6, 3.2, 0.0 },
                       { 2, 1.2, 1.1, 8.8 },
                       { 3, 5.6, 4.3, 5.5 },
                       { 4, 2.3, 4.1, 3.4 },
                       { 5, 8.5, 1.9, 6.8 },
                       { 6, 5.2, 5.3, 1 } };

int main() {
    int yi = 0, yj = 1;
    for (int xi = 1; xi < ROW; xi++) {
        for (int xj = 0; xj < COL; xj++) {
            double x_val = x[xi][xj];
            double y_val = y[yi][yj];
            yj = (yj + 1) % ROW == 0 ? 1 : yj + 1;  // move to the next column
            yi = yj == 1 ? yi + 1 : yi; // if required, move to next row

            cout << "(" << x_val << " * " << y_val << ") ";
        }
        cout << endl;
    }
    return 0;
}

Output

(5.5 * 6.6) (6.7 * 3.2) (3.3 * 0) (47 * 1.2) (1.2 * 1.1) (0.5 * 8.8) 
(1.2 * 5.6) (34.3 * 4.3) (66.7 * 5.5) (0.2 * 2.3) (2 * 4.1) (3.7 * 3.4) 
(5.6 * 8.5) (6.5 * 1.9) (7.5 * 6.8) (5.7 * 5.2) (4 * 5.3) (1.1 * 1) 

Upvotes: 2

Related Questions