// Write a program to print the row and column indices of each // cell of an M x N matrix // // Example for M = 3, N = 4 print out: // 11 12 13 14 // 21 22 23 24 // 31 32 33 34 // // MatrixLabels1 and MatrixLabels2 both solve this problem using // while loops, but with slightly different loop conditions, // variable initialization, and location of increment statements. // MatrixLabels3 solves this problem using for loops; it is most // closely analogous to MatrixLabels1. public class MatrixLabels3 { public static void main(String[] args) { // how many rows and columns the matrix will have int NumRows, NumColumns; // read in the number of rows and columns // assume the values read in are positive integers TokenReader in = new TokenReader(System.in); System.out.println("Enter the number of rows in the matrix: "); NumRows = in.readInt(); System.out.println("Enter the number of columns in the matrix: "); NumColumns = in.readInt(); // leave a blank line before the output System.out.println(); // iterate over the rows for (int row = 1; row <= NumRows; ++row) { // iterate over the columns for (int column = 1; column <= NumColumns; ++column) { System.out.print(row + (column + " ")); } System.out.println(); } } }