Guest User

Untitled

a guest
Jul 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. /*
  2. * Saddle.java
  3. * Written by: Dominic Wroblewski
  4. * First written: 16/11/09
  5. * Last updated: 17/11/09
  6. */
  7.  
  8.  
  9.  
  10. /*
  11.  
  12. Algorithm for program:
  13.  
  14. * Get grid size from user input
  15. * Create new 2D array using inputted grid size
  16. * Find least value in each row
  17. * Find max value in each column
  18. * Find largest value in the row minima
  19. * Find smallest value in the column maximums
  20. * If largest value in row minima is same as smallest in coloumn maxima then theres a saddle point
  21. * Output location of saddle point
  22. * Output number of iterations taken using the count variable
  23.  
  24. */
  25.  
  26.  
  27.  
  28. import sheffield.*;
  29.  
  30. public class Saddle {
  31. public static void main(String[] args) {
  32.  
  33. // Creating a new instance of EasyReader
  34. EasyReader keyboard = new EasyReader();
  35.  
  36. System.out.println();
  37.  
  38. // Getting the grid size from user input
  39. final int gridSize = keyboard.readInt("Enter the grid size: ");
  40.  
  41. System.out.println();
  42.  
  43. // Creating a new grid with the grid size entered by user
  44. int[][] grid = new int[gridSize][gridSize];
  45.  
  46. // Initialising some values
  47. boolean foundSaddlePoint = false;
  48. int maximaLocation = 0;
  49. int minimaLocation = 0;
  50.  
  51. // Creating the count, used for counting how many iterations have been made by the program
  52. int count = 0;
  53.  
  54. do {
  55. // Populating the grid with random numbers generated by the program
  56. for (int i=0; i<gridSize; i++){
  57. for (int j=0; j<gridSize; j++){
  58. // Creating the random number between 0 and 9 and storing into the grid
  59. int randomNumber = (int)(Math.random() * 9);
  60. grid[i][j]=randomNumber;
  61. }
  62. }
  63.  
  64. // Producing an output of the grid to the user
  65. for (int[] row : grid) {
  66. for (int col : row)
  67. System.out.print(col+" ");
  68. System.out.println();
  69. }
  70.  
  71. System.out.println();
  72.  
  73. // Find the minimum value in each row
  74.  
  75. int[] lowestInRows = new int[grid.length];
  76.  
  77. for (int i = 0; i<grid.length; i++) {
  78. lowestInRows[i] = grid[i][0];
  79. for (int j=0; j<grid[i].length; j++) {
  80. if ((lowestInRows[i]) > (grid[i][j])) {
  81. lowestInRows[i] = grid[i][j];
  82. }
  83. }
  84. }
  85.  
  86.  
  87. // Find the maximum value in each column
  88.  
  89. int[] maxInColumns = new int[grid.length];
  90.  
  91. for (int j=0; j<gridSize; j++) {
  92. maxInColumns[j] = grid[0][j];
  93. for (int i=0; i<gridSize; i++) {
  94. if (maxInColumns[j] < grid[i][j]) {
  95. maxInColumns[j] = grid[i][j];
  96. }
  97. }
  98. }
  99.  
  100.  
  101. // Find largest value in row minima
  102.  
  103.  
  104. int largestMinima = lowestInRows[0];
  105.  
  106. for (int i=0; i<gridSize; i++) {
  107. if (lowestInRows[i] >= largestMinima) {
  108. largestMinima = lowestInRows[i];
  109. minimaLocation = i;
  110. }
  111. }
  112.  
  113.  
  114. // Find the smallest value in the column maxima
  115.  
  116.  
  117. int smallestMaxima = maxInColumns[0];
  118.  
  119. for (int i=0; i<gridSize; i++) {
  120. if (maxInColumns[i] <= smallestMaxima) {
  121. smallestMaxima = maxInColumns[i];
  122. maximaLocation = i;
  123. }
  124. }
  125.  
  126. // Adding 1 to the count for the counter of the number of iterations
  127. count++;
  128.  
  129. // If a saddle point is made, then exit the DO WHILE loop
  130. if (smallestMaxima == largestMinima) {
  131. foundSaddlePoint = true;
  132. }
  133.  
  134. } while (!foundSaddlePoint);
  135.  
  136. // Outputting the location of the saddle point
  137. System.out.println("There is a saddle point at ("+minimaLocation+","+maximaLocation+")");
  138.  
  139. // Outputting the number of iterations the program ran before finding a saddle point
  140. System.out.println("It took "+count+" iterations to find one");
  141. System.out.println();
  142.  
  143. }
  144. }
Add Comment
Please, Sign In to add comment