Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Two_D_array_CMPS280_2016 {
  4.  
  5. // Initialize the array
  6. double iDoc[][] = {
  7. {1,2,3,4},
  8. {5,6,7,8},
  9. {9,10,11,12},
  10. {13,14,15,16}
  11. };
  12.  
  13. public static void main(String[] args) {
  14. Two_D_array_CMPS280_2016 array = new Two_D_array_CMPS280_2016();
  15. Scanner sc = new Scanner(System.in);
  16. int input = 0;
  17.  
  18. System.out.println("Total of all elements: " + array.Total_2DArray_Elements());
  19. System.out.println("Average: " + array.Average_2DArray_Elements());
  20.  
  21. System.out.println("Enter row: ");
  22. input = sc.nextInt();
  23. System.out.println("Total of elements on row: " + array.Total_2DArray_Elements_by_Certain_RowNo(input));
  24.  
  25. System.out.println("Enter column: ");
  26. input = sc.nextInt();
  27. System.out.println("Total of elements in column: " + array.Total_2DArray_Elements_by_CertainColumnNo((input)));
  28.  
  29. System.out.println("Enter row: ");
  30. input = sc.nextInt();
  31. System.out.println("Largest value in row: " + array.The_Largest_Value_In_Certain_RowNo(input));
  32.  
  33. System.out.println("Enter column: ");
  34. input = sc.nextInt();
  35. System.out.println("Largest value in col: " + array.The_Largest_Value_In_Certain_ColumnNo((input)));
  36. }
  37.  
  38.  
  39. // Total all elements in the array
  40. double Total_2DArray_Elements() {
  41. double sum = 0.0;
  42.  
  43. for (int i = 0; i < iDoc.length; ++i) {
  44. for (int j = 0; j < iDoc[i].length; ++j) {
  45. sum += iDoc[i][j];
  46. }
  47. }
  48.  
  49. return sum;
  50. }
  51.  
  52. // Return the average of all elements in the array
  53. double Average_2DArray_Elements() {
  54. int count = iDoc.length * iDoc[0].length; // assuming each row is of equal length
  55. double sum = Total_2DArray_Elements();
  56.  
  57. return sum / count;
  58. }
  59.  
  60.  
  61. // Return the total of all of the elements in the given row
  62. double Total_2DArray_Elements_by_Certain_RowNo(int row) {
  63. double sum = 0.0;
  64.  
  65. for (int i = 0; i < iDoc[row].length; ++i) {
  66. sum += iDoc[row][i];
  67. }
  68.  
  69. return sum;
  70. }
  71.  
  72. // Return the total of all of the elements in the given column
  73. double Total_2DArray_Elements_by_CertainColumnNo(int col) {
  74. double sum = 0.0;
  75.  
  76. for (int i = 0; i < iDoc.length; ++i) {
  77. sum += iDoc[i][col];
  78. }
  79.  
  80. return sum;
  81. }
  82.  
  83. // Return the largest value in the given row
  84. double The_Largest_Value_In_Certain_RowNo(int row) {
  85. double max = iDoc[row][0];
  86.  
  87. for (int i = 0; i < iDoc[row].length; ++i) {
  88. if (iDoc[row][i] > max) max = iDoc[row][i];
  89. }
  90.  
  91. return max;
  92. }
  93.  
  94. // Return the largest value in the given column
  95. double The_Largest_Value_In_Certain_ColumnNo(int col) {
  96. double max = iDoc[0][col];
  97.  
  98. for (int i = 0; i < iDoc.length; ++i) {
  99. if (iDoc[i][col] > max) max = iDoc[i][col];
  100. }
  101.  
  102. return max;
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement