【问题标题】:How to the add elements in two arrays, but in reverse order?如何在两个数组中添加元素,但顺序相反?
【发布时间】:2015-04-03 23:17:01
【问题描述】:

这段代码的目的是将两个数组中的元素相加,但顺序相反。 我不明白我做错了什么导致无法编译(语法、循环或数组错误??)。你能指出我正确的方向吗?谢谢!!

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
    const int ARRAY1_LEN = 3;
    const int ARRAY2_LEN = 2;

    int MyInts1[ARRAY1_LEN] = { 35, -3, 0};
    int MyInts2[ARRAY2_LEN] = {20, -1};

    cout << "Multiplying each int in MyInt1 by each in MyInts2 ... But Backwards:" << endl;

    for(int Array1Index = 0; Array1Index < ARRAY1_LEN - 1; Array1Index--);
        for(int Array2Index = 0; Array2Index < ARRAY2_LEN -1; Array2Index--);
        cout << MyInts1[Array1Index] << " x " << MyInts2[ Array2Index ] << " = " << MyInts1[Array1Index] * MyInts2[Array2Index] << endl;

        return 0;
}

【问题讨论】:

  • 您从索引 0 开始并永远向后退。您似乎认为只需将 ++ 更改为 -- 即可。
  • for 正文中什么也不做...
  • 您的代码不应编译 - 请发布您的实际代码
  • @JosephMansfield 那么我必须设置 Array1Index 和 Array2Index 什么值?
  • @MikeMB 那是它无法编译的东西,我不明白为什么。

标签: c++ arrays nested-loops decrement


【解决方案1】:

你的逻辑不正确。您从索引0 开始,然后向后退。这意味着,您正在输入负范围(-1、-2、-3、...),并且每个负数都满足循环条件。

应该是:

int main()
{
    const int ARRAY1_LEN = 3;
    const int ARRAY2_LEN = 2;

    int MyInts1[ARRAY1_LEN] = { 35, -3, 0 };
    int MyInts2[ARRAY2_LEN] = { 20, -1 };

    cout << "Multiplying each int in MyInt1 by each in MyInts2 ... But Backwards:" << endl;

    int start_1 = ARRAY1_LEN > 0 ? ARRAY1_LEN - 1 : 0;
    int start_2 = ARRAY2_LEN > 0 ? ARRAY2_LEN - 1 : 0;

    for(int Array1Index = start_1; Array1Index >= 0; Array1Index--)
    {
        for(int Array2Index = start_2; Array2Index >= 0; Array2Index--)
        {
            cout << MyInts1[Array1Index] << " x " << MyInts2[ Array2Index ] << " = " << MyInts1[Array1Index] * MyInts2[Array2Index] << endl;
        }
    }

    return 0;
}

如果至少有一个数组为空,此代码也将正常工作。

哦,还有一件事:你的代码也完全错误,因为你在for 后面有分号 (;),这意味着每个循环都有一个空的主体。所以,即使你的fors 是正确的,你也不会看到任何东西。

【讨论】:

  • @MateuzGrzejek 谢谢!
  • 这个答案有一些问题。首先,您从ARRAY1_LEN 计算start_2。其次,start_1start_2的计算是不必要的,因为数组的长度必须总是大于0。第三,for循环条件应该是&gt;=,否则不会使用第一个元素。
  • @JosephMansfield 是的,我注意到了,谢谢。当涉及到代码时,复制粘贴可能会很棘手:) 已经修复。此外,计算start_1start_2 比将ARRAY1_LEN - 1 之类的东西放在for 中更有用且更好。对于调试过程也很方便。
  • 没有理由引入额外的逻辑。如果实际上,如果ARRAY1_LEN 为零或更小(无论出于何种原因),编译器将产生错误,因此只要程序完全编译,int start_1 = ARRAY1_LEN &gt; 0 ? ARRAY1_LEN - 1 : 0; 将始终评估为int start_1 =ARRAY1_LEN-1。即使编译器不会产生错误,零或负数组长度的影响也会是循环体永远不会被执行。所以,如果你真的认为,你必须为最后一个有效索引引入一个别名,你至少应该省略三元语句。
【解决方案2】:

你的程序有两个错误:

  1. 整个循环体仅由 for 循环后面的分号组成,而不是您实际希望在循环内运行的代码。这也解释了为什么您的代码无法编译:您的乘法不是循环体的一部分,因此在循环头中定义的 Array1IndexArray2Index 不再存在。
  2. 虽然您正在减少数组索引,但您仍然从 0 开始,因此您将访问负数组索引。

所以你的代码实际上应该是这样的:

for (int Array1Index = ARRAY1_LEN - 1 ; Array1Index >= 0; Array1Index--){
    for (int Array2Index = ARRAY2_LEN - 1; Array2Index >=0; Array2Index--){
        cout << MyInts1[Array1Index] << " x " << MyInts2[Array2Index] << " = " << MyInts1[Array1Index] * MyInts2[Array2Index] << endl;
    }
}

【讨论】:

  • 您的代码中有一个错误:您正在访问MyInts1[ARRAY1_LEN],这是一个错误。此类元素不存在。
猜你喜欢
  • 2013-02-05
  • 2015-06-06
  • 2019-06-25
  • 2017-05-29
  • 2011-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-18
相关资源
最近更新 更多