package main
import (
"fmt"
)
/*
Function: countOddEven
Purpose: Counts how many odd and even numbers exist in the matrix.
Parameters:
- matrix: the 2D slice
- rows: number of rows in the matrix (computed inside the function)
- cols: number of columns in the matrix (computed inside the function)
- odd: variable to store odd count
- even: variable to store even count
*/
func countOddEven(matrix [][]int) (int, int) {
// Automatically compute matrix dimensions inside the function
rows := len(matrix)
cols := len(matrix[0])
odd := 0
even := 0
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
// Check if the number is even or odd
if matrix[i][j]%2 == 0 {
even++
} else {
odd++
}
}
}
return odd, even
}
func main() {
matrix := [][]int{
{1, 0, 2, 5},
{3, 5, 6, 9},
{7, 4, 1, 8},
}
// Call the function (rows and cols are now computed inside)
oddCount, evenCount := countOddEven(matrix)
// Display the result
fmt.Println("The frequency of odd numbers =", oddCount)
fmt.Println("The frequency of even numbers =", evenCount)
}
/*
run:
The frequency of odd numbers = 7
The frequency of even numbers = 5
*/