Write a program to print the max elements in all the rows.

12 July 2022

Write a program to print the max elements in all the rows.


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Given an N x M matrix,  Write a program to print the max elements in all the rows.

Sample input :

3 3

1 2 3

6 5 4

12 30 1

Sample output :

3

6

30

 

C Program

#include <stdio.h>

 

int main()

{

            int r,c;

            scanf("%d%d",&r,&c);

            int mat[r]c];

            for(int i = 0 ; i<r ; i++){

                        for(int j = 0 ; j<c ; j++){

                                    scanf("%d",&mat[i][j]) ;

                        }

            }

            int max;

            for(int i = 0 ; i<r ; i++){

                        max = mat[i][0];

                        for(int j = 1 ; j<c ; j++){

                                    if(mat[i][j] > max){

                                                max = mat[i][j];

                                    }

                        }

                        printf("%d\n",max);

            }

            return 0;

}

 

C++ Program

#include <bits/stdc++.h>

using namespace std;

 

int main()

{

            int r,c;

            cin>>r>>c;

            int mat[r]c];

            for(int i = 0 ; i<r ; i++){

                        for(int j = 0 ; j<c ; j++){

                                    cin>>mat[i][j] ;

                        }

            }

            int max;

            for(int i = 0 ; i<r ; i++){

                        max = mat[i][0];

                        for(int j = 1 ; j<c ; j++){

                                    if(mat[i][j] > max){

                                                max = mat[i][j];

                                    }

                        }

                        cout<<mat[i][j];

            }

            return 0;

}

 

JAVA Program

import java.util.*;

import java.lang.*;

import java.io.*;

 

class Ideone

{

            public static void main (String[] args) throws java.lang.Exception

            {

                        Scanner sc=new Scanner(System.in);

                        int r = sc.nextInt();

                        int c = sc.nextInt();

                        int mat[][] = new int[r][c];

                        for(int i = 0 ; i<r ; i++){

                                    for(int j = 0 ; j<c ; j++){

                                                mat[i][j] = sc.nextInt();

                                    }

                        }

                        int max;

                        for(int i = 0 ; i<r ; i++){

                        max = mat[i][0];

                        for(int j = 1 ; j<c ; j++){

                                    if(mat[i][j] > max){

                                                max = mat[i][j];

                                    }

                        }

                        System.out.println(max);

            }

                       

            }

}

 

PYTHON Program

def MaxRowElement(a):

    for i in range(0,len(a)):

        m = 0

        for j in range(0,len(a[0])):

            if a[i][j] > m :

                m = a[i][j]

        print(m)

 

a = [[1, 2, 3],

       [4, 5, 6],

       [7, 8, 9],]

       

MaxRowElement(a)


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !