Write a function that saves even numbers of an innuation array foreground to another with sufficiently distributed array space. Write the main program using that function in 2 cases:
The input array is the static array with the given elements
The input array is a dynamic array with the number of elements and elements imported from the keyboard
Displays the resulting array to the screen
#include <iostream>
using namespace std;
int GetEvenNumber(int numbers[], int size, int evenNumbers[]) {
int n=0;
for (int i=0; i<size; i++) {
if (numbers[i] % 2 == 0) {
evenNumbers[n] = numbers[i];
n++;
}
}
return n;
}
void PrintArray(int array[], int size) {
for (int i=0; i<size; i++) {
cout << array[i];
if (i != size-1) {
cout << ", ";
}
}
cout << endl;
}
int main() {
const int N = 5;
int a1[N] = {1, 2, 3, 4, 5};
int even[N];
int n;
cout << "Static array: " ;
PrintArray(a1, N);
n = GetEvenNumber(a1, N, even);
cout << "Even numbers: ";
PrintArray(even, n);
cout << endl;
int size;
cout << "Enetr size of the new array: ";
cin >> size;
int* a2 = new int[size];
int* even2 = new int[size];
cout << "Enter " << size << " numbers:" << endl;
for (int i=0; i<size; i++) {
cout << (i+1) << "-th number: ";
cin >> a2[i];
}
cout << endl << "Dynamic array: ";
PrintArray(a2, size);
n = GetEvenNumber(a2, size, even2);
cout << "Even numbers: ";
PrintArray(even2, n);
delete [] a2;
delete [] even2;
return 0;
}
Comments
Leave a comment