Here is the C++ program to find largest and second largest number in a 2d array or matrix.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include<iostream> using namespace std; int main() { int a[5][5],big1=1,big2=0,n,m,i,j; cout<<"Enter no of rows and columns(max 5):"; cin>>m>>n; cout<<"Enter the array:\n"; for(i=0;i<m;i++) for(j=0;j<n;++j) cin>>a[i][j]; for(i=0;i<m;++i) for(j=0;j<n;++j) { if(a[i][j]>big1) big1=a[i][j]; } for(i=0;i<m;++i) for(j=0;j<n;++j) { if(a[i][j]>big2&&a[i][j]<big1) big2=a[i][j]; } cout<<"\nLargest number:"<<big1; cout<<"\nSecond largest number:"<<big2; return 0; } |

**Output**

*Enter no of rows and columns(max 5):3*

* 3*

* Enter the array:*

* 4 6 8*

* 2 4 6*

* 2 12 5*

* Largest number:12*

* Second largest number:8*

i would like to see thhis solution

find the maximum element in even rows of a matrix using C++ programming.

thank you

above code doesn’t work for this input

2*2

4 3

2 1

prints 4 for both

There was a little mistake in code, I have corrected it, now working fine. Thanks for mentioning the mistake.

Thanks