Posted on by Kalkicode
Code Matrix

# Find frequencies of even and odd numbers in a matrix

In this problem involves finding the frequencies of even and odd numbers within a given matrix. A matrix is a two-dimensional array containing rows and columns of elements. The goal is to count how many even and odd numbers are present in the matrix.

## Problem Statement

Given a matrix containing integer elements, the objective is to count the number of even and odd elements within the matrix separately.

## Example

Consider the following matrix:

``````1   2   3   4   5
4   5   6  11  12
7   8   9  13  14``````

In this matrix, there are 7 even elements (2, 4, 4, 6, 12, 8, and 14) and 8 odd elements (1, 3, 5, 5, 11, 7, 9, and 13).

## Solution Idea

To solve this problem, we can iterate through each element of the matrix and check whether it is even or odd. Depending on the parity of the element, we increment the respective counter.

## Pseudocode

``````frequencies(matrix):
even = 0, odd = 0
for each row in matrix:
for each element in row:
if element is even:
increment even counter
else:
increment odd counter
print "Even Elements:", even
print "Odd Elements:", odd``````

## Algorithm Explanation

1. Define a function `frequencies(matrix)` that takes the input matrix as its parameter.
2. Initialize variables `even` and `odd` to keep track of the count of even and odd elements, respectively.
3. Iterate through each row of the matrix.
4. For each row, iterate through the elements in that row.
5. Check if the current element is even or odd by using the modulus operator (%). If it is even, increment the `even` counter. Otherwise, increment the `odd` counter.
6. After both loops complete, print the counts of even and odd elements.

## Code Solution

``````/*
C Program
Find frequencies of even and odd numbers in a matrix
*/
#include <stdio.h>
#define ROW 3
#define COL 5

//Display matrix elements
void show_data(int matrix[][COL])
{
for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COL; ++j)
{
printf("%3d",matrix[i][j] );
}
printf("\n");
}
printf("\n");
}

void frequencies(int matrix[ROW][COL])
{
//Counter variable which are store the information
//About number of even and odd elements
int even=0,odd=0;

for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COL; ++j)
{
if(matrix[i][j]%2==0)
{
//When even element
even++;
}
else
{
//When get new odd number
odd++;
}
}
}
//Display the number of even and odd elements
printf("  Even Elements : %d\n",even );
printf("  Odd Elements: %d\n",odd );

}
int main(){

int matrix[ROW][COL] = {
{ 1, 2, 3, 4, 5 },
{ 4, 5, 6 , 11 , 12 },
{ 7, 8, 9 ,13, 14}
};
show_data(matrix);
frequencies(matrix);

return 0;
}```
```

#### Output

``````  1  2  3  4  5
4  5  6 11 12
7  8  9 13 14

Even Elements : 7
Odd Elements: 8``````
``````/*
C++ Program
Find frequencies of even and odd numbers in a matrix
*/
#include<iostream>
#define ROW 3
#define COL 5
using namespace std;

class MyMatrix {
public:
//Display matrix elements
void show_data(int matrix[][COL])
{
for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COL; ++j)
{
cout<<"  "<<matrix[i][j];
}
cout<<"\n";
}
cout<<"\n";
}

void frequencies(int matrix[ROW][COL])
{
//Counter variable which are store the information
//About number of even and odd elements
int even=0,odd=0;

for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COL; ++j)
{
if(matrix[i][j]%2==0)
{
//When even element
even++;
}
else
{
//When get new odd number
odd++;
}
}
}
//Display the number of even and odd elements
cout<<"  Even Elements : "<<even <<endl;
cout<<"  Odd Elements: "<<odd <<endl;

}

};
int main() {
MyMatrix obj;
int matrix[][COL] = {
{ 1, 2, 3, 4, 5 },
{ 4, 5, 6 , 11 , 12 },
{ 7, 8, 9 ,13, 14}
};
obj.show_data(matrix);
obj.frequencies(matrix);

return 0;
}```
```

#### Output

``````  1  2  3  4  5
4  5  6  11  12
7  8  9  13  14

Even Elements : 7
Odd Elements: 8``````
``````/*
Java Program
Find frequencies of even and odd numbers in a matrix
*/
public class MyMatrix {

//Display matrix elements
public void show_data(int [][]matrix)
{
//Get the size of matrix
int row = matrix.length;
int col = matrix[0].length;
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
System.out.print("  "+matrix[i][j] );
}
System.out.print("\n");
}
System.out.print("\n");
}

public void frequencies(int [][]matrix)
{
//Counter variable which are store the information
//About number of even and odd elements
int even=0,odd=0;
//Get the size of matrix
int row = matrix.length;
int col = matrix[0].length;
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
if(matrix[i][j]%2==0)
{
//When even element
even++;
}
else
{
//When get new odd number
odd++;
}
}
}
//Display the number of even and odd elements
System.out.print("  Even Elements : "+even+" \n" );
System.out.print("  Odd Elements:  "+odd+"\n" );
}
public static void main(String[] args) {
MyMatrix obj = new MyMatrix();
//Define matrix
int [][]matrix = {
{ 1, 2, 3, 4, 5 },
{ 4, 5, 6 , 11 , 12 },
{ 7, 8, 9 ,13, 14}
};
obj.show_data(matrix);
obj.frequencies(matrix);

}
}```
```

#### Output

``````  1  2  3  4  5
4  5  6  11  12
7  8  9  13  14

Even Elements : 7
Odd Elements: 8``````
``````using System;
/*
C# Program
Find frequencies of even and odd numbers in a matrix
*/

public class MyMatrix {
//Display matrix elements
public void show_data(int[,] matrix) {
//Get the size of matrix
int row = matrix.GetLength(0);
int col = matrix.GetLength(1);
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
Console.Write(" " + matrix[i,j]);
}
Console.Write("\n");
}
Console.Write("\n");
}
public void frequencies(int[,] matrix) {
//About number of even and odd elements
//Counter variable which are store the information
//About number of even and odd elements
int even = 0, odd = 0;
//Get the size of matrix
int row = matrix.GetLength(0);
int col = matrix.GetLength(1);
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
if (matrix[i,j] % 2 == 0) {
//When even element
even++;
} else {
//When get new odd number
odd++;
}
}
}
Console.Write(" Even Elements : " + even + " \n");
Console.Write(" Odd Elements: " + odd + "\n");
}
public static void Main(String[] args) {
MyMatrix obj = new MyMatrix();
//Define matrix
int[,] matrix = {
{
1,
2,
3,
4,
5
},
{
4,
5,
6,
11,
12
},
{
7,
8,
9,
13,
14
}
};
obj.show_data(matrix);
obj.frequencies(matrix);
}
}```
```

#### Output

`````` 1 2 3 4 5
4 5 6 11 12
7 8 9 13 14

Even Elements : 7
Odd Elements: 8``````
``````<?php
/*
Php Program
Find frequencies of even and odd numbers in a matrix
*/
class MyMatrix {
//Display matrix elements

public 	function show_data(\$matrix) {
//Get the size of matrix
\$row = count(\$matrix);
\$col = count(\$matrix[0]);
for (\$i = 0; \$i < \$row; ++\$i) {
for (\$j = 0; \$j < \$col; ++\$j) {
echo(" ". \$matrix[\$i][\$j]);
}
echo("\n");
}
echo("\n");
}
public 	function frequencies(\$matrix) {
//Counter variable which are store the information
//About number of even and odd elements
\$even = 0;
\$odd = 0;
//Get the size of matrix
\$row = count(\$matrix);
\$col = count(\$matrix[0]);
for (\$i = 0; \$i < \$row; ++\$i) {
for (\$j = 0; \$j < \$col; ++\$j) {
if (\$matrix[\$i][\$j] % 2 == 0) {
//When even element
\$even++;
} else {
//When get new odd number
\$odd++;
}
}
}
//Display the number of even and odd elements

echo(" Even Elements : ". \$even ." \n");
echo(" Odd Elements: ". \$odd ."\n");
}
};

