C Language exercises

Source: Internet
Author: User

1. pointer exercise: swap the values of two pointer Variables

Level 1 pointer: * P, * q;

Int A = 0, B = 12; int * P = & A, * q = & B; printf ("before interchange: \ n % d, % d, % d \ n ", P, * P, & P); printf (" % d, % d, % d \ n ", Q, * q, & Q ); swap (p, q); printf ("after exchange: \ n % d, % d, % d \ n", P, * P, & P ); printf ("% d, % d, % d \ n", Q, * q, & Q );
Void swap (int * X, int * Y) {/* returns the same result using the XOR operation without intermediate variables */* x ^ = * Y; * y ^ = * X; * x ^ = * Y ;}

Output result:

Before exchange:

1606416820,0, 1606416808

1606416816,12, 1606416800

After exchange:

1606416820,12, 1606416808

1606416816,0, 1606416800

Exchange the addresses pointed to by two pointer variables:

Int A = 0, B = 12; int * P = & A, * q = & B; printf ("before interchange: \ n % d, % d, % d \ n ", P, * P, & P); printf (" % d, % d, % d \ n ", Q, * q, & Q ); swap (& P, * q); printf ("after exchange: \ n % d, % d, % d \ n", P, * P, & P ); printf ("% d, % d, % d \ n", Q, * q, & Q); void swap (int * X, int * Y) {/* the same result is obtained using the XOR operation without intermediate variables */* x ^ = * Y; * y ^ = * X; * x ^ = * Y ;}

Output result:

Before exchange:

1606416820,0, 1606416808

1606416816,12, 1606416800

After exchange:

1606416816,12, 1606416808

1606416820,0, 1606416800

Use a second-level pointer to exchange the addresses pointed to by two first-level pointer variables:

Int A = 2; int B = 3; int * P = & A; int * q = & B; int ** pp = & P; // defines the second-level pointer int ** QQ = & Q; // defines the second-level pointer printf ("before exchange: \ n % d, % d, % d \ n", P, * P, & P); printf ("% d, % d, % d \ n", Q, * q, & Q); swap (PP, QQ ); printf ("after exchange: \ n % d, % d, % d \ n", P, * P, & P); printf ("% d, % d, % d \ n ", Q, * q, & Q );

void swap(int **x,int **y){    int * temp;    temp=*x;    *x=*y;    *y=temp;}

Output result:

Before exchange:

1606416812,2, 1606416800

1606416808, 3, 1606416792

After exchange:

1606416808, 3, 1606416800

1606416812,2, 1606416792

2. Print the 9-9 multiplication table

void jiujiu(){    for(int i=1;i<10;i++){        for(int j=1;j<i+1;j++){                       printf("%d*%d=%d\t",j,i,i*j);                    }        printf("\n");    }}

Output result:

1*1 = 1

1*2 = 2
2*2 = 4

1*3 = 3
2*3 = 6 3*3 = 9

1*4 = 4
2*4 = 8 3*4 = 12
4*4 = 16

1*5 = 5
2*5 = 10 3*5 = 15
4*5 = 20 5*5 = 25

1*6 = 6
2*6 = 12 3*6 = 18
4*6 = 24 5*6 = 30
6*6 = 36

1*7 = 7
2*7 = 14 3*7 = 21
4*7 = 28 5*7 = 35
6*7 = 42 7*7 = 49

1*8 = 8
2*8 = 16 3*8 = 24
4*8 = 32 5*8 = 40
6*8 = 48 7*8 = 56
8*8 = 64

1*9 = 9
2*9 = 18 3*9 = 27
4*9 = 36 5*9 = 45
6*9 = 54 7*9 = 63
8*9 = 72 9*9 = 81

3. Input two integers at will. Use the two methods to output the largest one, and use the conditional statements and arithmetic operations to obtain the maximum value;

// Method 1: Determine the statement void bijiao1 () {int A, B; printf ("Please input two integers A, B :"); scanf ("% d", & A, & B); int max = A> B? A: B; printf ("maximum value: % d \ n", max);} // Method 2: arithmetic operation void bijiao2 () {int A, B; printf ("enter two integers A, B:"); scanf ("% d", & A, & B); int max = (A + B) + ABS (a-B)/2; // if you want to obtain the minimum value, write: int min = (a + B)-ABS (a-B )) /2; printf ("maximum value: % d \ n", max );}

Output result:

Please input two integersA, B:1 2

The maximum value is:2

Please input two integersA, B:3 4

The maximum value is:4

4. Print a heart shape:

    printf("  * *   * *\n");    printf(" *    *    *\n");    printf(" *         * \n");    printf("  *       *\n");    printf("   *     *\n");    printf("    *   *\n");    printf("      *\n");

5,Print out Yang Hui triangle

# Include <stdio. h> int main (INT argc, const char * argv []) {int A [10] [10]; // assign the sum of all the first and last bits to 1 for (INT I = 0; I <10; I ++) {A [I] [0] = 1; A [I] [I] = 1 ;} // A [I] [J] is the top two for (INT I = 2; I <10; I ++) {for (Int J = 1; j <I; j ++) {A [I] [J] = A [I-1] [J-1] + A [I-1] [J];} // print out the Yang Hui triangle for (INT I = 0; I <10; I ++) {// output the leading space for (int s = I; S <10; s ++) {printf ("") ;}for (Int J = 0; j <= I; j ++) {printf ("% d ", A [I] [J]);} printf ("\ n");} return 0 ;}

Output result:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

1 7 21 35 35 21 7 1

1 8 28 56 70 56 28 8 1

1 9 36 84 126 126 84 36 9 1


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.