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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include <cstdlib>
#include <iostream>
using namespace std;
int count(const int numbers[], int all, int & even, int & odd);
void split(int numbers[], int all, int odds[], int odd, int evens[], int even);
void print_list(int num[], int n);
int main()
{
int all, even, odd, zr;
int * numbers;
int * odds;
int * evens;
cout << "Enter number of elements: ";
cin >> all;
numbers = new int[all];
cout << "Enter list:" << endl;
for (int i=0; i<all; i++)
{
cin >> numbers[i];
}
zr = count(numbers,all,even,odd);
count(numbers,all,even,odd);
odds = new int[odd];
evens = new int[even];
split(numbers,all, odds, odd, evens, even);
cout << "Even elements: ";
print_list(evens, even);
cout << "Odd elements: ";
print_list(odds, odd);
cout << "There were " << zr << " zeros in the list.";
delete[] evens;
delete[] odds;
delete[] numbers;
return 0;
}
int count(const int numbers[], int all, int & even, int & odd)
{
int i, zer(0);
even = 0;
odd = 0;
for(i=0;i<all;i++)
{
if(numbers[i]%2==0)
{
even++;
}
if(numbers[i]==0)
{
zer++;
}
else
{
odd++;
}
}
return zer;
}
void split(int numbers[], int all, int odds[], int odd, int evens[], int even)
{
int i,e=0,o=0;
for(i=0;i<all;i++)
if(numbers[i]%2==0)
{
evens[e]=numbers[i];
e++;
}
else
{
odds[o]=numbers[i];
o++;
}
if(e!=even||o!=odd)
{
cout<<"Error\n";
system("exit");
}
}
void print(int num[],int n)
{
for(int i=0;i<n;i++)
{
cout<<num[i]<<" ";
}
cout<<endl;
}
void print_list(int num[], int n)
{
int i;
for(i=0;i<n;i++)
cout<<num[i]<<" ";
cout<<endl;
}
|