Learning C++ Programming from Scratch: Day 424 – Angled II; Problem Set Answers; Second Method

1193 – Angled II

Learning C++ Programming from Scratch: Day 424 - Angled II; Problem Set Answers; Second Method

Program Development Approach (For Beginners)

Hello everyone! Today we will understand step by step how the “Angled II” program is designed and implemented.

Step 1: Understand the Problem Requirements

We need to create an n×n square matrix. Observing the output example, for instance when n=5:

Dark version

  1  2  3  4  5
  2  3  4  5  4
  3  4  5  4  3
  4  5  4  3  2
  5  4  3  2  1

We find that:

  • The first row is: 1, 2, 3, …, n
  • The second row is like the first row shifted left by one position, with the rightmost position filled with <span>n-1</span> (which is 4 here)
  • The third row is again shifted left by one position, with the rightmost position filled with <span>n-2</span> (which is 3 here)
  • And so on, each row is “shifted left” by one position and a decreasing number is added at the end.

This “shift left” actually means: removing the first number and shifting all subsequent numbers one position to the left.

Step 2: Choose Data Structure

We need to store an n×n table, so we will use a two-dimensional array <span>matrix[10][10]</span>. The reason for using 10 is that the problem states n does not exceed 10, which is sufficient.

Step 3: Fill the First Row

The first row is simple, it is arranged in order from 1 to n. We use a loop:

for (int j = 0; j < n; j++) {
    matrix[0][j] = j + 1;
}

Thus when <span>j=0</span> it places 1, when <span>j=1</span> it places 2, …, when <span>j=n-1</span> it places n.

Step 4: Generate Subsequent Rows

