// 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 MatrixLabels1 { public static void main(String[] args) { // how many rows and columns the matrix will have int NumRows, NumColumns; // current row and column being processed int row = 1, column = 1; // 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 - assumes row starts as 1 while (row <= NumRows) { column = 1; // iterate over the columns - assumes column starts as 1 while (column <= NumColumns) { System.out.print(row + (column + " ")); ++column; } System.out.println(); ++row; } } }