function main() {
\$obj = new MyMatrix();
//Define matrix
\$matrix = array(array(1, 2, 3, 4, 5), array(4, 5, 6, 11, 12), array(7, 8, 9, 13, 14));
\$obj->show_data(\$matrix);
\$obj->frequencies(\$matrix);

}
main();```
```

#### Output

`````` 1 2 3 4 5
4 5 6 11 12
7 8 9 13 14

Even Elements : 7
Odd Elements: 8``````
``````/*
Node Js Program
Find frequencies of even and odd numbers in a matrix
*/
class MyMatrix {
//Display matrix elements
show_data(matrix) {
//Get the size of matrix
var row = matrix.length;
var col = matrix[0].length;
for (var i = 0; i < row; ++i) {
for (var j = 0; j < col; ++j) {
process.stdout.write(" " + matrix[i][j]);
}

process.stdout.write("\n");
}

process.stdout.write("\n");
}
frequencies(matrix) {
//Counter variable which are store the information
//About number of even and odd elements
var even = 0;
var odd = 0;
//Get the size of matrix
var row = matrix.length;
var col = matrix[0].length;
for (var i = 0; i < row; ++i) {
for (var j = 0; j < col; ++j) {
if (matrix[i][j] % 2 == 0) {
//When even element
even++;
} else {
//When get new odd number
odd++;
}
}
}

//Display the number of even and odd elements

process.stdout.write(" Even Elements : " + even + " \n");
process.stdout.write(" Odd Elements: " + odd + "\n");
}
}

function main(args) {
var obj = new MyMatrix();
//Define matrix
var matrix = [
[1, 2, 3, 4, 5],
[4, 5, 6, 11, 12],
[7, 8, 9, 13, 14]
];
obj.show_data(matrix);
obj.frequencies(matrix);
}
main();```
```

#### Output

`````` 1 2 3 4 5
4 5 6 11 12
7 8 9 13 14

Even Elements : 7
Odd Elements: 8``````
``````# Python 3 Program
# Find frequencies of even and odd numbers in a matrix
class MyMatrix :
# Display matrix elements
def show_data(self, matrix) :
row = len(matrix)
col = len(matrix[0])
i = 0
while (i < row) :
j = 0
while (j < col) :
print(" ", matrix[i][j], end = "")
j += 1

print(end = "\n")
i += 1

print(end = "\n")

def frequencies(self, matrix) :
even = 0
odd = 0
row = len(matrix)
col = len(matrix[0])
i = 0
while (i < row) :
j = 0
while (j < col) :
if (matrix[i][j] % 2 == 0) :
# When even element
even += 1
else :
# When get new odd number
odd += 1

j += 1

i += 1

print(" Even Elements : ", even ," \n", end = "")
print(" Odd Elements: ", odd ,"\n", end = "")

def main() :
obj = MyMatrix()
matrix = [
[1, 2, 3, 4, 5],
[4, 5, 6, 11, 12],
[7, 8, 9, 13, 14]
]
obj.show_data(matrix)
obj.frequencies(matrix)

if __name__ == "__main__":
main()```
```

#### Output

``````  1  2  3  4  5
4  5  6  11  12
7  8  9  13  14

Even Elements :  7
Odd Elements:  8``````
``````# Ruby Program
# Find frequencies of even and odd numbers in a matrix
class MyMatrix
# Display matrix elements
def show_data(matrix)
row = matrix.length
col = matrix[0].length
i = 0
while (i < row)
j = 0
while (j < col)
print(" ", matrix[i][j])
j += 1
end
print("\n")
i += 1
end
print("\n")
end
def frequencies(matrix)
even = 0
odd = 0
row = matrix.length
col = matrix[0].length
i = 0
while (i < row)
j = 0
while (j < col)
if (matrix[i][j] % 2 == 0)
# When even element
even += 1
else
# When get new odd number
odd += 1
end
j += 1
end
i += 1
end
print(" Even Elements  :", even ," \n")
print(" Odd Elements :", odd ,"\n")
end
end
def main()
obj = MyMatrix.new()
matrix = [
[1, 2, 3, 4, 5],
[4, 5, 6, 11, 12],
[7, 8, 9, 13, 14]
]
obj.show_data(matrix)
obj.frequencies(matrix)
end
main()```
```

#### Output

`````` 1 2 3 4 5
4 5 6 11 12
7 8 9 13 14

Even Elements  :7
Odd Elements :8
``````
``````/*
Scala Program
Find frequencies of even and odd numbers in a matrix
*/
class MyMatrix {
//Display matrix elements
def show_data(matrix: Array[Array[Int]]): Unit = {
val row: Int = matrix.length;
val col: Int = matrix(0).length;
var i: Int = 0;
while (i < row) {
var j: Int = 0;
while (j < col) {
print(" " + matrix(i)(j));
j += 1;
}
print("\n");
i += 1;
}
print("\n");
}
def frequencies(matrix: Array[Array[Int]]): Unit = {
var even: Int = 0;
var odd: Int = 0;
val row: Int = matrix.length;
val col: Int = matrix(0).length;
var i: Int = 0;
while (i < row) {
var j: Int = 0;
while (j < col) {
if (matrix(i)(j) % 2 == 0) {
//When even element
even += 1;
} else {
//When get new odd number
odd += 1;
}
j += 1;
}
i += 1;
}
print(" Even Elements : " + even + " \n");
print(" Odd Elements: " + odd + "\n");
}
}
object Main {
def main(args: Array[String]): Unit = {
val obj: MyMatrix = new MyMatrix();
val matrix: Array[Array[Int]] = Array(
Array(1, 2, 3, 4, 5),
Array(4, 5, 6, 11, 12),
Array(7, 8, 9, 13, 14));
obj.show_data(matrix);
obj.frequencies(matrix);
}
}```
```

#### Output

`````` 1 2 3 4 5
4 5 6 11 12
7 8 9 13 14

Even Elements : 7
Odd Elements: 8``````
``````/*
Swift 4 Program
Find frequencies of even and odd numbers in a matrix
*/
class MyMatrix {
//Display matrix elements
func show_data(_ matrix: [
[Int]
]) {
let row: Int = matrix.count;
let col: Int = matrix[0].count;
var i: Int = 0;
while (i < row) {
var j: Int = 0;
while (j < col) {
print(" ", matrix[i][j], terminator: "");
j += 1;
}
print("\n", terminator: "");
i += 1;
}
print("\n", terminator: "");
}
func frequencies(_ matrix: [
[Int]
]) {
var even: Int = 0;
var odd: Int = 0;
let row: Int = matrix.count;
let col: Int = matrix[0].count;
var i: Int = 0;
while (i < row) {
var j: Int = 0;
while (j < col) {
if (matrix[i][j] % 2 == 0) {
//When even element
even += 1;
} else {
//When get new odd number
odd += 1;
}
j += 1;
}
i += 1;
}
print(" Even Elements : ", even ," \n", terminator: "");
print(" Odd Elements: ", odd ,"\n", terminator: "");
}
}
func main() {
let obj: MyMatrix = MyMatrix();
let matrix: [
[Int]
] = [
[1, 2, 3, 4, 5],
[4, 5, 6, 11, 12],
[7, 8, 9, 13, 14]
];
obj.show_data(matrix);
obj.frequencies(matrix);
}
main();```
```

#### Output

``````  1  2  3  4  5
4  5  6  11  12
7  8  9  13  14

Even Elements :  7
Odd Elements:  8``````

## Output Explanation

The given matrix is displayed, and then the output indicates the counts of even and odd elements within the matrix. In this case, there are 7 even elements and 8 odd elements, as indicated by the output.

## Time Complexity

The time complexity of this algorithm is O(rows * cols), where rows is the number of rows in the matrix and cols is the number of columns in the matrix. This is because we iterate through each element of each row once in a nested loop structure.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post