Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. public static int checkRowForSameness(int[][] matrix, int i) {
  2. int testValue = matrix[i][0];
  3. // If all values in row i are the same, then return that value.
  4. // Otherwise, return -1.
  5. for (int j = 1; j < matrix[i].length; j++) {
  6. if (testValue != matrix[i][j]) {
  7. return -1;
  8. }
  9. }
  10.  
  11. return matrix[i][0];
  12. }
  13.  
  14. public static void rowChecks(int[][] matrix) {
  15. // Check which rows have all of the same entries.
  16. // Display a message for each such row.
  17. // Display a different message if none of the rows
  18. // have all the same entries.
  19. // Precondition: none of the entries are -1.
  20. for (int i = 0; i < matrix.length; i++) {
  21. //Call checkRowForSameness
  22. int rowSame = checkRowForSameness(matrix, i);
  23. if (rowSame == -1) {
  24. System.out.println("Not same numbers on row " + i);
  25. }
  26. else if (rowSame == 1) {
  27. System.out.println("All 1s on row " + i);
  28. }
  29. else {
  30. System.out.println("All 0s on row " + i);
  31. }
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement