Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method named printGrid that accepts two integer parameters rows and cols

ID: 3557300 • Letter: W

Question

Write a method named printGrid that accepts two integer parameters rows and cols. The output is a comma-separated grid of numbers where the first parameter (rows) represents the number of rows of the grid and the second parameter (cols) represents the number of columns. The numbers count up from 1 to (rows x cols). The output are displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column to the right once the bottom of the current column is reached. Assume that rows and cols are greater than 0. Here are some example calls to your method and their expected results: Call: printGrid(3, 6); printGrid(5, 3); printGrid(4, 1); printGrid(1, 3); Output: 1, 4, 7, 10, 13, 16 2, 5, 8, 11, 14, 17 3, 6, 9, 12, 15, 18 1, 6, 11 2, 7, 12 3, 8, 13 4, 9, 14 5, 10, 15 1 2 3 4 1, 2, 3

Explanation / Answer

public static void printGrid(int iRows, int iCols)
    {
        StringBuilder sb = new StringBuilder();
        for (int r = 1; r <= iRows; ++r)
        {
            for (int c = 1; c <= iCols; ++c)
            {
                if (c > 1)
                {
                    sb.append(", ");
                }
                sb.append((iRows * (c - 1)) + r);
            }
            sb.append(" ");
        }
        
        System.out.println(sb.toString());
    }  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote