
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count frequency of k in a matrix of size n where matrix(i, j) = i+j in C++
We are given a matrix of integer values and the task is to calculate the count of the frequency of a given integer variable let’s say, k in the matrix. The size of a matrix can depend upon the size the user wants and in the below program we are taking it to be 4X4. Matrix will be formed on the given condition i.e matrix(i, j) will be i+j. The index value of the first data in a matrix will be 0 and 0 i.e. matrix[0][0] = 0.
Input − int size = 4, k = 4
Output − count of 4 in given matrix 4x4 is 3
Explanation −
matrix[i][j] = i+j where i=j=4 Matrix[4][4] = { 0, 1, 2, 3 1, 2, 3, 4 2, 3, 4, 5 3, 4, 5, 6 } The number k i.e. 4 is occurring 3 times in a matrix.
Input − int size = 3, k = 1
Output − count of 2 in given matrix 4x4 is 2
Explanation −
matrix[i][j] = i+j where i=j=3 Matrix[3][3] = { 0, 1, 2 1, 2, 3 2, 3, 4 } The number k i.e. 1 is occurring 2 times in a given matrix.
Approach used in the below program is as follow
Input the size of a matrix of n x n and an integer value ‘k’ that will be searched in a matrix
Start the loop i from 0 till row size
Inside the loop start another loop j from 0 till column size
Set matrix[i][j] = i+j
Check IF matrix[i][j] = k
If yes, then increment the count by 1 else ignores the data.
Return the count
Print the result
Example
#include <cmath> #include <iostream> using namespace std; int count(int size, int k){ int count = 0; int matrix[size][size]; for(int i = 0;i<size;i++){ for(int j=0; j<size; j++){ matrix[i][j] = i+j; if(matrix[i][j] == k){ count++; } } } return count; } int main(){ int size = 4; int k = 4; int total = count(size, k); if(total>0){ cout<<"Count of frequency of "<<k<<" in a matrix of size "<<size<<"X"<<vsize<<" where matrix(i, j) = i+j is: "<<total; } else { cout<<"Frequency of element is 0 that means it is not present in a matrix"; } }
Output
If we run the above code we will get the following output −
Count of frequency of 4 in a matrix of size 4X4 where matrix(i, j) = i+j is: 3
- Related Articles
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized in C++
- Maximum difference of indices (i, j) such that A[i][j] = 0 in the given matrix in C++
- Python - Count the frequency of matrix row length
- How to rotate a matrix of size n*n to 90-degree k times using C#?
- How to print a matrix of size n*n in spiral order using C#?
- Program to find k where given matrix has k by k square of same value in C++
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in Python
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in C++
- How to rotate a matrix of size n*n to 90 degree using C#?
- Matrix creation of n*n in Python
- Count the triplets such that A[i] < B[j] < C[k] in C++
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
- Count of unique pairs (arr[i], arr[j]) such that i < j in C++
- Minimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k])) of three different sorted arrays in Python
- Minimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k])) of three different sorted arrays in C++
