Learning C++ Programming from Scratch, Day 425: 1193 – Angles II; Question Bank Answers; Third Method

1193 – Angles II

Learning C++ Programming from Scratch, Day 425: 1193 - Angles II; Question Bank Answers; Third Method

Development Approach Description

The purpose of this program is to output a pattern related to a two-dimensional array in a specific format. Below is a detailed development approach:

  1. Include Header Files and Namespace:

  • <span>#include <bits/stdc++.h></span>: This is a “universal header file” that includes almost all standard library header files, commonly used in competitive programming, making it convenient for us to use various standard library functions and types. For beginners, you can understand it as it prepares many commonly used functionalities for us.

  • <span>using namespace std;</span>: This line of code allows us to use identifiers from the standard library directly without needing to write the<span>std::</span> prefix each time.

  • Define Two-Dimensional Array and Main Function:

    • <span>int a[20][20];</span>: Defines a 20×20 two-dimensional integer array. Although this array is not used in the current code, it may be reserved for future expansion.

    • <span>int main()</span>: This is the entry point of the program, and all C++ programs start execution from here.

  • Input and Initialization:

    • <span>int n;</span>: Defines an integer variable<span>n</span> to store a value input by the user, which will determine the scale of the subsequent output.

    • <span>cin >> n;</span>: Reads an integer from standard input and stores it in<span>n</span>.

  • Outer Loop:

    • <span>for(int i = 1; i <= n; i++)</span>: This loop controls the number of output rows, from the 1st row to the<span>n</span>th row.

  • Inner Loop (First Part):

    • <span>int x = i;</span>: Defines the variable<span>x</span> and initializes it to the current row number<span>i</span>.

    • <span>for(int j = 1; j <= n; j++)</span>: This loop controls the number of elements output in each row, from the 1st to the<span>n</span>th.

    • <span>cout << setw(3) << x;</span>: Uses<span>setw(3)</span> to set the output width to 3 characters, then outputs the value of<span>x</span>.

    • <span>x++;</span>: After each output, increments the value of<span>x</span> by 1.

  • Reset Intermediate Variable and Inner Loop (Second Part):

    • <span>x = n;</span>: Resets<span>x</span> to<span>n</span>.

    • <span>for(int j = 1; j <= i - 1; j++)</span>: This loop is used to output the middle part of elements in each row.

    • <span>cout << setw(3) << x;</span>: Similarly sets the output width to 3 characters and outputs the value of<span>x</span>.

    • <span>x--;</span>: After each output, decrements the value of<span>x</span> by 1.

  • New Line:

    • <span>cout << endl;</span>: Outputs a newline character after each row output, so that the next row starts from a new line.

  • End of Program:

    • <span>return 0;</span>: Indicates that the program ends normally, returning a value of 0 to the operating system.

    Reference Code

    #include <bits/stdc++.h> // Includes almost all standard library header files
    using namespace std; // Uses the standard namespace
    int a[20][20]; // Defines a 20x20 two-dimensional integer array, currently unused
    int main() {    int n; // Defines an integer variable n to store the user input value    cin >> n; // Reads an integer from standard input and stores it in n
        for (int i = 1; i <= n; i++) { // Outer loop controls the number of output rows, from the 1st to the nth row        int x = i; // Defines variable x and initializes it to the current row number i        for (int j = 1; j <= n; j++) { // Inner loop controls the number of elements output in each row, from the 1st to the nth            cout << setw(3) << x; // Sets output width to 3 characters, outputs x's value            x++; // Increments x's value by 1        }
            x = n; // Resets x to n        for (int j = 1; j <= i - 1; j++) { // Inner loop for outputting the middle part of elements in each row            cout << setw(3) << x; // Sets output width to 3 characters, outputs x's value            x--; // Decrements x's value by 1        }
            cout << endl; // Outputs a newline character after each row output    }
        return 0; // Program ends normally, returns 0 to the operating system
    }

    Program Documentation

    1. Program Overview

    The main function of this program is to output a pattern based on an integer<span>n</span> input by the user, in a specific format. Each line of the pattern consists of two parts: the first part outputs<span>n</span> integers starting from the current row number, and the second part outputs<span>n</span> integers starting from<span>n</span> and decreasing for<span>i - 1</span> integers (where<span>i</span> is the current row number), with each output element occupying 3 character widths.

    2. Code Structure

    1. Header Files and Namespace:

    • <span>#include <bits/stdc++.h></span>: Introduces almost all standard library header files, making it convenient to use various standard library functionalities.

    • <span>using namespace std;</span>: Uses the standard namespace to simplify code writing.

  • Array Definition:

    • <span>int a[20][20];</span>: Defines a 20×20 two-dimensional integer array, which is currently not used in the code but can be utilized for future expansion.

  • Main Function<span>main</span>:

    • At the start of each outer loop, defines the variable<span>x</span> and initializes it to the current row number<span>i</span>.

    • The first inner<span>for</span> loop controls the number of elements output in each row, from 1 to<span>n</span>, outputting the value of<span>x</span> and incrementing<span>x</span> by 1.

    • Resets<span>x</span> to<span>n</span>.

    • The second inner<span>for</span> loop is used to output the middle part of elements in each row, from 1 to<span>i - 1</span>, outputting the value of<span>x</span> and decrementing<span>x</span> by 1.

    • Uses<span>cout << endl;</span> to output a newline after each row output.

    • Defines an integer variable<span>n</span> to store the user input value.

    • Uses<span>cin >> n;</span> to read an integer from standard input into<span>n</span>.

    • The outer<span>for</span> loop controls the number of output rows, from 1 to<span>n</span>.

    • Finally returns 0, indicating the program ends normally.

    3. Example Run

    Assuming the user inputs<span>5</span>, the program will output the following pattern:

    1  2  3  4  5
      5  6  7  8  4
      9 10 11 12  3
     13 14 15 16  2
     17 18 19 20  1
    
    
    
    
    

    The first part of each line is an increasing integer sequence starting from the current row number, and the second part is a decreasing integer sequence starting from<span>n</span>, with each integer occupying 3 character widths.

    4. Notes

    Leave a Comment