DDA_Max_Min

Write a program to ask the user to input a double dimensional integer array and print the biggest(Maximum) and the smallest(Minimum) element of the array with its place in the array. Print the sum and the average of all the elements also.

import java.io.*;
class MinMaxDDA{
    public static void main() throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Enter no. of rows");
        int r=Integer.parseInt(br.readLine());
        System.out.println("Enter no. of columns");
        int c=Integer.parseInt(br.readLine());
        int m[][]=new int[c][r];

        for(int i=0;i<c;i++){
            for(int j=0;j<r;j++){
                System.out.println("Enter value for matrix");
                m[i][j]=Integer.parseInt(br.readLine());
            }
        }

        System.out.println("the matrix is:");
        for(int i=0;i<c;i++){
            for(int j=0;j<r;j++){
                System.out.print(m[i][j]+" ");
            }
            System.out.println();
        }

        int x=m[0][0];
        int y=m[0][0];
        int r1=0,c1=0,r2=0,c2=0;
        int sum=0;double avg=0.0;
        for(int i=0;i<c;i++){
            for(int j=0;j<r;j++){sum+=m[i][j];
                if(m[i][j]<x){
                    x=m[i][j];
                    r1=i;
                    c1=j;
                }
                else if(m[i][j]>y){
                    y=m[i][j];
                    r2=i;
                    c2=j;
                }
            }
        }
        avg=sum/(c*r);
        System.out.println("The max value in matrix is "+y);
        System.out.println("It is at"+r2 +"  "+c2 +" index ");
        System.out.println("The min value in matrix is "+x);
        System.out.println("It is at"+r1 +"  "+c1 +" index ");
        System.out.println("The sum of all the elements is "+sum);
        System.out.println("The average of the  matrix is "+avg);
    }
}
Output:

Output:

Enter no. of rows
3
Enter no. of columns
3
Enter value for matrix
8
Enter value for matrix
7
Enter value for matrix
6
Enter value for matrix
4
Enter value for matrix
5
Enter value for matrix
3
Enter value for matrix
2
Enter value for matrix
1
Enter value for matrix
9
the matrix is:
8 7 6
4 5 3
2 1 9
The max value in matrix is 9
It is at 2 2 index
The min value in matrix is 1
It is at 2 1 index
The sum of all the elements is 45
The average of the matrix is 5.0

LEAVE A REPLY

Please enter your comment!
Please enter your name here