C語言練習題:陣列(C language exercise: Array)

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.

練習一:整數陣列加總
使用一個大小為10的整數陣列來讀取使用者所輸入的10個整數,並算出此十個整數的總和。

Exercise 1: Sum of an array of integer
Using an integer array of size 10 to store input from user. Now print the sum of these 10 integers.

練習一參考解法:
Exercise 1 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int numbers[10], sum = 0;

    for(int i = 0; i < 10; i++)
    {
        printf("Enter an integer for numbers[%d]:", i);
        scanf("%d", &numbers[i]);
        sum = sum + numbers[i];
    }

    printf("\nSum:%d", sum);

    return 0;
}

練習二:整數陣列中最大的整數
使用亂數函數 rand() 產生 10 整數,並找出最大值。

Exercise 2: Find the largest integer
Using rand() function to generate 10 integers and find the biggest integer.

練習二參考解法:
Exercise 2 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int numbers[10], big = 0;

    printf("The numbers:");
    for(int i = 0; i < 10; i++)
    {
        numbers[i] = rand();
        printf("%d,", numbers[i]);
        if(big < numbers[i])
            big = numbers[i];
    }

    printf("\nThe biggest integer:%d.\n", big);
    return 0;
}

練習三:Greeting message
Get the user's name from keyboard and print it with greeting message.

Exercise 3: 打招呼
請使用者輸入自己的姓名,程式輸出【姓名】,【打招呼訊息】。

練習三參考解法:
Exercise 3 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char name[50];
    printf("Input your name:");
    scanf("%s", name);  // name is a string that don't need'&' operator

    printf("%s, nice to meet you.\n", name);
    return 0;
}

練習四:奇數或偶數
使用亂數函數 rand() 產生 10 整數,分別對奇數的數加總以及偶數的數加總。

Exercise 4: Even or Odd
Using rand() function to generate 10 integers. Now, print the sum of even numbers and the sum of odd numbers. 

練習四參考解法:
Exercise 4 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int numbers[10], evenSum = 0, oddSum = 0;

    printf("The numbers:");
    for(int i = 0; i < 10; i++)
    {
        numbers[i] = rand() % 10;
        printf("%d,", numbers[i]);

        if(numbers[i] % 2)
        {
            oddSum += numbers[i];
        }
        else
        {
            evenSum += numbers[i];
        }
    }

    printf("\nThe sum of even numbers:%d\n", evenSum);
    printf("The sum of odd numbers:%d\n", oddSum);
    return 0;
}

練習五:二維陣列
使用亂數函數 rand()來建立大小為 3 x 3 的二維陣列。並輸出此矩陣的元素以及矩陣所有元素的平均值。

Exercise 5: 2D Array
Using rand() function to write a program for a 2D array of size 3x3. Then display the matrix and calculate the average of the matrix's elements.


練習五參考解法:
Exercise 5 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int matrix[3][3];
    float avg = 0.0f;


    printf("The matrix:\n");
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            matrix[i][j] = rand() % 100;
            avg += matrix[i][j];
            printf("matrix[%d][%d] = %2d, ", i, j, matrix[i][j]);
        }
        printf("\n");
    }

    avg = avg / 9.0;
    printf("Average:%f\n", avg);

    return 0;
}

練習六:矩陣相加
使用亂數函數 rand()來建立兩個大小為 3 x 3 的二維陣列。並輸出此兩矩陣相加後的結果。

Exercise 6:  Matrix Addition
Using rand() function to write a program for two 2D array of size 3x3. And output the addition of the two matrix.

練習六參考解法:
Exercise 6 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>

void prtMatrix(int m[3][3])
{
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            printf("[%d][%d] = %2d, ", i, j, m[i][j]);
        }
        printf("\n");
    }

}

int main()
{
    int a[3][3];
    int b[3][3];
    int c[3][3];

    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            a[i][j] = rand() % 100;
            b[i][j] = rand() % 100;
            c[i][j] = a[i][j] + b[i][j];
        }
    }

    printf("a:\n");
    prtMatrix(a);

    printf("\n");

    printf("b:\n");
    prtMatrix(b);

    printf("\n");

    printf("The addition of matrix a and matrix b:\n");
    prtMatrix(c);

    return 0;
}


