1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
// headers etc
#include <iostream>
#include <cmath>
using namespace std;
void count(int array[numElements], int numElements, int numEven, int numOdd);
void split(int array[numElements], int numElements, int arrayEven[numEven], int numEven, int arrayOdd[numOdd], int numOdd);
void print_array(int array[numElements], int numElements);
int main(){
int numElements;
int numEven;
int numOdd;
int * array;
int * arrayEven;
int * arrayOdd;
cout << "Enter number of elements: ";
cin >> numElements;
array = new int[numElements];
cout << "Enter list: " << endl;
cin >> array[0];
for (int a=1; a<numElements; a++){
cin >> array[a];
}
count(array, numElements, numEven, numOdd);
arrayEven = new int[numEven];
arrayOdd = new int[numOdd];
split(array, numElements, arrayEven, numEven, arrayOdd, numOdd);
cout << "Even elements:" << endl;
print_array(arrayEven, numEven);
cout << "Odd elements:" << endl;
print_array(arrayOdd, numOdd);
delete [] array;
delete [] arrayEven;
delete [] arrayOdd;
}
void count(int array[numElements], int numElements, int numEven, int numOdd){
for (int a=0; a<numElements; a++){
if (array[a]%2==0){
numEven++;
}
else if (array[a]%2==1){
numOdd++;
}
}
}
void split(int array[numElements], int numElements, int arrayEven[numEven], int numEven, int arrayOdd[numOdd], int numOdd){
int checkEven;
int checkOdd;
int b=0;
int c=0;
for (int a=0; a<numElements; a++){
if (array[a]%2==0){
arrayEven[b]=array[a];
b++;
}
else if (array[a]%2==1){
arrayOdd[c]=array[a];
c++;
}
}
if (!numEven==b || !numOdd==c){
cerr << "Error.";
exit(10);
}
}
void print_array(int array[numElements], int numElements){
for (int a=0; a<numElements; a++){
cout << array[a];
}
}
|