Today we will first explain a C language classic examples, but also from the zero-start series in the after-school exercises.
Please use the console program to plot the case.
Circulation Classic Examples
Analyze the situation
This topic is required to print 30 lines "*", the number of printing per line is different. Through this information, we should immediately reflect the use of the loop to complete. So let's think about what we've learned in the Loop section.
On the loop, I first think of some examples:
Example 1
Print 30 "*" with one row per "*".
int i;for (i = 0; i < 30; i++){ printf("*\n");}
Example 2
Print a line "*" with a number of 30
int i;for (i = 0; i < 30; i++){ printf("*");}
The code for these two titles is only one \ n, but the results are completely different. Having mastered these two examples, we can naturally complete the following example.
Example 3
Prints a matrix of 30 rows and 30 columns consisting of "*".
int main(){ int i, j; for (i = 0; i < 30; i++) { for (j = 0; j < 30; j++) { printf("*"); } printf("*\n"); } return 0;}
The results of the implementation are as follows:
30*30 Matrix
We will change the requirements of example 3 and ask for the following
Example 4
Print 30 Lines "*". The first line prints 1 "*", the second line prints 3 "*", the third line prints 5 "*", ..., the 30th line prints 59 "*".
In the previous program, we used the variable I to control the loop of the row, and the variable J controls the loop of the column. The range of 0~29,j I is 0~29. So what is the relationship between I and J in example 4?
First line: i = 0; J Cycle 1 times
Second line: i = 1; J Cycle 3 times
Line three: i = 2; J Cycle 5 times
...
Line 30th: i = 29; J Cycle 59 times
So we get a relationship like this:
Line N: i = n-1; J Cycle 2i + 1 times
So how to implement loop 2i + 1 times, is to let J from 0 to 2i + 1.
According to this idea, you can get the following code:
#include <stdio.h>#define LINE 30int main(){ int i, j; for (i = 0; i < LINE; i++) { for (j = 0; j < 2 * i + 1; j++) { printf("*"); } printf("\n"); } return 0;}
The results of the implementation are as follows:
Example 4
Here's a question, in fact, there are two common forms of a For loop:
for (i = 0; i < n; i++)
And
for (i = 1; i <= n; i++)
This is done in the same number of times and can be substituted for each other. Most c programmers prefer the first way, since the array's subscript access starts at 0, which is easier to write. Now you can choose the way you like.
Okay, back to the example. Now our code is close to the target output, and what is missing is the need to enter a different number of spaces before each line "*". We analyze the number of I and spaces and can get the following relationship:
Output line-i spaces per line
So, we get the final implementation program.
Answer
#include <stdio.h>#define LINE 30int main(){ int i, j; for (i = 0; i < LINE; i++) { for (j = 0; j < (LINE - i); j++) { printf(" "); } for (j = 0; j < 2 * i + 1; j++) { printf("*"); } printf("\n"); } return 0;}
Run this code, and you'll see that the print results are the front one.
After-school practice
Write your own code and print out the diagram below.
for more questions about diamond types, click here
C Language Code Training (i)