/*
Function: countOddEven
Purpose: Counts how many odd and even numbers exist in the matrix.
Parameters:
- matrix: the 2D list
- 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
*/
fun countOddEven(matrix: List<List<Int>>): Pair<Int, Int> {
// Automatically compute matrix dimensions inside the function
val rows: Int = matrix.size
val cols: Int = matrix[0].size
var odd: Int = 0
var even: Int = 0
for (i in 0 until rows) {
for (j in 0 until cols) {
// Check if the number is even or odd
if (matrix[i][j] % 2 == 0)
even++
else
odd++
}
}
return Pair(odd, even)
}
fun main() {
val matrix: List<List<Int>> = listOf(
listOf(1, 0, 2, 5),
listOf(3, 5, 6, 9),
listOf(7, 4, 1, 8)
)
// Call the function (rows and cols are now computed inside)
val (oddCount, evenCount) = countOddEven(matrix)
// Display the result
println("The frequency of odd numbers = $oddCount")
println("The frequency of even numbers = $evenCount")
}
/*
run:
The frequency of odd numbers = 7
The frequency of even numbers = 5
*/