hpilo

Chapter5_Arrays_Ex11

Dec 15th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. import java.util.Scanner;
  2. /*
  3. =====================================================
  4. chapter 5: Arrays
  5.  
  6. Ex11: Display matrix and max value placed in frame
  7. =====================================================
  8. */
  9.  
  10. public class MyProgram {
  11. public static void main(String[] args) {
  12. final int SIZE=10;
  13. int matrix[][]=new int[SIZE][SIZE];
  14. int row=0,col=0;
  15. int max;
  16. Scanner s=new Scanner(System.in);
  17. boolean loop=true;
  18.  
  19. System.out.println("Enter rows: ");
  20. row=s.nextInt();
  21.  
  22. while(row<=0 || row>SIZE){
  23. System.out.println("try again: ");
  24. row=s.nextInt();
  25. }
  26.  
  27. System.out.println("Enter cols: ");
  28. col=s.nextInt();
  29.  
  30. while(col<=0 || col>SIZE){
  31. System.out.println("try again: ");
  32. col=s.nextInt();
  33. }
  34. System.out.println("Enter "+row*col+" values: ");
  35.  
  36. max=matrix[0][0];
  37.  
  38. for(int i=0;i<row;i++){
  39. for(int j=0;j<col;j++){
  40. matrix[i][j]=s.nextInt();
  41. System.out.print(matrix[i][j]+"\t\t");
  42. if( (i==0 || i==row-1 ) && matrix[i][j]>max)
  43. max=matrix[i][j];
  44. if( (i!=0 && i!=row-1 && j==0 ) && matrix[i][j]>max )
  45. max=matrix[i][j];
  46. if( (i!=0 && i!=row-1 && j==col-1 ) && matrix[i][j]>max )
  47. max=matrix[i][j];
  48. }
  49. System.out.println();
  50. }
  51. System.out.println("max frame value: "+max);
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment