Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将两个不同大小的二维数组相乘_C++_Arrays_Multidimensional Array - Fatal编程技术网

C++ 将两个不同大小的二维数组相乘

C++ 将两个不同大小的二维数组相乘,c++,arrays,multidimensional-array,C++,Arrays,Multidimensional Array,我有两个二维数组,它们必须是数组,而不是向量。声明如下: 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 我需要能够将x的行乘以y的行,但是x的第一行必须被忽略,y的第一列必须被忽略 数据可能如下所示: x = { { 1, 2, 3, 4, 5, 6 }, { 5.5, 6.7, 3.

我有两个二维数组,它们必须是数组,而不是向量。声明如下:

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
我需要能够将x的行乘以y的行,但是x的第一行必须被忽略,y的第一列必须被忽略

数据可能如下所示:

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 } };
所以我需要跳过x数组的第一行,将x行乘以y列。比如:

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

我尝试了很多东西,但我一直回到这个循环:

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
(inti=1;i<6;i++)的
for//我也反向尝试过
{//像i=0;i<4和j=1等等
对于(int j=0;j<4;j++)
{
cout
6.7*3.2
->
3.3*0.0
…我尝试了多种方式修改此循环,包括带有while循环的双嵌套循环。没有任何效果,有时会导致整数值相乘


我需要跳过
x[][]
中的第一行和
y[][]
中的第一列。然后将
x[][]
中的行乘以
y[][]
中的相应列。但是跳过
y[]
中的第一列,这是正确的循环

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

UPD1:问题已更新

UPD2:但随后移动到原始语句

下面是更新问题的代码

#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;
}

您是否尝试使用调试器运行代码?“输入错误”可以通过复制/粘贴您运行的实际代码来避免。制作一个,并将其复制/粘贴到问题中。请阅读。我现在感觉自己像个白痴。我需要编辑我的帖子。我问了错误的问题。@xtryingx不要!接受您对此问题的答案并提出一个新问题。这是不正确的。这将得到第一列输出是你想要的,它不会得到它,列是y的第二个索引,
I
以1开始,所以第一列被跳过输出不是我想要的。这得到了y的第一列,我在问题中声明,我想要避免x的第一行和y的第一列。我认为这是一个错误漫长的一天。让我快速测试一下。我已经在答案中添加了输出,告诉我您在哪里看到第一个
y
列?
#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;
}
(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)