Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Has to be in c++, please keep it simple as possible. Thanks. Given that A[MAX_RO

ID: 3784482 • Letter: H

Question

Has to be in c++, please keep it simple as possible. Thanks.

Given that A[MAX_ROWS] [MAX_COLUMNS] is a 2 dimensional array of integers, write a C++ function Even to find the total number of even elements in the array. It should have 3 input parameters: A[length][width] Length Width The function should return an integer. The constants MAX_ROWS and MAX_COLUMNS maybe accessed globally by defining the following global constants: const int MAX_ROWS=3; const int MAX_C0LUMNS=2; The array A is initialized by the following statement in your main(): int A[MAX_ROWS][MAX_COLUMNS] = {{3, 2}, {4, 5}, {2, 2}};

Explanation / Answer

//C++ code
#include <iostream>
#include <cstring>

using namespace std;

const int MAX_ROWS = 3;
const int MAX_COLUMNS = 2;

int even(int A[][MAX_COLUMNS], int length,int width)
{
int countEven = 0;
for (int i = 0; i < length; ++i)
{
for (int j = 0; j < width; ++j)
{
if(A[i][j]%2 == 0 )
countEven++;
}
}
return countEven;
}

int main()
{
int A[MAX_ROWS][MAX_COLUMNS] = {{3,2},{4,5},{2,2}};

cout << "Total number of even numbers in the array: " << even(A,MAX_ROWS,MAX_COLUMNS) << endl;
return 0;
}

/*
output:

Total number of even numbers in the array: 4


*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote