136k views
3 votes
Given that arrayIntValues [MAX_ROWS][MAX_COLUMNS] is a 2 dimensional array of positive integers, write a C++ function Even to find the total number of even elements in the array.

It should have input parameter array arrayIntValues.
A[length][width]
Length
Width

The function should return an integer. Also create a C++ subroutine called printArray with the input parameter array.

1 Answer

2 votes

Answer:

Here is the C++ program:

#include <iostream> //to use input output functions

using namespace std; // to identify objects like cin cout

const int MAX_ROWS=3; //sets the maximum number of rows to 3

const int MAX_COLUMNS=2; //sets the maximum number of columns to 2

//function prototypes

void printArray(const int array[MAX_ROWS][MAX_COLUMNS]);

void Even(const int arrayIntValues[MAX_ROWS][MAX_COLUMNS]);

int main(){ //start of main method

int arrayIntValues[MAX_ROWS][MAX_COLUMNS]; //declares a 2D array of integers

cout<<"Enter elements of 2D array: "; //prompts user to enter integers in a 2D array

for(int i=0;i<MAX_ROWS;i++) { //outer loop to iterate through rows

for(int j=0;j<MAX_COLUMNS;j++) //inner loop to iterate through columns

cin>>arrayIntValues[i][j]; } //reads and stores integers in 2D array

Even(arrayIntValues); //calls Even method by passing arrayIntValues

printArray(arrayIntValues); } //calls printArray method by passing arrayIntValues

void Even(const int arrayIntValues[MAX_ROWS][MAX_COLUMNS]){ //method that takes an array as parameter and finds the total number of even elements in the array

int count = 0; //counts the number of even numbers in array

for(int row=0;row<MAX_ROWS;row++){ //outer loop iterates through rows of the array

for(int col=0;col<MAX_COLUMNS;col++){ //inner loop iterates through columns of the array

if(arrayIntValues[row][col]%2==0 && arrayIntValues[row][col]>=0) //if the element at specified index of 2D array is an even number i.e. completely divisible by 2 and that element is positive

count++; } } //adds 1 to the count variable each time an even positive integer appears in the array

cout<<"total number of even elements in the array: "<<count<<endl; } //displays the number of even element in arrayIntValues

void printArray(const int array[MAX_ROWS][MAX_COLUMNS]){ //method that takes an array as parameter and displays the even numbers in that array

for(int row=0;row<MAX_ROWS;row++){ //outer loop iterates through rows of the array

for(int col=0;col<MAX_COLUMNS;col++){ //inner loop iterates through columns of the array

if(array[row][col]%2==0 && array[row][col]>=0) //if the element at specified index of 2D array is an even number i.e. completely divisible by 2 and that element is positive

cout<<array[row][col]<<endl; //displays the even element of the array

else //if element is not even

continue; } } } //continues the loop to look for even elements

Step-by-step explanation:

The program first prompts the user to enter elements in 2D array. It then calls Even method which iterates through the rows and columns of the array to look for the positive even numbers (elements) in array . Whenever a positive even number is encountered in the array, the count variable is incremented to 1. At the end the method returns an integer which is the total number of even elements in the array. Then the program calls printArray method which iterates through the rows and columns of the array to look for the positive even numbers (elements) in array . Whenever a positive even number is encountered in the array, that element is displayed on the output screen. The screenshot of the output is attached.

Given that arrayIntValues [MAX_ROWS][MAX_COLUMNS] is a 2 dimensional array of positive-example-1
User Malgosia
by
5.3k points