【发布时间】:2016-09-11 05:19:23
【问题描述】:
C++ 初学者在这里开始学习使用数组的不同方法。我正在尝试将下面的程序放在一起。它应该要求用户输入一组将进入数组的数字。这组数字可以在用户想要的任何时候增加或减少。
例如:
1 2 3 4 9 8 7 4 5 6 10 11 12 20 19 18 17
该程序将检查该组数字,并在每次增加或减少时进行说明。现在程序将返回“增加”或“减少”。
例如,针对上述返回的一组数字运行它:
增加 递减 减少
但是,我有两个问题:
我认为它没有正确考虑阵列中发生的所有变化(增加和减少)。不确定我哪里出错了。
我现在必须返回发生的更改数量,而不是“减少”/“增加”。这意味着,在上面的示例中,它将返回“3”,因为有 1 个真值和 2 个假值。
如果有人可以提供建议,我将非常感谢您的帮助。
非常感谢!!!
#include <iostream>
using namespace std;
bool increasing(int arr[], int n);
int main()
{
int arr[20], n;
cout << "Enter a set of Increasing/Decreasing numbers (ex. 1 2 3 6 5 4 7 8 9 3 2 1)." << endl;
cout << "Press 'Enter' to see results" << endl;
while (cin >> n)
{
for (int i = 0; i <= n; i++)
{
cin >> arr[i];
}
cout << (increasing(arr, n) ? "increasing" : "decreasing") << endl;
}
return 0;
}
bool increasing(int arr[], int n)
{
int x = 0;
for (int i = 0; i < n - 1; i++)
{
if (arr[i] < arr[i + 1])
{
x++;
}
}
if (x == n - 1)
{
return true;
}
else
{
return false;
}
}
【问题讨论】: