Article directory
- Preface
- 1. Common numerical rules
- 2. Nested Loop Graph Questions
- 3. Program error
-
- 3.1 Open your project or source file.
- 3.2 Select "Debug" > "Start Debugging" in the menu bar or use the shortcut key F8 to start the debugging session.
- 3.3 In a debugging session, you can use the following debugging functions:
-
- 3.3.1 Set breakpoints: Set breakpoints on lines of code where you think an error may occur. A breakpoint will cause the program to pause when execution reaches that line.
- 3.3.2 Single-step execution: Use the F7 key to execute the program line by line. This will allow you to see the execution flow of your program step by step.
- 3.3.3 View variables: In a debugging session, you can view the current value of a variable. You can view a variable's value by hovering your mouse over it or using the Watch window.
- 3.3.4 Watch Expressions: You can add expressions in the Watch window to view their values in a debugging session.
- 4. Practice
-
- Question 1: 1225 - Print hollow isosceles triangle
- Question 2: 1069 - Character Graphics 5-Star Trapezoid
- Question 3: 1073 - Hourglass
- Question 4: 1230 - Bow Tie
- Question 5: 1247 - Print the complete bow of n lines
- Question 6: 1246 - Please output a 9*9 multiplication table with n rows
- Question 7: 1008 - Character Graphics 9 - Numerical Triangle
- Question 8: 1006 - Printing an asterisk triangle
- Question 9: 1239 - Challenge Question 2 - Magnified X
- Question 10: 1353 - Axisymmetric Triangle
- 5. Summary
Preface
As a supplement to loop structure three, this chapter continues to explain issues related to nested loop graphics.
Learning route: C++ learning route from entry to NOI
Study Outline: C++ National Youth Olympiad in Informatics (NOI) Entry Level - Outline
Previous stop: Introduction to C++ Program Structure: Loop Structure Three
Next stop:
1. Common numerical rules
Assuming that i is a continuous natural number from 1 to 5, learn to obtain common sequence by looping variable i.
1.1 Output the sequence 1 2 3 4 5.
The output is: 1 2 3 4 5.
for(int i=1;i<=5;i++){
cout<<i<<" ";
}
1.2 Output the sequence 2 4 6 8 10.
The output is: 2 4 6 8 10.
for(int i=1;i<=5;i++){
cout<<i*2<<" ";
}
1.3 Output the sequence 1 3 5 7 9.
The output result is: 1 3 5 7 9.
for(int i=1;i<=5;i++){
cout<<i*2-1<<" ";
}
1.4 Output the sequence 5 4 3 2 1.
The output is: 5 4 3 2 1.
for(int i=1;i<=5;i++){
cout<<5-i+1<<" ";
}
1.5 Output the sequence 9 7 5 3 1.
The output is: 9 7 5 3 1.
for(int i=1;i<=5;i++){
cout<<(5-i+1)*2-1<<" ";
}
Summary: Suppose i is a continuous natural number from 1 to 5
(1) Continuous natural numbers: cout<<i<<" ";
(2) Continuous even numbers: cout<<i * 2<<" ";
(3) Continuous odd numbers: cout<<i * 2-1<< " ";
(4) Natural numbers in reverse order: cout<<5-i+1<<" ";
(5) Odd numbers in reverse order: cout<<(5-i+1) * 2-1<<" ";
2. Nested Loop Graph Questions
Question 1: 1068 - Character graphics 4-asterisk triangle
Enter an integer to print the character graphic.
1. Analyze the problem
- Known: an integer
- Unknown: Graphics
- Relationship: Laws of Sequences
2. Define variables
Define variables as needed based on the known and unknown of the analysis.
//二、数据定义
int n;
3. Enter data
Read the integer n from the keyboard.
//三、数据输入
cin>>n;
4.Data calculation
According to the existing graphic analysis rules.
First, observe the graph. Although only the * graph is output, the graph actually contains space elements, so you need to use a loop to control spaces and a loop to control asterisks.
Spaces:
If the input integer n=3, then there will be two lines with spaces, and the spaces will decrease in sequence. The first line has 2 spaces, which is n-1, and the second line has 1 space, which is n-2.
It is known that i is increasing, so the number of spaces is exactly ni.
for(int j=1;j<=n-i;j++){
//五、输出结果
cout<<" ";
}
Asterisk:
If the input integer n=3, the number of asterisks appearing is 1, 3, 5, which is an odd number sequence. In the rule summarized above, the odd number sequence is i * 2-1, so the number of asterisks is i * 2-1.
for(int k=1;k<=2*i-1;k++){
//五、输出结果
cout<<"*";
}
Then we can loop the contents of each line n times, paying attention to line breaks.
//四、数据计算
for(int i=1;i<=n;i++){
cout<<endl;
}
5. Output results
#include<iostream>
using namespace std;
int main(){
//一、分析问题
//已知:一个整数
//未知:图形
//关系:数列规律
//二、数据定义
int n;
//三、数据输入
cin>>n;
//四、数据计算
for(int i=1;i<=n;i++){
for(int j=1;j<=n-i;j++){
//五、输出结果
cout<<" ";
}
for(int k=1;k<=2*i-1;k++){
//五、输出结果
cout<<"*";
}
cout<<endl;
}
return 0;
}
Question 2: 1070 - Character graphics 6-asterisk inverted triangle
Enter an integer to print the character graphic.
1. Analyze the problem
- Known: an integer
- Unknown: Graphics
- Relationship: Laws of Sequences
2. Define variables
Define variables as needed based on the known and unknown of the analysis.
//二、数据定义
int n;
3. Enter data
Read the integer n from the keyboard.
//三、数据输入
cin>>n;
4.Data calculation
According to the existing graphic analysis rules.
First, observe the graph. Although only the * graph is output, the graph actually contains space elements, so you need to use a loop to control spaces and a loop to control asterisks.
Spaces:
If the input integer n=3, then there will be two lines with spaces, and the spaces will increase in sequence, 0 in the first line, 1 in the second line, and 2 in the third line. The spacing of each line is controlled by the number of lines.
It is known that i is increasing, so the number of spaces is just j < i.
for(int j=1;j<i;j++){
//五、输出结果
cout<<" ";
}
Asterisk:
If the input integer n=3, the number of asterisks appearing is 5, 3, 1, which is an odd sequence in reverse order. In the rule summarized above, the reverse odd number sequence is (3-i+1) * 2-1, so the number of asterisks is (n-i+1) * 2-1.
for(int k=(n-i+1)*2-1;k>0;k--){
//五、输出结果
cout<<"*";
}
Then we can loop the contents of each line n times, paying attention to line breaks.
//四、数据计算
for(int i=1;i<=n;i++){
cout<<endl;
}
5. Output results
#include<iostream>
using namespace std;
int main(){
//一、分析问题
//已知:一个整数
//未知:图形
//关系:数列规律
//二、数据定义
int n;
//三、数据输入
cin>>n;
//四、数据计算
for(int i=1;i<=n;i++){
for(int j=1;j<i;j++){
//五、输出结果
cout<<" ";
}
for(int k=(n-i+1)*2-1;k>0;k--){
//五、输出结果
cout<<"*";
}
cout<<endl;
}
return 0;
}
Question Three: 1071 - Character Graphics 7 - Star Diamond
Input an integer n, please print out n ∗ 2+1 lines of character graphics.
1. Analyze the problem
- Known: an integer
- Unknown: Graphics
- Relationship: Laws of Sequences
2. Define variables
Define variables as needed based on the known and unknown of the analysis.
//二、数据定义
int n;
3. Enter data
Read the integer n from the keyboard.
//三、数据输入
cin>>n;
4.Data calculation
This question belongs to a top-bottom symmetrical figure. For this type of question, the divide and conquer method should be adopted, that is, the figure is divided into several parts according to rules and printed.
Upper part:
Divide the rhombus into two parts, and the upper part is an equilateral triangle. According to the previous question "1068 - Character Graphic 4 - Asterisk Triangle" you can easily get the following code.
//四、数据计算
for(int i=1;i<=n+1;i++){
for(int j=n-i;j>=0;j--){
//五、输出结果
cout<<" ";
}
for(int k=1;k<=2*i-1;k++){
//五、输出结果
cout<<"*";
}
cout<<endl;
}
Lower part:
Divide the rhombus into two parts, and the lower part is an inverted triangle. According to the previous question "1070 - Character Graphic 6 - Asterisk Inverted Triangle" you can easily get the following code.
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
//五、输出结果
cout<<" ";
}
for(int k=(n-i+1)*2-1;k>0;k--){
//五、输出结果
cout<<"*";
}
cout<<endl;
}
5. Output results
#include<iostream>
using namespace std;
int main(){
//一、分析问题
//已知:一个整数
//未知:图形
//关系:数列规律
//二、数据定义
int n;
//三、数据输入
cin>>n;
//四、数据计算
for(int i=1;i<=n+1;i++){
for(int j=n-i;j>=0;j--){
//五、输出结果
cout<<" ";
}
for(int k=1;k<=2*i-1;k++){
//五、输出结果
cout<<"*";
}
cout<<endl;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
//五、输出结果
cout<<" ";
}
for(int k=(n-i+1)*2-1;k>0;k--){
//五、输出结果
cout<<"*";
}
cout<<endl;
}
return 0;
}
Question Four: 1219 - Magnifying Arrow
Please print the enlarged arrows for n lines (n must be an odd number).
1. Analyze the problem
- Known: an integer
- Unknown: Graphics
- Relationship: Laws of Sequences
2. Define variables
Define variables as needed based on the known and unknown of the analysis.
//二、数据定义
int n;
3. Enter data
Read the integer n from the keyboard.
//三、数据输入
cin>>n;
4.Data calculation
This question belongs to a top-bottom symmetrical figure. For this type of question, the divide and conquer method should be adopted, that is, the figure is divided into several parts according to rules and printed.
Upper part:
Spaces:
The spaces in the upper part are increasing in sequence. You can refer to the asterisk triangle writing method.
for(int k=1;k<i;k++){
//五、输出结果
cout<<" ";
}
Asterisks:
The number of asterisks output in each line is determined, that is, n.
for(int j=1;j<=n;j++){
//五、输出结果
cout<<"*";
}
Because it is an odd number of columns, either one more line is output in the upper half, or one more line is output in the lower half.
for(int i=1;i<=n/2+1;i++){
cout<<endl;
}
Lower half:
Spaces:
The spaces in the lower half are decreasing in sequence.
for(int k=1;k<=i-1;k++){
//五、输出结果
cout<<" ";
}
Asterisks:
In the same way, the number of asterisks output in each line of the lower half is also determined, that is, n.
for(int j=1;j<=n;j++){
//五、输出结果
cout<<"*";
}
Loop n/2 times and perform subtraction to facilitate outputting spaces in reverse order.
for(int i=n/2;i>=1;i--){
cout<<endl;
}
5. Output results
#include<iostream>
using namespace std;
int main(){
//一、分析问题
//已知:一个整数
//未知:图形
//关系:数列规律
//二、数据定义
int n;
//三、数据输入
cin>>n;
//四、数据计算
for(int i=1;i<=n/2+1;i++){
for(int k=1;k<i;k++){
//五、输出结果
cout<<" ";
}
for(int j=1;j<=n;j++){
//五、输出结果
cout<<"*";
}
cout<<endl;
}
for(int i=n/2;i>=1;i--){
for(int k=1;k<=i-1;k++){
//五、输出结果
cout<<" ";
}
for(int j=1;j<=n;j++){
//五、输出结果
cout<<"*";
}
cout<<endl;
}
return 0;
}
3. Program error
Debugging with DEV C++ gives you a better understanding of the execution flow of your program and allows you to view the values of variables at runtime. Help you find logic errors in your program.
Here are the general steps for debugging with DEV C++:
3.1 Open your project or source file.
Open or create a new project.
3.2 Select "Debug" > "Start Debugging" in the menu bar or use the shortcut key F8 to start the debugging session.
3.3 In a debugging session, you can use the following debugging functions:
3.3.1 Set breakpoints: Set breakpoints on lines of code where you think an error may occur. A breakpoint will cause the program to pause when execution reaches that line.
Click once where there is a number and see the changes appear different from other rows.
3.3.2 Single-step execution: Use the F7 key to execute the program line by line. This will allow you to see the execution flow of your program step by step.
You can select next step, skip, next statement and other operations in the debug bar.
3.3.3 View variables: In a debugging session, you can view the current value of a variable. You can view a variable's value by hovering your mouse over it or using the Watch window.
After selecting the variable with the mouse, click Add View in the debug bar to see the variable changes in the "Monitor" window on the left.
3.3.4 Watch Expressions: You can add expressions in the Watch window to view their values in a debugging session.
It is the same operation as viewing variables.
4. Practice
Question 1: 1225 - Print hollow isosceles triangle
Read an integer n from the keyboard, representing the side length of an isosceles triangle. Please output an isosceles triangle with side length n!
Question 2: 1069 - Character Graphics 5-Star Trapezoid
Question 3: 1073 - Hourglass
Teacher Zhao is currently programming an operating system. It happens that the mouse is busy and needs an hourglass symbol. Everyone happens to be learning C++. Your task is to help Teacher Zhao compile a program to print an hourglass symbol.
Question 4: 1230 - Bow Tie
Please output the shape of the bow in n rows. n must be an odd number!
Question 5: 1247 - Print the complete bow of n lines
Please read an integer n (an odd number in the range of 1 to 10) from the keyboard, and print out a complete bow with n lines as shown in the figure below!
Question 6: 1246 - Please output a 9*9 multiplication table with n rows
Please read an integer n from the keyboard, which represents n rows, and output a 9*9 multiplication table with n rows.
Question 7: 1008 - Character Graphics 9 - Numerical Triangle
Enter an integer to print the character graphic.
Question 8: 1006 - Printing an asterisk triangle
Prints an asterisk triangle.
Question 9: 1239 - Challenge Question 2 - Magnified X
Please program to draw an enlarged X (capital letter).
Question 10: 1353 - Axisymmetric Triangle
In mathematics, we find that there is a type of graphics that are symmetrical graphics. We call the graphics that are the same on the left and right along the y-axis, and the graphics that are the same on the top and bottom are called symmetry along the x-axis.
5. Summary
The above is the content of using nested loops to print out various graphics. For this type of questions, we must start with the graphics, and divide the graphics or not as needed according to the changing rules of the graphics. Then find the pattern of the sequence.