天天看点

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 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 

继续阅读