【问题标题】:C++: How to use a 'for' loop to multiply 2D arrays?C++:如何使用“for”循环来乘以二维数组?
【发布时间】:2021-05-03 01:20:38
【问题描述】:

我正在编写一个程序来填充两个多维数组并仅使用 for 循环将它们的元素相乘。代码非常简单,但我遇到了乘法循环中的许多错误。我无法理解这个。

我的循环语法有问题吗?我将附上下面的代码。第 48 行和第 49 行是乘法的循环代码。提前致谢。

#include <iostream>
#include <iostream>
using namespace std;

int main()
{

cout << "Creating array: " << endl;

cout << " " << endl;

cout << "Please enter the number of rows: "; 
int numRows2 = 0;
cin >> numRows2;

cout << " " << endl;

cout << "Please enter the number of columns: ";
int numCols2 = 0;
cin >> numCols2;

double myNums2[numRows2][numCols2];

for (int counter = 0; counter < numRows2; counter++)
    {
        for (int counter_a = 0; counter_a < numCols2; counter_a++)
           {
            cout << "Enter Element [" << counter << "][" << counter_a << "]: ";
            cin >> myNums2[counter][counter_a];
            }
    }

cout << " " << endl;

cout << "Displaying contents of the array: " << endl;

for (int counter = 0; counter < numRows2; ++counter)
    {
        for (int counter_a = 0; counter_a < numCols2; ++counter_a)

            cout << "Element [" << counter <<"][" << counter_a << "]: " << myNums2[counter][counter_a] << endl;
    }


cout << " " << endl;

cout << "Multiplying each element in myNums1 by each element in myNums2: " << endl;

for (int indexRow1 = 0, int indexCol1 = 0; indexRow1 < numRows, indexCol1 < numCols; ++indexRow1, ++indexCol1)
    for (int indexRow2 = 0, int indexCol2; indexRow2 < numRows2, indexCol2 < numCols2; ++indexRow2, ++indexCol2)
        {
            cout << myNums1[indexRow1][indexRow2] << " x " << myNums2[indexRow2][indexCol2] << " = " << myNums1[indexRow1][indexCol1] * myNums2[indexRow2][indexCol2] << endl;
        }

return 0;
}

【问题讨论】:

  • cin &gt;&gt; numCols2; double myNums2[numRows2][numCols2]; 是 VLA,不是有效的 C++。使用std::vector 而不是数组。
  • 当我注释掉乘法代码时,该语法完美运行。这个具体项目需要用到数组。
  • 很遗憾得知该项目要求您使用无效代码。您确定不允许使用 vector 吗?如果是这样,也许您应该动态分配数组,或者您可以创建不会超过的恒定大小的数组。
  • 我必须仔细检查项目要求...我可能读错了一些东西。使用 std::vector 并动态分配数组是更好的解决方案。
  • 在最后一个'cout'中:myNums1[R1][R2]

标签: c++ arrays loops multidimensional-array


【解决方案1】:

您不能在for 循环中声明两个变量,因为您正在这样做。 您可以查看thread 了解实现方法。

在您共享的 sn-p 上,某些变量未声明,numRowsnumColsmyNums1

indexCols2 未初始化,即使 for 循环不正确。

正如评论中提到的@cigien,要使用数组,如果您想从用户那里读取大小,则必须动态分配内存。

【讨论】:

    【解决方案2】:

    for 循环处的代码中有两个错误:

    for (int indexRow1 = 0, int indexCol1 = 0; indexRow1 < numRows, indexCol1 < numCols; ++indexRow1, ++indexCol1)
        for (int indexRow2 = 0, int indexCol2; indexRow2 < numRows2, indexCol2 < numCols2; ++indexRow2, ++indexCol2)
    
    
    • 第一:这个声明是错误的
    for (int indexRow1 = 0, int indexCol1 = 0, ...)
    
    The correct one is:
    
    for (int indexRow1 = 0, indexCol1 = 0, ...)
    
    • 第二:你没有在for循环之前声明numRowsnumCols。您可能需要检查您的代码并声明它们。

    【讨论】:

      猜你喜欢
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 2014-05-24
      • 2021-05-02
      • 2013-09-18
      • 2016-01-20
      相关资源
      最近更新 更多