我有以下问题:
在这里...输出摘要如下。
输出摘要:测试两个数组以确定它们是否在相同位置包含相同的值。编写一个测试函数 isEqual 的主程序。使用原型(prototype):bool isEqual(int A[ ], int B[ ])
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define FLUSH cin.clear(); while(cin.get()!='\n');
bool isEqual (int A[], int B[]);
int main()
{
int index;
bool matches[10] = {0, 1, 0, 0, 0, 1, 0, 0, 0, 0};
int A[10] = {2, 3, 4, 5, 8, 11, 15, 7, 3, 1};
int B[10] = {1, 3, 7, 2, 3, 11, 8, 9, 10, 0};
for (index = 0; index <= 9; index++)
{
matches[index] = isEqual (A[index], B[index]);
}
if (matches[index] == 1)
{
cout << 1 << endl;
}
}
bool isEqual (int A[], int B[])
{
return (A == B);
}
我必须使用所示的 isEqual() 原型(prototype),但不确定如何在匹配数组中获取正确的值。
最佳答案
好的
当您使用 [] “运算符”时,它会检查该位置数组的值
那么假设对于数组 A,A[1] == 3
,对吗? B 也一样,B[2] == 7
请记住,数组是从 0 开始的,这意味着元素 0 实际上是数组中的第一个元素 (B[0] == 1
)。
所以当你调用“isEqual(A[index], B[index])
”时,实际发生的是
(假设索引 == 0)
isEqual(2, 1);
您需要意识到您实际上是在传递两个数字而不是数组。
我不喜欢给你做家庭作业,因为你不会学到任何东西。但为了让你走在正确的轨道上,我很确定这个问题可以改写如下
编写一个函数“isEqual”,接受两个 int
数组。
在该函数内部,比较循环内两个数组在相同索引位置的每个数字
if (A[index] == B[index])...
如果所有这些比较都为真,则两个数组相等。
我建议您还为数组大小 (10) 声明一个全局常量。通常,IsEqual 的原型(prototype)是 IsEqual(int A[], int B[], int ARRAY_SIZE)
,但您的原型(prototype)不是那样的,因此您必须知道数组有多大
希望对你有帮助
关于c++ - 在相同位置测试具有相同值的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4372725/