Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. arrayName[row][col] = holder;
  2.  
  3. package workfiles;
  4.  
  5. import java.util.*;
  6. import java.util.Scanner;
  7.  
  8. public class prob2 {
  9.  
  10. // Do not modify this method
  11. public static void main(String[] args) {
  12.  
  13. try
  14. {
  15. int [][] iArray = enter2DPosArray();
  16. System.out.println("The original array values:");
  17. print2DIArray(iArray);
  18. int [][] tArray = transposition(iArray);
  19. System.out.println("The transposed array values:");
  20. print2DIArray(tArray);
  21. }
  22.  
  23. catch (InputMismatchException exception)
  24. {
  25. System.out.println("The array entry failed. The program will now halt.");
  26. }
  27.  
  28. }
  29.  
  30. // A function that prints a 2D integer array to standard output
  31. // It prints each row on one line with newlines between rows
  32. public static void print2DIArray(int[][] output) {
  33.  
  34. }
  35.  
  36. // A function that enters a 2D integer array from the user
  37. // It raises an InputMismatchException if the user enters anything other
  38. // than positive (> 0) values for the number of rows, the number of
  39. // columns, or any array entry
  40. public static int[][] enter2DPosArray() throws InputMismatchException {
  41.  
  42. int row=0;
  43. int col=0;
  44. int arow=0;
  45. int acol=0;
  46. int holder=0;
  47. Scanner numScan = new Scanner(System.in);
  48.  
  49. while (row<=0){
  50. System.out.print("How many rows (>0) should the array have? ");
  51. row = numScan.nextInt();
  52. }
  53.  
  54. while (col<=0){
  55. System.out.print("How many columns (>0) should the array have? ");
  56. col = numScan.nextInt();
  57. }
  58. int[][] arrayName = new int[row+1][col+1];
  59.  
  60. while (arow < row) {
  61.  
  62. if (acol<=col)
  63. System.out.println("Enter a positive (> 0) integer value: ");
  64. holder = numScan.nextInt();
  65. arrayName[arow][acol] = holder;
  66. acol ++;
  67.  
  68. if (acol>col)
  69. acol=0;
  70. arow ++;
  71. System.out.println("Enter a positive (> 0) integer value: ");
  72. holder = numScan.nextInt();
  73. arrayName[arow][acol] = holder;
  74. acol ++;
  75.  
  76.  
  77.  
  78. }
  79. //arrayName[i][j]
  80. numScan.close();
  81. return arrayName;
  82. }
  83.  
  84. public static int[][] transposition(int [][] arrayName) {
  85.  
  86. int r=0, c=0;
  87.  
  88. int[][] transpose = new int[r][c];
  89. for (int i = 0; i < r; i++) {
  90. for (int j = 0; j < c; j++) {
  91. transpose[i][j] = arrayName[j][i];
  92. }
  93. }
  94. return transpose;
  95. }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement