数组是否升序
Array with whether if an array is ascending or not ascending
所以问题是弄清楚数组是否升序,这意味着如果数组是 2,3,4,6,它将显示是它正在升序,如果是 3,2,5,6 它将显示 no 不升序。
我似乎无法让程序显示 0,这意味着不升序,我知道问题出在 for 循环或 if 语句中的某个地方,但我不知道如何解决它
#include <iostream>
using namespace std;
int ascending(int x[], int npts);
int main()
{
int number[10];
int num;
cout<<"How many numbers would you like to enter? (max = 10) ";
cin>>num;
for(int i = 0; i<num; i++)
{
cout<<"Enter number ";
cin>>number[i];
}
int asc = ascending(number, num);
cout<<asc;
}
int ascending(int x[], int n)
{
int count = 0;
for(int i = 0; i<n-1; i++)
{
if(x[i]<x[i + 1])
count++;
if(count <= n - 1)
return 1;
else
return 0;
}
}
您的问题出在升序函数中,您在第一次迭代后返回 1。 为了确定数字序列是否升序,必须遍历所有数字序列。但是,如果您发现一个在任何时候都不升序的数字,那么您可以立即返回 0。如果您安全地遍历所有这些而不返回 0,那么您就知道序列正在升序
int ascending(int x[], int n)
{
for(int i = 0; i<n-1; i++)
{
if(x[i]>=x[i + 1])
return 0
}
return 1
}
int ascending(int x[], int n)
{
int i = 0;
Do
{
if(x[i]> x[i + 1])
return 0;
i++;
} while (i < n-1);
return 1;
}
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 检查输入是否不是整数或数字
- 是否可以初始化不可复制类型的成员变量(或基类)
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 此代码是否违反一个定义规则
- 是否需要删除包含对象的"pair"?
- 是否可以从int转换为enum类类型
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 无论条件是否为true,if总是在c++中执行
- 如何检查 2D 数组在 c++ 中是否按升序排列?
- 如何确定数字是否具有从右到左升序的数字
- 确定以下函数是否确定数组是否按升序排序
- 数组是否升序
- std::set迭代顺序是否始终根据C++规范升序
- 如何打印这个数组的四个角?我怎么知道这些列是否是严格升序的
- OpenMP循环中的索引是否按升序处理
- 如何用C++编写一个程序,该程序从输入中接收10个数字并检查这些数字是否按升序排列