#include <iostream>
/*
Function: countOddEven
Purpose: Counts how many odd and even numbers exist in the matrix.
Parameters:
- matrix: the 2D array
- rows: number of rows in the matrix
- cols: number of columns in the matrix
- odd: reference variable to store odd count
- even: reference variable to store even count
*/
void countOddEven(int matrix[][4], size_t rows, size_t cols, int &odd, int &even) {
odd = 0;
even = 0;
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < cols; j++) {
// Check if the number is even or odd
if (matrix[i][j] % 2 == 0)
even++;
else
odd++;
}
}
}
int main() {
int matrix[][4] = {
{1, 0, 2, 5},
{3, 5, 6, 9},
{7, 4, 1, 8}
};
// Automatically compute matrix dimensions
static const size_t rows = (sizeof(matrix) / sizeof(matrix[0]));
static const size_t cols = (sizeof(matrix[0]) / sizeof(matrix[0][0]));
int oddCount, evenCount;
// Call the function
countOddEven(matrix, rows, cols, oddCount, evenCount);
// Display the result
std::cout << "The frequency of odd numbers = " << oddCount << std::endl;
std::cout << "The frequency of even numbers = " << evenCount << std::endl;
}
/*
run:
The frequency of odd numbers = 7
The frequency of even numbers = 5
*/