
- 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 all sorted rows in a matrix in C++
In this tutorial, we will be discussing a program to find the number of all sorted rows in a matrix.
For this we will be provided with m*n matrix. Our task is to count all the rows in the given matrix that are sorted either in ascending or descending order.
Example
#include <bits/stdc++.h> #define MAX 100 using namespace std; //counting sorted rows int count_srows(int mat[][MAX], int r, int c){ int result = 0; for (int i=0; i<r; i++){ int j; for (j=0; j<c-1; j++) if (mat[i][j+1] <= mat[i][j]) break; if (j == c-1) result++; } for (int i=0; i<r; i++){ int j; for (j=c-1; j>0; j--) if (mat[i][j-1] <= mat[i][j]) break; if (c > 1 && j == 0) result++; } return result; } int main(){ int m = 4, n = 5; int mat[][MAX] = {{1, 2, 3, 4, 5}, { 4, 3, 1, 2, 6}, {8, 7, 6, 5, 4}, {5, 7, 8, 9, 10}}; cout << count_srows(mat, m, n); return 0; }
Output
3
- Related Articles
- Count all the columns in a matrix which are sorted in descending in C++
- Find a common element in all rows of a given row-wise sorted matrix in C++
- Find Number of Sorted Rows in a Matrix in Java?
- Find the Kth Smallest Sum of a Matrix With Sorted Rows in C++
- Count rows in a matrix that consist of same element in C++
- How to count all characters in all rows of a field in MySQL?
- Count elements smaller than or equal to x in a sorted matrix in C++
- Exact count of all rows in MySQL database?
- To print all elements in sorted order from row and column wise sorted matrix in Python
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix using Python?
- Find distinct elements common to all rows of a matrix in Python
- Find distinct elements common to all rows of a Matrix in C++
- Absolute distinct count in a sorted array?
- Python Program to sort rows of a matrix by custom element count
- Kth Smallest Element in a Sorted Matrix in Python

Advertisements