115k views
3 votes
Write a program that declares an array named myArray with 8 components of the type int. Initialize the array to 8 values that the user inputs (remember to prompt the user). Finally, pass the array as a parameter to a new function called filterEvens. This new function will display all of the even numbers in the array.

User Bigtony
by
6.7k points

1 Answer

5 votes

Answer:

#include <iostream>

using namespace std;

void filterEvens(int myArray[]) {

for (int i = 0; i < 8; ++i) {

if(myArray[i]%2==0){

cout<<myArray[i]<<" ";

}

}

}

int main(){

int myArray[8];

for(int i =0;i<8;i++){

cin>>myArray[i];

}

filterEvens(myArray);

return 0;

}

Step-by-step explanation:

The solution is provided in C++

#include <iostream>

using namespace std;

The function filerEvens is defined here

void filterEvens(int myArray[]) {

This iterates through the elements of the array

for (int i = 0; i < 8; ++i) {

This checks if current element is an even number

if(myArray[i]%2==0){

If yes, this prints the array element

cout<<myArray[i]<<" ";

}

}

}

The main begins here

int main(){

This declares an integer array of 8 elements

int myArray[8];

The following iteration allows input into the array

for(int i =0;i<8;i++){

cin>>myArray[i];

}

This calls the defined function filter Evens

filterEvens(myArray);

return 0;

}

User Jordan Owens
by
6.4k points