C++实现判断输入的数组是否是升序的程序

简介:
#include <iostream>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::vector;

//input elements for the vector class object v.
void input(vector<int> &v);

//display the elements.
void output(vector<int> v);

//return the bool value. If the elements are ascending, return 1,otherwise return 0.
bool isAscendingOrder(vector<int> v);

int main()
{

vector<int> v;

input(v);//Input elements.
if (isAscendingOrder(v))
cout << "The elements are ascending!" << endl;
else
cout << "The elements are not ascending!" << endl;
output(v);//output elements.

system("pause");
return 0;
}

void input(vector<int> &v){
int numbers;
int num;

cout << "How many numbers do you want to enter: ";
cin >> numbers;
cout << "Enter "<< numbers << " for a group of integer numbers:" << endl;
for (int i = 0; i < numbers; i++)
{
cin >> num;
v.push_back(num);
}
}//End input

//If it is ascending, return 1, otherwise return 0;
bool isAscendingOrder(vector<int> v)
{
for (int i = 0, j = 1; i < v.size() && j < v.size(); i++, j++)
{
if (v[i] > v[j])
return 0;
}
return 1;
}//End isAscendingOreder

//function output
void output(vector<int> v)
{
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
}//End output 

目录
相关文章
|
22天前
|
存储 缓存 算法
【C/C++ 性能优化】提高C++程序的缓存命中率以优化性能
【C/C++ 性能优化】提高C++程序的缓存命中率以优化性能
111 0
|
24天前
|
存储 算法 编译器
【C++ 字符数组的模板特化】面向字符串的C++模板特化:理解与实践
【C++ 字符数组的模板特化】面向字符串的C++模板特化:理解与实践
47 1
|
30天前
|
存储 缓存 安全
C++数组全解析:从基础知识到高级应用,领略数组的魅力与技巧
C++数组全解析:从基础知识到高级应用,领略数组的魅力与技巧
52 1
|
1月前
|
存储 算法 搜索推荐
在C++编程语言中数组的作用类型
在C++编程语言中数组的作用类型
14 0
在C++编程语言中数组的作用类型
|
29天前
|
缓存 编译器 程序员
C/C++编译器并行优化技术:并行优化针对多核处理器和多线程环境进行优化,以提高程序的并行度
C/C++编译器并行优化技术:并行优化针对多核处理器和多线程环境进行优化,以提高程序的并行度
60 0
|
29天前
|
缓存 编译器 程序员
C/C++编译器全局优化技术:全局优化是针对整个程序进行的优化,包括函数之间的优化
C/C++编译器全局优化技术:全局优化是针对整个程序进行的优化,包括函数之间的优化
25 0
|
29天前
|
缓存 算法 编译器
C/C++编译器内存优化技术:内存优化关注程序对内存的访问和使用,以提高内存访问速度和减少内存占用。
C/C++编译器内存优化技术:内存优化关注程序对内存的访问和使用,以提高内存访问速度和减少内存占用。
35 0
|
30天前
|
自然语言处理 编译器 调度
深入gcc编译器:C/C++代码如何变为可执行程序
深入gcc编译器:C/C++代码如何变为可执行程序
75 0
|
30天前
|
存储 缓存 安全
【C/C++ 基础 数组容器比较】深入探究C++容器:数组、vector与array之间的异同
【C/C++ 基础 数组容器比较】深入探究C++容器:数组、vector与array之间的异同
15 0
|
30天前
|
并行计算 安全 编译器
【C/C++ 编译相关 gcc】一次搞懂GCC编译选项:优化代码、调试程序必备!
【C/C++ 编译相关 gcc】一次搞懂GCC编译选项:优化代码、调试程序必备!
32 0