Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Program to Find Largest and Second Largest Number in 2D Array in CPP (C plus plus)

Program to Find Largest and Second Largest Number in 2D Array in CPP (C plus plus)

In this tutorial, we will learn about Program to Find Largest and Second Largest Number in 2D Array in CPP (C plus plus).

#include<iostream>
 
using namespace std;
 
int main()
{
    int array[5][5];
	int b1=1;
	int b2=0;
	int p;
	int q;
	int i;
	int j;
	
    cout<<"Please enter the number of rows and columns (MAX 5):";
    cin>>p>>q;
    
    cout<<"Please enter the array:"<<endl;
 
    for(i=0;i<p;i++)
        for(j=0;j<q;++j)
            cin>>array[i][j];
 
    for(i=0;i<p;++i)
        for(j=0;j<q;++j)
        {
            if(array[i][j]>b1)
                b1=array[i][j];
        }
 
    for(i=0;i<p;++i)
        for(j=0;j<q;++j)
        {
            if(array[i][j]>b2&&array[i][j]<b1)
                b2=array[i][j];
        }
 
    cout<<endl<<"Largest number is:"<<b1;
    cout<<endl<<"Second largest number is:"<<b2;
 
    return 0;
}

Output:

Program to Find Largest and Second Largest Number in 2D Array in CPP (C plus plus)

Program to Find Largest and Second Largest Number in 2D Array in CPP (C plus plus)