Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count all the columns in a matrix which are sorted in descending in C++
In this tutorial, we will be discussing a program to find the number of columns in a matrix which are sorted in descending.
For this we will be provided with a matrix. Our task is to count the number of columns in the matrix having elements sorted in descending order.
Example
#include <bits/stdc++.h>
#define MAX 100
using namespace std;
//counting columns sorted in descending order
int count_dcolumns(int mat[][MAX], int r, int c){
int result = 0;
for (int i=0; i<c; i++){
int j;
for (j=r-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 = 2, n = 2;
int mat[][MAX] = {{1, 3}, {0, 2,}};
cout << count_dcolumns(mat, m, n);
return 0;
}
Output
2
Advertisements