練習七:矩陣相減
使用亂數函數 rand()來建立兩個大小為 3 x 3 的二維陣列。並輸出此兩矩陣相減後的結果。

Exercise 7: Matrix Subtraction
Using rand() function to write a program for two 2D array of size 3x3. And output the subtraction of the two matrix.

練習七參考解法:
Exercise 7 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>

void prtMatrix(int m[3][3])
{
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            printf("[%d][%d] = %2d, ", i, j, m[i][j]);
        }
        printf("\n");
    }

}

int main()
{
    int a[3][3];
    int b[3][3];
    int c[3][3];

    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            a[i][j] = rand() % 100;
            b[i][j] = rand() % 100;
            c[i][j] = a[i][j] - b[i][j];
        }
    }

    printf("a:\n");
    prtMatrix(a);

    printf("\n");

    printf("b:\n");
    prtMatrix(b);

    printf("\n");

    printf("The subtraction of matrix a and matrix b:\n");
    prtMatrix(c);

    return 0;
}

練習八:方陣相乘
使用亂數函數 rand()來建立兩個大小為 3 x 3 的方陣 A 與 B。並輸出此兩方陣A乘B後的結果。

Exercise 8: Square Matrices Multiplication
Using rand() function to write a program for two 2D array of size 3x3. And output the subtraction of the two square matrices.

練習八參考解法:
Exercise 8 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void prtMatrix(int m[3][3])
{
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            printf("[%d][%d] = %3d, ", i, j, m[i][j]);
        }
        printf("\n");
    }

}

int main()
{
    int A[3][3];
    int B[3][3];
    int C[3][3];

    srand((unsigned) time(0));

    // Matrices initialization
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            A[i][j] = rand() % 10;
            B[i][j] = rand() % 10;
            C[i][j] = 0;
        }
    }

    // C = A x B
    int sum = 0;
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            sum = 0;
            for(int k = 0; k < 3; k++)
            {
                sum = sum + A[i][k] * B[k][j];
            }
            C[i][j] = sum;
        }
    }

    printf("A:\n");
    prtMatrix(A);

    printf("\n");

    printf("B:\n");
    prtMatrix(B);

    printf("\n");

    printf("The A X B:\n");
    prtMatrix(C);

    return 0;
}


練習九:取代陣列內的元素
使用亂數函數 rand() 產生 10 個整數,尋找特定的數值,並用 -1 來取代。

Exercise 9: Replace an element in an array
Using rand() function to generate 10 integers. Then find a specific integer and replace it with -1. 


練習九參考解法:
Exercise 9 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int numbers[10], target = -1;

    srand(time(0));

    printf("Enter an integer between [0 - 9]:");
    scanf("%d", &target);

    printf("The numbers:\n");
    for(int i = 0; i < 10; i++)
    {
        numbers[i] = rand() % 10;
        printf("%2d,", numbers[i]);

        // Find the target and replace it with -1
        if(numbers[i] == target)
            numbers[i] = -1;
    }

    printf("\nAfter replacing, the numbers:\n");
    for(int i = 0; i < 10; i++)
    {
        printf("%2d,", numbers[i]);

    }

    printf("\n");
    return 0;
}

練習十:費氏數列
輸入一個正整數 N,使用陣列的方式來產生前 N 個的費氏數列。

Exercise 10: Fibonacci Sequence
Input a positive integer N. Then show the fibonacci series up to n terms.

練習十參考解法:
Exercise 10 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int fibo[1000];
    int N;
    int t;

    printf("Enter a positive integer [1-999]:");
    scanf("%d", &N);

    fibo[0] = 0;
    fibo[1] = 1;

    for(int i = 2; i < N; i++)
    {
        fibo[i] = fibo[i-1] + fibo[i-2];
    }


    for(int i = 0; i < N; i++)
        printf("%2d,", fibo[i]);

    printf("\n");
    return 0;
}

沒有留言:

張貼留言