81 lines
1.2 KiB
C++
81 lines
1.2 KiB
C++
/*
|
|
* read list of ints
|
|
* output whether list contains all even, all odd, or neither.
|
|
* input begins with an int indicating the number of ints in the list
|
|
*
|
|
* input:
|
|
* 5 2 4 6 8 10
|
|
*
|
|
* output:
|
|
* all even
|
|
*
|
|
* input:
|
|
* 5 1 -3 5 -7 9
|
|
*
|
|
* output:
|
|
* all odd
|
|
*
|
|
* input:
|
|
* 5 1 2 3 4 5
|
|
*
|
|
* output:
|
|
* not even or odd
|
|
*
|
|
* Two functions
|
|
*
|
|
* bool IsVectorEven(vector<int> myVec)
|
|
* bool IsVectorOdd(vector<int> myVec)
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <vector>
|
|
using namespace std;
|
|
|
|
/* Define your function here */
|
|
bool IsVectorEven(vector<int> myVec) {
|
|
for (int i=0;i<myVec.size();i++){
|
|
if (myVec.at(i) % 2 != 0){
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool IsVectorOdd(vector<int> myVec) {
|
|
for (int i=0;i<myVec.size();i++){
|
|
if (myVec.at(i) % 2 == 0){
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
int main() {
|
|
/* Type your code here. */
|
|
|
|
int length;
|
|
int input;
|
|
cin >> length;
|
|
|
|
vector<int> listOfNums;
|
|
|
|
for (int i=0;i<length;i++){
|
|
cin >> input;
|
|
listOfNums.push_back(input);
|
|
}
|
|
|
|
if (IsVectorEven(listOfNums)){
|
|
cout << "all even" << endl;
|
|
}
|
|
else if (IsVectorOdd(listOfNums)){
|
|
cout << "all odd" << endl;
|
|
}
|
|
else{
|
|
cout << "not even or odd" << endl;
|
|
}
|
|
|
|
return 0;
|
|
}
|