Page 376 - ComputerScience_Class_11
P. 376

38              index = 0; // Resetting index to use it for filling the 2D array
                39              for (int i = 0; i < 3; i++)

                40              {
                41                  for (int j = 0; j < 3; j++)

                42                  {
                43                      array[i][j] = flatArray[index++]; // Fill 2D array with sorted values

                44                    System.out.print(array[i][j] + " "); // Print the sorted 2D array
                45                  }

                46                  System.out.println();

                47              }
                48          }

                49      }

              The output of the preceding program is as follows:

                      BlueJ: Terminal Window - Java
                   Options

                  Enter 9 elements for the 3x3 array:
                  9
                  8
                  7
                  6
                  5
                  4
                  3
                  2
                  1
                  Array arranged in ascending order:
                  1 2 3

                  4 5 6
                  7 8 9




                Program 10     Write a Java program to accept number into a 3 × 4 matrix. Display the original array. Then
                               calculate and display each row total.
                1       class MatrixRowTotal

                2       {

                3           public static void main(String args[])





                  374  Touchpad Computer Science (Ver. 3.0)-XI
   371   372   373   374   375   376   377   378   379   380   381