Notes on Personal Understanding of C Language Pointer Author: Chuwen Time: 2020-12-15 Classification: C/C++ comment #Preamble Just last year (October 2019), in order to learn Android development, I went to learn Java (although I didn't learn much). This strongly typed language should be the * * second strongly typed language * * that I really learned, the first is C language, and I learned basic grammar knowledge when I was a freshman. But in the process of learning * * pointer * *, I learned nothing, I didn't know why I needed to do it. Up to now, I can write some Android software with small functions, such as regular layout, interactive use of basic controls, and use of some frameworks (such as MPAndroidChart and Gson). I also learned a lot of new knowledge and had a certain understanding of the * * pointer * * I learned before. Of course, there are other gains. I may write specifically in the * * 2020 summary * *. #C language pointer >Recently, I learned some algorithms and used C language pointers, but I generally understand what they mean, but sometimes I forget them. So I write again to consolidate them ```c #include "stdio.h" int main(){ int var1;// Define production routine variables int *point;// Defining Pointer Variables int *point2;// Defining Pointer Variables var1 = 20170101;// Variable assignment Printf ("Print variable value var=% d n", var1); //Add a&before the variable name to indicate the address of the pointer Printf ("Print variable storage address var=% p n n",&var1); /***********************************************************/ //Pointer assignment: assign the pointer address of var1 to the point pointer variable point = &var1; Printf ("point pointer address% p n", point); //Pointer variable value Printf ("The value of the point pointer variable is% d n n", * point); //Pointer variable assignment to pointer variable point2 = point; Printf ("point2 pointer address% p n", point2); //Modify the pointer value because the pointer address of point and point2 points to the pointer address of variable var1 //Then when the pointer variables of point and point2 modify their values, all "pointer variables" pointing to the pointer address //Its value will be changed to 100 *point2 = 100; Printf ("value of var1% d n", var1); Printf ("value of point% d n", * point); Printf ("point2 value% d n n", * point2); var1 = 200; Printf ("value of var1% d n", var1); Printf ("value of point% d n", * point); Printf ("point2 value% d n n", * point2); return 0; } ``` #Summary -Get the pointer address of the general variable, and add '&' before the variable name to get -Get the value of the pointer variable by adding '*' before the pointer variable name -When the value of the pointer address changes, all values pointing to the * * pointer address * * will change
The difference between C/C++language # include "" and # include<>double quotation marks and angle bracket reference header file. h Author: Chuwen Time: 2020-12-14 Classification: C/C++ comment #Antecedent Because when I was writing the header file, I used '# include<xxx. h>' to import the header file. I was puzzled by an error. Then I thought of '# include "xxx. h" to import the header file. It was really successful ! [An error occurred when using # include<xxx. h>to import the header file, and compilation failed] [1] Then I thought that the use of the two should be different from each other. Then I searched online and if so, I simply recorded it #The simple difference is 1. '# include<xxx. h>' Introduces the header file of * * system built-in * *, and the compiler will look for it in the system directory 2. '# include "xxx. h"' introduces the header file * * written by the user * *. The compiler will search in the user directory, and then in the C installation directory. * * If no * * is found, it will search in the system directory #Summary It must be correct to use double quotation marks' # include "xxx. h" 'to import header files, whether they are system supplied header files or user written header files (' xxx. h ') #Another piece of water [1]: https://cdn.nowtime.cc/2020/12/14/722291638.jpg
[C language] Find Legendre polynomials of order n by recursion Author: Chuwen Time: 2019-06-02 Classification: C/C++ comment #Title ># # Solve Legendre polynomials of order n by recursion. The recursion formula is: ``` { 1 (n=0) Pn(x) = { x (n=1) { ((2n-1)*x-Pn-1(x)-(n-1)*On-2(x))/n (n>=1) ``` ! [Solve Legendre polynomials of order n by recursion, and the recursion formula is] [1] #Solutions 1. It is required to input the values of n and x 2. Output the value of Pn (x) #C language code implementation ```c #include "stdio.h" int main(){ double legendre(double n, double x);// Claiming user-defined function legendre double n,x; Printf ("Please enter n, x (separated by spaces):"); scanf("%lf %lf", &n, &x); Printf ("result:% lf", legendre (n, x)); return 0; } double legendre(double n, double x){ double s; if(n == 0){ s = 1; }else if(n == 1){ s = x; }else if(n > 1){ //Use recursive legendre (n-1.0, x), legendre (n-2.0, x) s = ((2.0*n-1.0)*x-legendre(n-1.0, x)-(n-1.0)*legendre(n-2.0, x))/n; } return s; } ``` #Test ># # Enter the values of n and x as' 2 'and' 2` ># # Predicted return results: ` 1.5` ``` Please enter n, x (separated by spaces): 2 2 Result: 1.500000 ``` [1]: https://cdn.nowtime.cc/2019/06/02/4033601759.jpg
C Language Output Yang Hui Triangle Author: Chuwen Time: 2019-05-16 Classification: C/C++ comment #Principle Look at the notes yourself. It is better to write a few lines on the paper (such as six lines) and find the rule (each number is equal to the sum of the two numbers above it) ># # For the 'Yanghui Triangle', please check [Yanghui Triangle - Baidu Encyclopedia] [1] #C code: ```c #include <stdio.h> int main() { //Row: indicates a row //Column: indicates the column int row,column; //Set<Row 0 Column 0>value to 1 //Set<Row 1, Column 0, Column 1>to 1 int arr[10][10]={{1}, {1,1}}; for(row=2; row<10; row++){ arr[row][0] = 1;// Set the value of row and column 0 to 0 for(column=1; column<row; column++){ //This value=the value of the previous row<the column - 1>+the value of the previous row<the column> //For example: row=2, column=1, the value of arr [2] [1] is //arr[2-1][1-1]+arr[2-1][1] arr[row][column] = arr[row-1][column-1]+arr[row-1][column]; } arr[row][column] = 1;// Set the value of row and column to 1 } Printf ("Output<10 lines>Yang Hui Triangle: n"); for(row=0; row<10; row++){ for(column=0; column<10; column++){ If (arr [row] [column]!=0) {//Number whose output value is not 0 printf("%4d", arr[row][column]); } } printf("\n"); } } ``` [1]: https://baike.baidu.com/item/%E6%9D%A8%E8%BE%89%E4%B8%89%E8%A7%92/215098
C Language | Circular Structure Programming for Monkeys Eating Peaches - Question 12 Author: Chuwen Time: 2019-04-19 Classification: C/C++ comment #Title >Monkeys eat peaches. The monkey picked several peaches on the first day, and immediately, in addition to being ordinary, he was not satisfied, so he ate one more peach. In the morning of the second day, I ate half of the remaining peaches and one more. >Every morning after that, I ate the leftovers of the previous day. On the tenth morning, when I wanted to eat again, there was only one peach left. >* * How many peaches were picked on the first day** #Solutions 1. Assume that * * Peach * * has a total of 'x', you can list the formula: >Y1: Number of peaches left on the first day >Y2: Number of peaches left on the second day > ... >Y9: Number of peaches left on the 9th day ``` y1 = x/2 - 1 y2 = y1/2 - 1 y3 = y2/2 - 1 y4 = y3/2 - 1 y5 = y4/2 - 1 y6 = y5/2 - 1 y7 = y6/2 - 1 y8 = y7/2 - 1 y9 = y8/2 - 1 ``` 2. According to the * * title * *, there is still a peach on the 9th day, so we can solve the equation to get: ``` Because y9=1 So take y9=1 into y9=y8/2 - 1, and solve the equation y8 = 1+1*2 y8 = 4 Then continue to unravel, you can get how many peaches you picked on the first day ``` --- #C code ```c #include <stdio.h> int main() { int days=1, peach=1, eat; do{ peach = (peach+1)*2; days++; }while(days<10); Printf ("The monkey picked% d peaches on the first day", peach); printf("\n-------------------------------\n"); Printf ("Verification result: n"); days = 1; do{ eat = peach/2+1;// How many peaches did you eat peach -= eat;// Remaining quantity of peaches Printf ("Day% 2d: I ate% 3d peaches and left% 3d peaches n", days, eat, peach); days++; }while(days<=10); } ``` #Operation results ``` Monkey picked 1534 peaches on the first day ------------------------------- Verification results: Day 1: After eating 768 peaches, 766 peaches remained Day 2: 384 peaches were eaten and 382 peaches were left Day 3: 192 peaches left Day 4: 96 peaches left Day 5: I ate 48 peaches and the rest 46 peaches Day 6: 24 peaches left Day 7: 12 peaches left Day 8: I ate 6 peaches and left 4 peaches Day 9: Eating 3 peaches and remaining 1 peach Day 10: After eating 1 peach, 0 peach remained ``` ! [Snipaste_2019-04-19_13-56-06.png][1] [1]: https://cdn.nowtime.cc/2019/04/19/2394092364.png