您的位置:首页 > 编程语言 > C语言/C++

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

2012-04-23 22:16 363 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