C Language exercises (2)

Source: Internet
Author: User

1. Use a pointer to complete the following:

Define an array of three integers, write three functions, input (), deal (), print (): input, processing, print: input function, three numbers must be entered. The deal function needs to be sorted in ascending order, and the print function needs to print the results.

My idea: first define an array of integers with a length of 3; then input them separately, then process the sorting, sort them by sorting method, and finally output the result;

Implementation Code:

The main function defines four lines of code:

    int a[3];    input(a);    deal(a);    print(a);

Implementation Methods:

Void input (int * X) {for (INT I = 0; I <3; I ++) {printf ("Enter the % D digit :", I + 1); scanf ("% d", & (* X); * x ++;} void deal (int * X) {for (INT I = 0; I <2; I ++) {for (Int J = I + 1; j <3; j ++) {If (* (x + I)> * (x + J) {* (x + I) ^ = * (x + J); * (x + J) ^ = * (x + I); * (x + I) ^ = * (x + J) ;}}} void print (int * X) {printf ("minimum to large:"); For (INT I = 0; I <3; I ++) {printf ("% d ,", * x ++ );}}

OK,

2,

There are n people in a circle, ordered by number. When the first person reports the number (from 1 to 3), the person who reports the number 3 leaves the circle and asks the last person who left the number.

The code is annotated as follows:

Int Nmax = 50; // a 50-length array, int I, K, M, N, num [Nmax], * P; printf ("Please input the total of numbers: "); scanf (" % d ", & N); // enter an integer p = num; // P points to the first address of the array for (I = 0; I <n; I ++) * (p + I) = I + 1; // assign values to each element of the array, from 1 to n; I = 0; // check whether the number of record loops reaches the end, and assign a value of 0 k = 0 when developing; // K indicates that the number of records is three times m = 0; // There are several records, if m still has the last number, that is, when m> = n-1, the output will jump out without repeating. While (M <n-1) {If (* (p + I )! = 0) K ++; If (k = 3) {* (p + I) = 0; k = 0; m ++;} I ++; if (I = N) I = 0;} while (* P = 0) P ++; // determines which number is not 0, if it is not 0, it is the last one left. printf ("% d is leftn", * P); // the last number is output.

3. The Code is as follows: output the result.

    int  a=100;    fun(&a);void fun(int *n){    while( (*n)--);    printf("%d\n",++(*n));}

Result: 0

Explanation: first, fun (& A) transmits the address. In the function, * n is the value of A, which is 100. At the end of the while loop, a semicolon is equivalent to an empty statement, this is easy for beginners to ignore. It is incorrect to ignore it. What you want to output is from 100 to 0. How can you output a 0! The reason is a semicolon. Let's look at the conditions in it. * n is 100 of A's value, which is equivalent to a --. In this way, a is 99, and so on. When a is 0, that is false. As we all know, 0 in a language is false, so when * n is 0, (* n) -- it jumps out when it turns to-1, print ++ (-1) as shown in the following figure. If you say yes, It is 0;

4. The Code is as follows: output the result.

int a[] = {0, 2,4,6,8};int *p[5]={a, a+1, a+2, a+3, a+4};int ** pp = p;

A = * P = * PP; printf ("% d \ n", * PP ++ ); // It is the first address of a, printf ("% d \ n", * PP-A); // 1-0 = 1 printf ("% d \ n ", ** ++ pp); // printf ("% d \ n", PP-P );

Output result:

4176

1

4

2

Explanation: First, it indicates that a is five in a number group, and * P [5] An array pointer also has five elements: the first address of A, the second address of A, the third person, the fourth address, and the fifth address. Then, the ** P second-level pointer points to the first address of P, that is, the first address of A. In this case, we will know what to output!

The first output statement: the first output * PP is the first address of P, that is, the first fbfh of A. Therefore, the output address is 4176;
If you do not believe it, you can print this sentence again:

Printf ("% d, % d \ n", A, & A [0], * P, * PP );

The output result of this sentence is:Limit 6, limit 6, limit 6

The second output statement: after the first output statement is executed, * PP ++ points to the next element, which is a + 1 in * P. When a + 1-A = 1, so I output 1 in this way;

The third output statement: ** ++ pp looks ugly. Let's take a look and write it as this * (* ++ pp,(* ++ Pp) is equivalent to a + 2, because after the first output statement is executed, * PP is a + 1, now ++ is changed to a + 2, and then * (a + 2) is not the value 4 of a [2!

Fourth output statement: after the third statement is executed, * PP points to * P [2] and the value is a + 2. p is the first address, it reduces PP-P = a + 2-A = 2; therefore, output 2;

5. output in the form:

*******

*****

***

*

***

*****

*******

The Code is as follows:

Int M, N; For (int K = 1; k <= 7; k ++) {n = 7-abs (K-4 ); // The space M = ABS (K-4) * 2 + 1 Output in front of a row; // how many stars are there on a row for (INT I = 0; I <N; I ++) {printf ("% s", "") ;}for (Int J = 0; j <m; j ++) {printf ("% s ", "*");} printf ("\ n ");}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.