Starting from the first row (i=1), each row depends on the previous row:

  1. Copy the values from the second element of the previous row to the current row’s front.
  • For example, if the previous row is <span>2,3,4,5</span>, we copy <span>3,4,5</span> to the first three positions of the current row.
  • Then place a decreasing number at the last position of the current row: <span>n - i</span>
    • For the first row (i=1) place <span>n-1</span>
    • For the second row (i=2) place <span>n-2</span>
    • ……
    • For the n-1 row (i=n-1) place <span>n-(n-1)=1</span>

    The code is as follows:

    for (int i = 1; i < n; i++) {
        for (int j = 0; j < n-1; j++) {
            matrix[i][j] = matrix[i-1][j+1];
        }
        matrix[i][n-1] = n - i;
    }
    

    Step 5: Format Output

    Using <span><iomanip></span> with <span>setw(3)</span> makes each number occupy 3 character widths, ensuring a neat and beautiful output.

    cout << setw(3) << matrix[i][j];
    

    Each row outputs a newline after completion.

    Summary of Development Approach:

    1. Understand the Pattern: Observe the example and find the relationship between rows (shift left + fill number)
    2. Implement Step by Step: First do the first row, then use a loop for the subsequent rows
    3. Utilize Arrays: Use a two-dimensional array to store intermediate results
    4. Pay Attention to Boundaries: Handle the last column separately
    5. Format Output: Make the results look neat

    📄 Program Documentation (For Beginners)

    1. Program Name

    Angled II Matrix Generation Program

    2. Function Overview

    This program generates a special n×n matrix based on the user’s input of a positive integer <span>n</span> (n ≤ 10). The characteristics of the matrix are: each row slides left by one position compared to the previous row, and a decreasing number is added at the end.

    3. Input Description

    • Input an integer <span>n</span>, representing the size of the matrix (n×n)
    • Range: 1 ≤ n ≤ 10

    4. Output Description

    • Output an n×n integer matrix
    • Each number occupies 3 character widths, right-aligned
    • Each row ends with a newline

    5. Detailed Explanation of Matrix Generation Rules

    Taking <span>n=4</span> as an example:

    Row Number Content Generation Method Description
    0 1 2 3 4 First row: arranged in order from 1 to n
    1 2 3 4 3 Previous row removes the first number (1), shifts left, and fills the end with <span>n-1=3</span>
    2 3 4 3 2 Previous row removes the first number (2), shifts left, and fills the end with <span>n-2=2</span>
    3 4 3 2 1 Previous row removes the first number (3), shifts left, and fills the end with <span>n-3=1</span>

    6. Code Structure Explanation

    Code Segment Function
    <span>#include <bits/stdc++.h></span> Includes all commonly used header files (like iostream, iomanip, etc.)
    <span>using namespace std;</span> Uses the standard namespace to avoid writing <span>std::</span>
    <span>int n; cin >> n;</span> Reads the user input n
    <span>int matrix[10][10];</span> Defines a 10×10 integer array to store the matrix
    First for loop Initializes the first row: 1, 2, …, n
    Nested for loop Fills from the first row to the n-1 row, implementing the “shift left + fill number” logic
    Final nested loop Formats and outputs the entire matrix

    7. Key Variable Explanation

    Variable Type Function
    <span>n</span> <span>int</span> User input for matrix size
    <span>matrix</span> <span>int[10][10]</span> Two-dimensional array storing the entire matrix
    <span>i</span> <span>int</span> Outer loop variable representing the current row number (from 0 to n-1)
    <span>j</span> <span>int</span> Inner loop variable representing the current column number (from 0 to n-1)

    8. Formatting Output Explanation

    • <span>setw(3)</span>: From <span><iomanip></span>, sets the output width to 3 characters
    • For example: the number <span>1</span> displays as <span>" 1"</span>, <span>12</span> displays as <span>" 12"</span>, ensuring alignment
    • This makes the matrix look neat and beautiful

    9. Example Run

    Input:

    Dark version
    
    5
    

    Output:

    Dark version
    
      1  2  3  4  5
      2  3  4  5  4
      3  4  5  4  3
      4  5  4  3  2
      5  4  3  2  1
    

    10. Learning Suggestions

    • First manually simulate the case of <span>n=3</span> or <span>n=4</span>, to understand the pattern
    • Draw the changes of <span>matrix[i][j]</span> on paper
    • Try printing intermediate results (like outputting each row after generation) to help with debugging
    • Understand how “the j+1 position of the previous row” corresponds to “the j position of the current row”

    11. Frequently Asked Questions

    Why is the array defined as [10][10]?

    Because the problem limits n ≤ 10, so a maximum of 10×10 is needed, and a fixed size is sufficient.

    <span>setw(3)</span> what is it?

    It sets the output width to 3, aligning the numbers neatly.

    Why <span>matrix[i][n-1] = n - i</span>?

    Because the number to fill at the end of the i-th row is decreasing from n, the 0-th row theoretically ends with n, but the filling starts from the first row, so it is <span>n - i</span>.

    I hope this detailed commentary and explanation can help you fully understand this program! Keep going, programming starts with understanding each small example 💪!

    Reference Code

    #include <bits/stdc++.h>
    using namespace std;
    /*1193 - Angled II Problem Description: Input a positive integer n, output an n×n matrix. The pattern of the matrix is: starting from the first row, each row slides left by one position compared to the previous row, and fills the last position with a decreasing number (starting from n down to 1). For example: when n=5, the output is as follows:
      1  2  3  4  5
      2  3  4  5  4
      3  4  5  4  3
      4  5  4  3  2
      5  4  3  2  1*/
    int main() {
        int n;
        cin >> n;  // Read user input integer n, indicating the size of the n×n matrix
        // Create a 10×10 two-dimensional array to store the matrix
        // Here using fixed size 10×10 simplifies the problem, guaranteed n ≤ 10
        int matrix[10][10];
        // Fill the first row: the first row is a sequence from 1 to n
        for (int j = 0; j < n; j++) {
            matrix[0][j] = j + 1;  // matrix[0][0]=1, matrix[0][1]=2, ..., matrix[0][n-1]=n
        }
        // Fill the remaining rows (from the 1st row to the n-1 row)
        for (int i = 1; i < n; i++) {
            // Copy the values from the second element of the previous row to the first n-1 positions of the current row
            // That is matrix[i][j] = matrix[i-1][j+1], implementing the "shift left" effect
            for (int j = 0; j < n - 1; j++) {
                matrix[i][j] = matrix[i-1][j+1];  // The first n-1 numbers of the current row come from the last n-1 numbers of the previous row
            }
            // Fill the last position (the n-th position) of the current row with a decreasing number
            // This number is n - i, decreasing as the row number i increases
            matrix[i][n-1] = n - i;  // The last of the first row is n-1, the second row is n-2, ..., the n-1 row is 1
        }
        // Output the entire n×n matrix
        for (int i = 0; i < n; i++) {
            // Traverse each row
            for (int j = 0; j < n; j++) {    // Traverse each element of the current row
                cout << setw(3) << matrix[i][j];  // Each number occupies 3 character widths, right-aligned output
            }
            cout << endl;  // After outputting a row, newline
        }
        return 0;  // Program ends normally
    }
    

    Leave a Comment