Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. 1 2 3 4
  2. 2 4 6 8
  3. 2 4 6 8
  4. 3 2 3 4
  5.  
  6. import java.util.*; // Scanner class
  7. import java.io.*; // File class
  8.  
  9. public class Lab10
  10. {
  11. static public void main( String [ ] args ) throws Exception
  12. {
  13. if ( args.length != 1 )
  14. {
  15. System.out.println("Error -- usage is: java Lab10 matdataN.txt");
  16. System.exit( 0 );
  17. }
  18.  
  19. //Requirement #1: first int value: # of rows, second int value: # of cols
  20. File newFile = new File(args[0]);
  21. Scanner in = new Scanner(newFile);
  22.  
  23. int numRows = in.nextInt();
  24. int numCols = in.nextInt();
  25.  
  26. //Requirement #2: declare two-d array of ints
  27. int[][] matrix;
  28. matrix = new int[numRows][numCols];
  29.  
  30. //Requirement #3 & 4: read file one line at a time (nested for loops
  31. //and nextInt()) and print
  32.  
  33. for (int i = 0; i < numRows; i++)
  34. {
  35. for (int j = 0; j < numCols; j++)
  36. {
  37. matrix[i][j] = in.nextInt();
  38. System.out.print(matrix[i][j]+ " ");
  39. }
  40. System.out.println();
  41. }
  42.  
  43. //Requirement #5: traverse each row and sum the values and display the sums
  44. int rowTotal = 0;
  45. for (int i = 0; i < numRows; i++)
  46. {
  47. rowTotal = 0;
  48. for (int j = 0; j < numCols; j++)
  49. {
  50. rowTotal += matrix[i][j];
  51. }
  52. System.out.println("Sum for row = " + rowTotal);
  53. }
  54.  
  55. //Requirement #6: traverse each column and sum the values and display the sums
  56. int colTotal = 0;
  57. for (int i = 0; i < numRows; i++)
  58. {
  59. colTotal = 0;
  60. for (int j = 0; j < numCols; j++)
  61. {
  62. colTotal += matrix[j][i];
  63. }
  64. System.out.println("Sum for col = " + colTotal);
  65. }
  66.  
  67. //Requirement #7: traverse the perimeter and sum the values and display the sum
  68.  
  69. //sum bottom row matrix
  70. int perTotal = 0;
  71. for (int i = (numRows-1); i < numRows; i++)
  72. {
  73. perTotal = 0;
  74. for (int j = 0; j < numCols; j++)
  75. {
  76. perTotal += matrix[i][j];
  77. }
  78. }
  79.  
  80. //sum + top row matrix
  81. for (int i = 0; i < numRows - (numRows-1); i++)
  82. {
  83. for (int j = 0; j < numCols; j++)
  84. {
  85. perTotal += matrix[i][j];
  86. }
  87. System.out.println("Sum of perimeter = " + perTotal);
  88. }
  89.  
  90. // sum + first col middle
  91. for (int i = 1; i < (numRows-1); i++)
  92. {
  93. for (int j = 0; j < numCols - (numCols-1); j++)
  94. {
  95. perTotal += matrix[j][i];
  96. }
  97. System.out.println("Sum = " + perTotal);
  98. }
  99.  
  100. // sum + last col middle
  101. for (int i = 1; i < (numRows-1); i++)
  102. {
  103. for (int j = (numCols-1); j < numCols; j++)
  104. {
  105. perTotal += matrix[j][i];
  106. }
  107. System.out.println(perTotal);
  108. }
  109.  
  110. }
  111.  
  112. int perTotal = 0;
  113. // top and bottom row
  114. for (int c = 0; c < numCols; c++)
  115. perTotal += matrix[0][c] + matrix[numRows-1][c];
  116. // left and right column
  117. for (int r = 1; r < numRows-1; r++)
  118. perTotal += matrix[r][0] + matrix[r][numCols-1];
  119.  
  120. // output
  121. System.out.println("Perimeter=" + perTotal);
  122.  
  123. public static int perimeter(int[][] array) {
  124. int perimter = 0;
  125. for (int i = 0; i < array[0].length; i++) {
  126. perimter += array[0][i] + array[array.length - 1][i];
  127. }
  128. for (int r = 1; r < array.length - 1; r++) {
  129. perimter += array[r][0] + array[r][array[0].length - 1];
  130. }
  131. return perimter;
  132. }
  133.  
  134. Matrix a = new Basic2DMatrix(...); // creates a real matrix
  135.  
  136. // calculates the sum of '1' row
  137. double d1 = a.foldRow(1, Matrices.asSumAccumulator(0));
  138. // calculates the sum of '2'
  139. double d2 = a.foldColumn(2, Matrices.asSumAccumulator(0));
  140.  
  141. // the helper class that fetches border elements of matrix
  142. class PerimeterFetcher implements MatrixFunction {
  143.  
  144. private int rows;
  145. private int columns;
  146.  
  147. public PerimeterFectcher(int rows, int columns) {
  148. this.rows = rows;
  149. this.columns = columns;
  150. }
  151.  
  152. @Override
  153. public double evaluate(int i, int j, double value) {
  154. return i == 0 ? value : j == 0 ? value : (i + 1) == rows ? value
  155. : (j + 1) == columns ? value : 0;
  156. }
  157. }
  158.  
  159. // calculates the perimeter of matrix
  160. double p = a.fold(Matrices.asSumFunctionAccumulator(0,
  161. new PerimeterFetcher(a.rows(), a.columns())));
  162.  
  163. Matrix a = new Basic2DMatrix(...);
  164. double s = a.sum(); // wrapper around a.fold(...);
  165.  
  166. //Requirement #7: traverse the perimeter and sum the values and display the sum
  167.  
  168. int perTotal = 0;
  169.  
  170. // First line
  171. for (int j = 0; j < numCols; j++)
  172. {
  173. perTotal += matrix[0][j];
  174. }
  175.  
  176. // If there is only one line, then it is done.
  177. if (numRows > 1)
  178. {
  179. // Last line
  180. for (int j = 0; j < numCols; j++)
  181. {
  182. perTotal += matrix[numRows-1][j];
  183. }
  184.  
  185. // Other lines
  186. for (int i = 1; i < numRows -1); i++)
  187. {
  188. perTotal += matrix[i][0] + matrix[i][numcols -1];
  189. }
  190. }
  191.  
  192. //Perimeter
  193. System.out.println("Perimter="+perTotal);
  194.  
  195. int perTotal = 0;
  196. for (int i = 0; i < numCols; i++)
  197. {
  198. perTotal += matrix[0][i] + matrix[numRows-1][i];
  199. }
  200. for (int j = 1; j < numRows-1; j++)
  201. {
  202. perTotal += matrix[j][0] + matrix[j][numCols-1];
  203. }
  204. System.out.println("Perimeter = " + perTotal);
  205.  
  206. for (int i = 0; i < ROWS; i++){
  207. for (int j = 0; j < COLUMNS; j++){
  208. sum = sum + myArray[i][j];
  209. }
  210. }
  211.  
  212. System.out.print(sum);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement