Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmC++ Program for Frequencies of even and odd numbers in a matrix

C++ Program for Frequencies of even and odd numbers in a matrix

Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in the matrix 
Examples: 

Input : m = 3, n = 3
        { 1, 2, 3 }, 
        { 4, 5, 6 }, 
        { 7, 8, 9 }
Output : Frequency of odd number =  5 
         Frequency of even number = 4


Input :   m = 3, n = 3
         { 10, 11, 12 },
         { 13, 14, 15 },
         { 16, 17, 18 }
Output : Frequency of odd number  =  4 
         Frequency of even number  = 5
 

CPP




// C++ Program to Find the frequency
// of even and odd numbers in a matrix
#include <bits/stdc++.h>
using namespace std;
 
#define MAX 100
 
// function for calculating frequency
void freq(int ar[][MAX], int m, int n)
{
    int even = 0, odd = 0;
 
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            // modulo by 2 to check
            // even and odd
            if ((ar[i][j] % 2) == 0)
                ++even;
            else
                ++odd;
        }
    }
 
    // print Frequency of numbers
    cout << "Frequency of odd number = " << odd << endl;
    cout << "Frequency of even number = " << even << endl;
}
 
// Driver code
int main()
{
    int m = 3, n = 3;
 
    int array[][MAX]
        = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
 
    freq(array, m, n);
    return 0;
}


Output

Frequency of odd number = 5
Frequency of even number = 4

Time Complexity: O(m*n), Where m is the number of rows and n is the number of columns in the given matrix.
Auxiliary Space: O(1)

Approach: Using Recursive method

C++




// C++ Program to Find the frequency
// of even and odd numbers in a matrix
#include <bits/stdc++.h>
using namespace std;
 
#define MAX 100
int even = 0, odd = 0;
 
void freq(int ar[][MAX], int m, int n, int i, int j) {
    if (i == m) //base condition
    {
        cout << "Frequency of odd number = " << odd << endl;
        cout << "Frequency of even number = " << even << endl;
        return;
    }
    if (j == n)
    {
        freq(ar, m, n, i+1, 0); //recursive call
        return;
    }
    if (ar[i][j] & 1)
        ++odd;
    else
        ++even;
    freq(ar, m, n, i, j+1);  //recursive call
}
 
int main() {
    int m = 3, n = 3;
    int array[][MAX] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    freq(array, m, n, 0, 0);
    return 0;
}


Output

Frequency of odd number = 5
Frequency of even number = 4

Time Complexity: O(m*n), Where m is the number of rows and n is the number of columns in the given matrix.
Auxiliary Space: O(m*n) , Each call to the function freq takes O(1) space in the call stack and the number of calls is m*n.

Please refer complete article on Frequencies of even and odd numbers in a matrix for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Last Updated :
09 Feb, 2023
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments