Welcome everyone to punch in every day ~
Training camp Rules
- Every day an exercise, please do your own coding
- The next day's article will tell you one or several classic solutions
- Complete the practice of the students, welcome to put the code in the message
- If there is a problem, please leave a message, I will find the opportunity to focus on the answer
Hopefully this hands-on approach will help you master C programming as quickly as possible.
1. Examples
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.
Loop Classic Example 2. Analysis
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:
2.1 Example 1
Print 30 "*" with one row per "*".
i;for (i = 0; i < 30; i++){ printf("*\n");}
2.2 Example 2
Print a line "*" with a number of 30
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.
2.3 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
2.4 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?
Find the Rules |
|
|
First line |
i = 0; |
J Cycle 1 times |
Second line |
i = 1; |
J Cycle 3 times |
Third line |
i = 2; |
J Cycle 5 times |
... |
... |
... |
Line 30th |
i = 29; |
J Cycle 59 times |
So we get a relationship like this:
regularity |
|
|
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 30< Span class= "hljs-function" >int main () {int I, J; for (i = 0; i < line; i++) {for (j = Span class= "Hljs-number" >0; J < 2 * i + 1; j + +) {printf (printf (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.
3. Answer
#Include<stdio.h> #define line 30int main () {int I, J; Span class= "Hljs-keyword" >for (i = 0; i < line; i++) {for (j = 0; J < (Line-i); J + +) {printf ( ");} for (j = 0; J < 2 * i + 1; J + +) {printf ( "*");} printf (return 0;}
Run this code, and you'll see that the print results are the front one.
4. After-school practice
Write your own code and print out the diagram below.
Diamond Tomorrow Analysis
You are welcome to join the Learning Exchange Group if you encounter any problems or want to acquire learning resources in the learning process.
639368839, we learn c/c++! together.
C Language Code training camp (1)