Dremendo Tag Line

Input numbers in 2d array and print the largest number in each row in C++

Two Dimensional Array - Question 9

In this question, we will see how to input numbers in a 3X3 integer matrix (2d array) and print the largest number in each row in C++ programming. To know more about two dimensional array click on the two dimensional array lesson.

Q9) Write a program in C++ to input numbers in a 3X3 integer matrix (2d array) and print the largest number in each row.

Program

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int a[3][3], r,c,lg,x=0;

    cout<<"Enter 9 numbers\n";
    for(r=0; r<3; r++)
    {
        for(c=0; c<3; c++)
        {
            cin>>a[r][c];
        }
    }

    for(r=0; r<3; r++)
    {
        x=0;
        for(c=0; c<3; c++)
        {
            cout<<a[r][c]<<" ";
            if(x==0)
            {
                lg=a[r][c];
                x=1;
            }
            else if(a[r][c]>lg)
            {
                lg=a[r][c];
            }
        }
        cout<<"  Largest Number = "<<lg<<endl;
    }
    return 0;
}

Output

Enter 9 numbers
18
12
5
10
15
45
38
72
64
18 12 5   Largest Number = 18
10 15 45   Largest Number = 45
38 72 64   Largest Number = 72
video-poster