Skip to content
ExamSpine

Lesson 3.7 · Write it

Grid coordinates

A whole programPython or Java6 tests, 3 of them hidden

On a phone? Put the lines in order instead

Write a program that reads a number of rows and a number of columns, and prints every coordinate in the grid, row by row, starting from 0.

  1. Read the number of rows on the first line and the number of columns on the second line.
  2. For each row from 0 up to rows minus 1, and for each column from 0 up to cols minus 1, print the row number, then a comma with no spaces, then the column number.
  3. Print one coordinate per line.
  4. Visit all columns of a row before moving to the next row.

Both numbers are always whole numbers from 1 to 20. Your program is run several times. Only what it prints is checked.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 6 times: the 3 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • a 2 by 3 grid (rules 2 and 4)

    You type

    1. typed: 2
    2. typed: 3

    It prints

    0,0
    0,1
    0,2
    1,0
    1,1
    1,2
  • one row, two columns (rule 2)

    You type

    1. typed: 1
    2. typed: 2

    It prints

    0,0
    0,1
  • three rows, one column (rule 2)

    You type

    1. typed: 3
    2. typed: 1

    It prints

    0,0
    1,0
    2,0
solution.pySolution.java