#include <stdio.h>
/*
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: pointer to store odd count
- even: pointer 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(void) {
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 */
printf("The frequency of odd numbers = %d\n", oddCount);
printf("The frequency of even numbers = %d\n", evenCount);
return 0;
}
/*
run:
The frequency of odd numbers = 7
The frequency of even numbers = 5
*/