=begin
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 (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
=end
def count_odd_even(matrix)
# Automatically compute matrix dimensions inside the function
rows = matrix.length
cols = matrix[0].length
odd = 0
even = 0
rows.times do |i|
cols.times do |j|
# Check if the number is even or odd
if matrix[i][j] % 2 == 0
even += 1
else
odd += 1
end
end
end
[odd, even]
end
def main
matrix = [
[1, 0, 2, 5],
[3, 5, 6, 9],
[7, 4, 1, 8]
]
# Call the function (rows and cols are now computed inside)
odd_count, even_count = count_odd_even(matrix)
# Display the result
puts "The frequency of odd numbers = #{odd_count}"
puts "The frequency of even numbers = #{even_count}"
end
main
=begin
run:
The frequency of odd numbers = 7
The frequency of even numbers = 5
=end