Sie sind auf Seite 1von 7

Program:Given a square matrix list [ ] [ ] of order 3.

Input the value for n and the positive integers in the matrix and perform the following task: 1. Display the original matrix 2. Print the row and column position of the largest element of the matrix. 3. Print the row and column position of the second largest element of the matrix. 4. Sort the elements of the rows in the ascending order and display the new matrix. Sample data: INPUT List [] [ ] 513 746 982 OUTPUT 513 746 982 The largest element 9 is in row 3 and column 1 The second largest element 8 is in row 3 and column 2 Sorted list 135 467 289

Algorithm:Step1:- START. Step2:- Define a 2-D array. Step3:- Define main method and create an object of class. Step4:- Define a method take and accept the values entered by user. Step5:- Print the original array. Step6:- Define a method sort to sort the elements of the rows of the given array. Step7:- Define a method display to find the largest and second largest number in the array. It also stores the position the largest and second largest number. Step8:- Print the largest and second largest number with its position. Step9:- Print the sorted array. Step10:- STOP.

Source code:import java.io.*; class ArrayProgram { int array[][]=new int[3][3]; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int i,j; public void take() throws Exception { for(i=0;i< 3;i++) { for(j=0;j< 3;j++) { System.out.print("Enter Number:"); array[i][j]=Integer.parseInt(br.readLine()); } } System.out.println("\nValues as follows\n"); for(i=0;i< 3;i++) { for(j=0;j< 3;j++) { System.out.print(" "+array[i][j]); } System.out.println(); } } private void sort() { int k,temp; for(i=0;i< 3;i++) { for(j=0;j< 3;j++) { for(k=j+1;k< 3;k++) { if(array[i][j] >array[i][k])

{ temp=array[i][j]; array[i][j]=array[i][k]; array[i][k]=temp; } } } } System.out.println("\nValues as follows after sorting row wise\n"); for(i=0;i< 3;i++) { for(j=0;j< 3;j++) { System.out.print(" "+array[i][j]); } System.out.println(); } } private void display() { int max1=0,max2=0,maxi=0,maxj=0; for(i=0;i< 3;i++) { for(j=0;j< 3;j++) { if(array[i][j] >max1) { max1=array[i][j]; maxi=i+1; maxj=j+1; } } } System.out.println("Maximum value="+max1 + " and the row and column numbers are:"+maxi+","+maxj); for(i=0;i< 3;i++) {

for(j=0;j< 3;j++) { if(array[i][j] >max2 && array[i][j]< max1) { max2=array[i][j]; maxi=i+1; maxj=j+1; } } } System.out.println("2nd Maximum value="+max2 + " and the row and column numbers are:"+maxi+","+maxj); } public static void main(String args[])throws Exception { ArrayProgram object=new ArrayProgram(); object.take(); object.display(); object.sort(); } }

Output:Enter Number:1 Enter Number:5 Enter Number:9 Enter Number:7 Enter Number:5 Enter Number:3 Enter Number:8 Enter Number:6 Enter Number:2 Values as follows 159 753 862 Maximum value=9 and the row and column numbers are:1,3 2nd Maximum value=8 and the row and column numbers are:3,1 Values as follows after sorting row wise 159 357 268

Documentation:Variable name array[][] i j k temp max1 max2 maxi maxj

Type int int int int int int int int int

Uses To store the given array. Loop and Row counter. Loop and Column counter. Loop and column counter. To store value for swapping. To store the largest number. To store second largest no. To store Row number. To store Column number.

Das könnte Ihnen auch gefallen