【发布时间】: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