<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    BeautifulMan

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      16 隨筆 :: 0 文章 :: 0 評論 :: 0 Trackbacks
    這一章感覺好難?。。。?br />學習筆記:(關于指針和多維數組)
    // 多維數組和指針
    #include <stdio.h>
    int main(void)
    {
        int zippo[4][2] = {{2, 4}, {6, 8}, {1, 3}, {5, 7}};
        /*
           zippo[0]是一個整數大小對象的地址,而zippo是兩個整數大小對象的地址。
           因為(一個)整數和兩個整數組成的數組開始于同一個地址,因此zippo和zippo[0]具有相同的數值。
           驗證:
           輸出也顯示出二維數組zippo的地址和一維數組zippo[0]的地址是相同的,均為相應的數組
           首元素的地址,它的值是和&zippo[0][0]相同的;
           而且,*zippo也是一個指針,它與一維數組zippo[0](也是一個指針)的地址相同,證明了我的猜想!
        
    */
        printf("===========首先驗證第一條結論===========\n");
        printf("zippo: \t\t%p\n&zippo[0]: \t%p\n", zippo, &zippo[0]);
        printf("zippo[0]: \t%p\n&zippo[0][0]: \t%p\n",zippo[0],&zippo[0][0]);
        printf("*zippo: \t%p\n&*zippo: \t%p\n", *zippo, &*zippo);
        printf("\n");
        /*
           zippo所指向對象的大小是兩個int,而zippo[0]所指向對象的大小是一個int
           驗證:
           zippo[0]指向4字節長的數據對象。對zippo[0]加1導致它的值增加4。數組名zippo是包含
           兩個int數的數組的地址,因此它指向8字節長的數據對象。所以,對zippo加1導致它的值增加8。
        
    */
        printf("===========然后驗證第二條結論===========\n");
        printf("zippo: \t\t%p\nzippo+1: \t%p\n", zippo, zippo+1);
        printf("zippo[0]: \t%p\nzippo[0]+1: \t%p\n", zippo[0], zippo[0]+1);
        printf("\n");
        /*
           *zippo也是一個指針,它與一維數組zippo[0](也是一個指針)的地址相同,它們指向同一個int變量
           即zippo[0][0]
           *zippo[0] = zippo[0][0]
           **zippo = *zippo[0] = zippo[0][0](得證)
           ------------------------------------------------
           分析*(*(zippo+2)+1)
           zippo------------------第1個大小為2個int的元素的地址
           zippo+2----------------第3個大小為2個int的元素的地址
           *(zippo+2)-------------第3個元素,即包含2個int值的數組,因此也是其第1個元素的(int值)的地址
           *(zippo+2)+1-----------包含2個int值的數組的第2個元素(int值)的地址
           *(*(zippo+2)+1)--------數組第3行第2列int(zippo[2][1])的值

           總結:更一般地,要表示單個元素,可以使用數組符號或指針符號;并且在這兩種表示中即可以使用
           數組名,也可以使用指針:
           zippo[m][n] == *(*(zippo+m)+n)
        
    */
        printf("===========最后驗證第三條結論===========\n");
        printf("*zippo: \t%p\nzippo[0]: \t%p\n", zippo, zippo[0]);
        printf("*(zippo+1): \t%p\nzippo[1]: \t%p\n", *(zippo+1), zippo[1]);
        printf("**zippo: \t%d\nzippo[0][0]: \t%d\n", **zippo, zippo[0][0]);
        printf("*(*(zippo+2)+1)\t%d\nzippo[2][1]: \t%d\n", *(*(zippo+2)+1), zippo[2][1]);
        return 0;
    }
    // 指針的兼容性
    #include <stdio.h>
    int main(void)
    {
        /*
           指針之間的賦值規則比數值類型的賦值更嚴格
           舉例說明:
        
    */
        int n = 5;
        double x;
        int * pi = &n;
        double * pd = &x;
        x = n;  // 不需要進行類型轉換就直接把一個int數值賦給一個double類型的變量(隱藏的類型轉換)
        pd = pi // 編譯時錯誤,原因:pd指向一個double類型的數值,pi指向一個int類型的數值

        int * pt;
        int (* pa) [3];
        int ar1[2][3];
        int ar2[3][2];
        int **p2; // (指向int指針)的指針
        pt = &ar1[0][0];   // pt為指向一個int數值的指針,ar1[0][0]是一個int數值的變量
        pt = ar1[0];       // pt為指向一個int數值的指針,ar1[0]也為指向一個int數值的指針
        pt = ar1;          // pt為指向一個int數值的指針,ar1指向由3個int值構成的指針(非法)
        pa = ar1;          // pa指向由3個int值構成的數組,ar1也指向由3個int值構成的數組
        pa = ar2;          // pa指向由3個int值構成的數組,ar2指向由2個int值構成的數組(非法)
        p2 = &pt;          // p2是(指向int指針)的指針,&pt(頭一次見,得記下來)也是(指向int指針)的指針
        *p2 = ar2[0];      // *p2為指向int的指針,ar2[0]也是指向int的指針
        p2 = ar2;          // p2是(指向int指針)的指針,ar2是指向由2個int值構成的數組(非法)
        return 0;
    }
    復習題
    1、下面程序將打印什么?
    #include <stdio.h>
    int main(void)
    {
        int ref[] = {8, 4, 0, 2};
        int *ptr;
        int index;

        for(index = 0, ptr = ref; index < 4; index++, ptr++)
            printf("%d %d\n", ref[index], *ptr);
        return 0;
    }
    答:
    8 8 
    4 4
    0 0
    2 2
    2、在第1題中,數組ref包含多少個元素?
    答:4
    3、在第1題中,ref是哪些數據的地址?ref+1呢?++ref指向什么?
    答:
    數組名ref指向數組的第一個元素(整數8),表達式ref+1指向第二個元素(整數4)。++ref不是合法的C表達式,因為ref是常量而不是變量。ref == &ref[0]
    4、下面每種情況中*ptr和*(ptr+2)的值分別是什么?
        a.
        int *ptr;
        int torf[2][2] = {12, 14, 16};
        ptr = torf[0];
        b.
        int *ptr;
        int fort[2][2] = { {12}, {14, 16} };
        ptr = fort[0];
    答:
    a.
    *ptr = 12
    *(ptr+2) = 16 注意:ptr+2指向第三個元素,它是第二行的第一個元素,而不是不確定的。
    b.
    *ptr = 12
    *(ptr+2) = 14 同上
    5、下面每種情況中**ptr和**(ptr+1)的值分別是什么?
        a.
        int (*ptr) [2];
        int torf[2][2] = {12, 14, 16};
        ptr = torf;
        b.
        int (*ptr) [2];
        int fort[2][2] = { {12}, {14, 16} };
        ptr = fort;
    答:
    a.
    **ptr = 12
    **(ptr + 1) = 16
    b.
    **ptr = 12
    **(ptr + 1) = 14
    6、假設有如下定義:
    int grid[30][100];
    a.用1種方法表示grid[22][56]的地址。
    b.用2種方法表示grid[22][0]的地址。
    c.用3種方法表示grid[0][0]的地址。
    答:
    a.
    &grid[22][56]
    b.
    &grid[22][0] or grid[22](不是&grid[22])
    c.
    &grid[0][0] or grid[0] or int * grid(這里grid[0]是整數元素grid[0][0]的地址,grid是具有100個元素的數組grid[0]的地址。這兩個地址具有相同的數值但是類型不同,類型指派可以使他們的類型相同)。
    7、用適當的方法聲明下面每個變量:
    a.digits:一個包含10個int值的數組
    b.rates:一個包含6個float值的數組
    c.mat:一個包含3個元素的數組,其中每個元素是一個包含5個整數的數組
    d.psa:一個包含20個指向char的指針的數組
    e.pstr:一個指向數組的指針,其中數組由20個char值構成
    答:
    a.
    int digits[10];
    b.
    float rates[6];
    c.
    int mat[3][5];
    d.
    char *(psa[20]); (psa是指針數組而不是指向數組的指針。具體地,psa會指向一個單個char(數組的第一個元素),psa+1會指向下一個字節)
    e.
    char (*pstr) [20];
    8、
    a.定義一個包含6個int值的數組,并且用數值1、2、4、8、16和32進行初始化。
    b.用數組符號表示a部分中數組的第3個元素(數值為4的那個元素)。
    c.假設系統支持C99規則,定義一個包含100個int值的數組并且初始化它,使它的末元素為-1,其他元素的值不考慮。
    答:
    a.
    int array[6] = {1, 2, 4, 8, 16, 32};
    b.
    array[2];
    c.
    int ar[100] = { [99] = -1 };
    9、包含10個元素的數組的索引范圍是什么?
    答:0~9
    10、假設有如下聲明:
    float rootbeer[10], things[10][5], *pf, value = 2.2;
    int i = 3;
    則下列語句中哪些是正確的,哪些是錯誤的?
    a.rootbeer[2] = value;
    b.scanf("%f", &rootbeer);
    c.rootbeer = value;
    d.printf("%f", rootbeer);
    e.things[4][4] = rootbeer[3];
    f.things[5] = rootbeer;
    g.pf = value;
    h.pf = rootbeer;
    答:
    a------正確
    b------錯誤(注意:rootbeer不是一個float變量
    c------錯誤
    d------錯誤
    e------正確
    f ------錯誤(注意:不能使用數組賦值)
    g------錯誤
    h------正確
    11、聲明一個800x600的int數組。
    答:
    int array[800][600];
    12、以下是3個數組聲明:
    double trots[20];
    short clops[10][30];
    long shots[5][10][15];
    a.以傳統的void函數方式寫出處理數組trots的函數原型和函數調用;然后以變長數組方式,寫出處理數組trots的函數原型和函數調用。
    b.以傳統的void函數方式寫出處理數組clops的函數原型和函數調用;然后以變長數組方式,寫出處理數組clops的函數原型和函數調用。
    c.以傳統的void函數方式寫出處理數組shots的函數原型和函數調用;然后以變長數組方式,寫出處理數組shots的函數原型和函數調用。
    答:
    a.
    void sum(double *pt, int n);
    sum(trots, 20);
    -------------------------------------
    void sum(int n, double ar[n]);
    sum(20, trots);
    b.
    void sum(short (*pt) [30], int n);
    sum(clops, 10);
    -------------------------------------
    void sum(int n, int m, short ar[n][m]);
    sum(10, 30, clops);
    c.
    void sum(long ar[][10][15], int n);
    sum(shots, 5);
    -------------------------------------
    void sum(int n, int m, int q, long ar[n][m][q]);
    sum(5, 10, 15, shots);
    13、下面是兩個函數原型:
    void show(double ar[], int n);                     //n是元素數
    void show2(double ar2[][3], int n);           //n是行數 
    a.編寫一個函數調用,把包含數值8、3、9和2的復合文字傳遞給函數shows()。
    b.編寫一個函數調用,把包含2行3列數值的復合文字傳遞給函數show2(),其中第一行為8、3、9;第二行為5、4、1。
    答:
    a.
    show((double [4]) {8, 3, 9, 2}, 4);
    b.
    show2((double [][3]) { {8, 3, 9}, {5, 4, 1} }, 2);
    編程練習(哈哈哈?。。☆}目感覺越來越簡單了呢!除了最后一題外,好高興?。。。?/span>
    1、
    #include <stdio.h>
    #define MONTHS 12
    #define YEARS 5
    int main(void)
    {
        const float rain[YEARS][MONTHS] = {
            {4.3, 4.3, 4.3, 3.0, 2.0, 1.2, 0.2, 0.2, 0.4, 2.4, 3.5, 6.6},
            {8.5, 8.2, 1.2, 1.6, 2.4, 0.0, 5.2, 0.9, 0.3, 0.9, 1.4, 7.3},
            {9.1, 8.5, 6.7, 4.3, 2.1, 0.8, 0.2, 0.2, 1.1, 2.3, 6.1, 8.4},
            {7.2, 9.9, 8.4, 3.3, 1.2, 0.8, 0.4, 0.0, 0.6, 1.7, 4.3, 6.2},
            {7.6, 5.6, 3.8, 2.8, 3.8, 0.2, 0.0, 0.0, 0.0, 1.3, 2.6, 5.2}
        };
        int year, month;
        float subtot, total;
        const float (*po) [MONTHS] = rain;

        printf(" YEAR   RAINFALL (inches) \n");
        for(year = 0, total = 0; year < YEARS; year++)
        {
            for(month = 0, subtot = 0; month < MONTHS; month++)
                subtot += *(*(po + year) + month);
            printf("%5d %15.1f\n", 2000 + year, subtot);
            total += subtot;
        }
        printf("\nThe yearly average is %.1f inches.\n\n", total/YEARS);
        printf("MONTHLY AVERAGES: \n\n");
        printf("Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
        printf("Nov Dec\n");
        for(month = 0; month < MONTHS; month++)
        {
            for(year = 0, subtot = 0; year < YEARS; year++)
                subtot += *(*(po + year) + month);
            printf("%4.1f ", subtot/YEARS);
        }
        printf("\n");
        return 0;
    }
    2、
    #include <stdio.h>
    void copy_arr(const double sou[], double tar[], int n);
    void copy_ptr(const double *sou, double *tar, int n);
    int main(void)
    {
        int i;
        const double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
        double target1[5];
        double target2[5];
        copy_arr(source, target1, 5);
        copy_ptr(source, target2, 5);

        printf("--------------輸出驗證----------------\n");
        for(i = 0; i < 5; i++)
            printf("target1[%d] = %.1f\ttarget2[%d] = %.1f\n", i, target1[i], i, target2[i]);
        return 0;
    }
    void copy_arr(const double sou[], double tar[], int n)
    {
        int i;

        for(i = 0; i < n; i++)
            tar[i] = sou[i];
    }
    void copy_ptr(const double *sou, double *tar, int n)
    {
        int i;

        for(i = 0; i < n; i++)
            *(tar + i) = *(sou + i);
    }
    3、
    #include <stdio.h>
    int get_max(const int *ar, int n);
    int main(void)
    {
        const int source[5] = {16, 2, 78, 990, 123};

        printf("--------------輸出驗證----------------\n");
        printf("max is %d\n", get_max(source, 5));
        return 0;
    }
    int get_max(const int *ar, int n)
    {
        int i, max;
        max = *ar;
        for(i = 1; i < n; i++)
        {
            max = *(ar + i) > max ? *(ar + i) : max;
        }
        return max;
    }
    4、(沒必要做兩次循環,一次循環就夠了,真是寫的羅里吧嗦的?。?br />
    #include <stdio.h>
    int get_max_index(const double *ar, int n);
    int main(void)
    {
        const double source[5] = {16.3, 2.2, 78.78, 990.99, 123};

        printf("--------------輸出驗證----------------\n");
        printf("index is %d\n", get_max_index(source, 5));
        return 0;
    }
    int get_max_index(const double *ar, int n)
    {
        int i, index;
        double max = *ar;
        for(i = 1; i < n; i++)
        {
            max = *(ar + i) > max ? *(ar + i) : max;
        }
        for(i = 0; i < n; i++)
        {
            if(*(ar + i) == max)
            {
                index = i;
                break;
            }
        }
        return index;
    }
    改進之后的程序代碼:
    #include <stdio.h>
    #define SIZE 5
    int max(double arr[], int n);
    int main(void)
    {
        double source[SIZE] = {1.89, 90.00, 56.78, 789.78, 23.34};

        printf("The max index is %d\n", max(source, SIZE));
        return 0;
    }
    int max(double arr[], int n)
    {
        int i = 0, index;
        int max = arr[i];
        for(i = 1; i < n; i++)
            if(arr[i] > max)
            {
                max = arr[i];
                index = i;
            }
        return index;
    }
    5、
    #include <stdio.h>
    double get_max_min(const double *ar, int n);
    int main(void)
    {
        const double source[5] = {16.3, 2.2, 78.78, 990.99, 123};

        printf("--------------輸出驗證----------------\n");
        printf("max - min = %.2f\n", get_max_min(source, 5));
        return 0;
    }
    double get_max_min(const double *ar, int n)
    {
        int i;
        double max = *ar;
        double min = *ar;
        for(i = 1; i < n; i++)
        {
            max = *(ar + i) > max ? *(ar + i) : max;
            min = *(ar + i) < min ? *(ar + i) : min;
        }
        return max - min;
    }
    6、
    #include <stdio.h>
    #define ROWS 3
    #define COLS 4
    void copy_ptr(double (*sou) [COLS], double (*tar) [COLS], int rows);
    int main(void)
    {
        int i;
        int j;
        double source[ROWS][COLS] = {
            {2.1, 3.4, 78.9, 23.3},
            {231.1, 45.5, 34, 12},
            {23.7, 567.8, 56.5, 32}
        };
        double target[ROWS][COLS];
        copy_ptr(source, target, ROWS);
        printf("--------------Output verification----------------\n");
        for(i = 0; i < ROWS; i++)
        {
            for(j = 0; j < COLS; j++)
                printf("%.1f\t", *(*(target + i) + j));
            printf("\n");
        }
        return 0;
    }
    void copy_ptr(double (*sou) [COLS], double (*tar) [COLS], int rows)
    {
        int i;
        int j;
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < COLS; j++)
                *(*(tar + i) + j) = *(*(sou + i) + j);
        }
    }
    7、
    #include <stdio.h>
    void copy_ptr(double *sou, double *tar, int n);
    int main(void)
    {
        int i;
        double source[7] = {12.12, 23.4, 34.23, 1, 1.2, 5.6, 67.78};
        double target[3];

        copy_ptr(source, target, 3);
        printf("--------------Output verification----------------\n");
        for(i = 0; i < 3; i++)
            printf("%.2f\n", *(target + i));
        return 0;
    }
    void copy_ptr(double *sou, double *tar, int n)
    {
        int i;

        for(i = 0; i < n; i++)
            *(tar + i) = *(sou + i + n - 1);
    }
    8、
    #include <stdio.h>
    #define ROWS 3
    #define COLS 5
    void copy_ptr(double (*sou)[COLS], int n, int m, double tar[n][m]);
    void show_arr(int n, int m, double ar[n][m]);
    int main(void)
    {
        double source[ROWS][COLS] = {
            {23.12, 45.66, 45.0, 89.9, 77.6},
            {11.1, 22.22, 4.45, 34.3, 4},
            {22.1, 789.99, 34.23, 12.12, 56}
        };
        double target[ROWS][COLS];

        copy_ptr(source, ROWS, COLS, target);
        printf("--------------show array source----------------\n");
        show_arr(ROWS, COLS, source);
        printf("--------------show array target----------------\n");
        show_arr(ROWS, COLS, target);
        return 0;
    }
    void copy_ptr(double (*sou)[COLS], int n, int m, double tar[n][m])
    {
        int r;
        int c;

        for(r = 0; r < n; r++)
        {
            for(c = 0; c < m; c++)
                *(*(tar + r) + c) = *(*(sou + r) + c);
        }
    }
    void show_arr(int n, int m, double ar[n][m])
    {
        int r;
        int c;

        for(r = 0; r < n; r++)
        {
            for(c = 0; c < m; c++)
                printf("%.2f\t", ar[r][c]);
            printf("\n");
        }
    }
    9、
    #include <stdio.h>
    void sum_array(int *ar1, int *ar2, int *ar3, int n);
    int main(void)
    {
        int i;
        int array1[4] = {2, 4, 5, 8};
        int array2[4] = {1, 0, 4, 6};
        int array3[4];

        sum_array(array1, array2, array3, 4);
        printf("--------------Output verification----------------\n");
        for(i = 0; i < 4; i++)
            printf("%d\t", *(array3 + i));
        return 0;
    }
    void sum_array(int *ar1, int *ar2, int *ar3, int n)
    {
        int i;

        for(i = 0; i < n; i++)
            *(ar3 + i) = *(ar1 + i) + *(ar2 + i);
    }
    10、
    #include <stdio.h>
    #define ROWS 3
    #define COLS 5
    void show_array(int (*ar)[COLS], int rows);
    void double_array(int (*ar)[COLS], int rows);
    int main(void)
    {
        int source[ROWS][COLS] = {
            {1, 2, 3, 4, 5},
            {2, 3, 4, 5, 6},
            {3, 4, 5, 6, 7}
        };

        printf("--------------show array source----------------\n");
        show_array(source, ROWS);
        double_array(source, ROWS);
        printf("--------------again show array source----------------\n");
        show_array(source, ROWS);
        return 0;
    }
    void show_array(int (*ar)[COLS], int rows)
    {
        int r;
        int c;

        for(r = 0; r < rows; r++)
        {
            for(c = 0; c < COLS; c++)
                printf("%d\t", *(*(ar + r) + c));
            printf("\n");
        }
    }
    void double_array(int (*ar)[COLS], int rows)
    {
        int r;
        int c;

        for(r = 0; r < rows; r++)
        {
            for(c = 0; c < COLS; c++)
                *(*(ar + r) + c) *= 2;
        }
    }
    11、(感覺代碼越寫越多了,與之前沒簡便到哪兒去)
    #include <stdio.h>
    #define MONTHS 12
    #define YEARS 5
    //對于每一年,計算各月的總降水量并把各個值存儲在一個數組中
    void fun1(float (*ye)[MONTHS], float * yea);
    // 對于每一年,顯示各月的總降水量
    void show_array1(float *ar);
    // 計算年降水平均量
    float sum1(float *ar);
    // 對于每個月,計算月降水平均量并把各個值存儲在一個數組中
    void fun2(float (*ye)[MONTHS], float * mon);
    // 對于每個月,顯示月降水平均量
    void show_array2(float *ar);
    int main(void)
    {
        const float rain[YEARS][MONTHS] = {
            {4.3, 4.3, 4.3, 3.0, 2.0, 1.2, 0.2, 0.2, 0.4, 2.4, 3.5, 6.6},
            {8.5, 8.2, 1.2, 1.6, 2.4, 0.0, 5.2, 0.9, 0.3, 0.9, 1.4, 7.3},
            {9.1, 8.5, 6.7, 4.3, 2.1, 0.8, 0.2, 0.2, 1.1, 2.3, 6.1, 8.4},
            {7.2, 9.9, 8.4, 3.3, 1.2, 0.8, 0.4, 0.0, 0.6, 1.7, 4.3, 6.2},
            {7.6, 5.6, 3.8, 2.8, 3.8, 0.2, 0.0, 0.0, 0.0, 1.3, 2.6, 5.2}
        };
        float year_rain[YEARS];
        float month_rain[MONTHS];

        fun1(rain, year_rain);
        fun2(rain, month_rain);
        printf(" YEAR   RAINFALL (inches) \n");
        show_array1(year_rain);
        printf("\nThe yearly average is %.1f inches.\n\n", sum1(year_rain));

        printf("MONTHLY AVERAGES: \n\n");
        printf("Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
        printf("Nov Dec\n");
        show_array2(month_rain);
        printf("\n");
        return 0;
    }
    void fun1(float (*ye)[MONTHS], float * yea)
    {
        float subtot;
        int year, month;

        for(year = 0 ; year < YEARS; year++)
        {
            for(month = 0, subtot = 0; month < MONTHS; month++)
                subtot += *(*(ye + year) + month);
            *(yea + year) = subtot;
        }
    }
    void show_array1(float *ar)
    {
        int i;

        for(i = 0; i < YEARS; i++)
            printf("%5d %15.1f\n", 2000 + i, *(ar + i));
    }
    float sum1(float *ar)
    {
        int i;
        float total;

        for(i = 0; i < YEARS; i++)
            total += *(ar + i);
        return total / YEARS;
    }
    void fun2(float (*ye)[MONTHS], float * mon)
    {
        int year, month;
        float subtot;

        for(month = 0; month < MONTHS; month++)
        {
            for(year = 0, subtot = 0; year < YEARS; year++)
                subtot += *(*(ye + year) + month);
            *(mon + month) = subtot / YEARS;
        }
    }
    void show_array2(float *ar)
    {
        int i;

        for(i = 0; i < MONTHS; i++)
            printf("%4.1f", *(ar + i));
    }
    有必要寫那么多的函數嗎?只須寫兩個函數就可以搞定的,非得寫那么多,改進之后程序如下:
    #include <stdio.h>
    #define MONTHS 12
    #define YEARS 5
    // 計算年降水總量與所有年度的總降水量
    double calculate1(const float arr[][MONTHS], int y);
    // 計算各年該月份的總降水量
    void calculate2(const float arr[][MONTHS], int y);
    int main(void)
    {
        const float rain[YEARS][MONTHS] = {
            {4.3, 4.3, 4.3, 3.0, 2.0, 1.2, 0.2, 0.2, 0.4, 2.4, 3.5, 6.6},
            {8.5, 8.2, 1.2, 1.6, 2.4, 0.0, 5.2, 0.9, 0.3, 0.9, 1.4, 7.3},
            {9.1, 8.5, 6.7, 4.3, 2.1, 0.8, 0.2, 0.2, 1.1, 2.3, 6.1, 8.4},
            {7.2, 9.9, 8.4, 3.3, 1.2, 0.8, 0.4, 0.0, 0.6, 1.7, 4.3, 6.2},
            {7.6, 5.6, 3.8, 2.8, 3.8, 0.2, 0.0, 0.0, 0.0, 1.3, 2.6, 5.2}
        };
        float total;

        printf(" YEAR   RAINFALL (inches) \n");
        total = calculate1(rain, YEARS);
        printf("\nThe yearly average is %.1f inches.\n\n", total/YEARS);
        printf("MONTHLY AVERAGES: \n\n");
        printf("Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
        printf("Nov Dec\n");
        calculate2(rain, YEARS);
        printf("\n");
        return 0;
    }
    double calculate1(const float arr[][MONTHS], int y)
    {
        int year, month;
        double total, subtot;

        for(year = 0, total = 0; year < y; year++)
        {
            for(month = 0, subtot = 0; month < MONTHS; month++)
                subtot += arr[year][month];
            printf("%5d %15.1f\n", 2000 + year, subtot);
            total += subtot;
        }
        return total;
    }
    void calculate2(const float arr[][MONTHS], int y)
    {
        int year, month;
        double subtot;

        for(month = 0; month < MONTHS; month++)
        {
            for(year = 0, subtot = 0; year < y; year++)
                subtot += arr[year][month];
            printf("%4.1f ", subtot/YEARS);
        }
    }
    12、(關于如何輸入數字,沒搞明白,還是借鑒CSDN----vs9841前輩的做法,不過后面都是自己寫了
    #include <stdio.h>
    #define ROWS 3
    #define COLS 5
    // 從鍵盤獲取一個double數
    double get_double(void);
    // 向source[ROWS][COLS]中輸入數值
    void input_double(int n, int m, double (*dou)[COLS]);
    // 計算每個數集的平均值
    void get_average(int n, int m, double (*dou)[COLS]);
    // 計算所有數值的平均值
    double get_all_average(int n, int m, double (*dou)[COLS]);
    // 找出所有數中的最大值
    double get_max(int n, int m, double (*dou)[COLS]);
    int main(void)
    {
        double source[ROWS][COLS];
        input_double(ROWS, COLS, source);
        printf("----------------------------------------------------\n");
        get_average(ROWS, COLS, source);
        printf("----------------------------------------------------\n");
        printf("Mean values of all the numbers: %5.2f\n", get_all_average(ROWS, COLS, source));
        printf("----------------------------------------------------\n");
        printf("Maximum value for all: %5.2f\n", get_max(ROWS, COLS, source));
        return 0;
    }
    double get_double(void)
    {
        double input;
        char ch;

        while(scanf("%lf", &input) != 1)
        {
            while((ch = getchar()) != '\n')
                putchar(ch);
            printf(" is not a double.\nPlease enter a ");
            printf("double value, such as 23.3, -12.1, or 3: \n");
        }
        return input;
    }
    void input_double(int n, int m, double (*dou)[COLS])
    {
        int i, j;

        printf("Please enter data of %dx%d two dimensional array\n", n, m);
        for(i = 0; i < n; i++)
        {
            printf("Start with %d sets of numbers: \n", i+1);
            for(j = 0; j < m; j++)
            {
                printf("%d number: ", j+1); // 記住每次只能處理輸入一個數
                dou[i][j] = get_double();
            }
        }
        printf("Data entry is complete, as shown below: \n");
        for(i = 0; i < n; i++)
        {
            printf("%d sets of numbers: \n", i+1);
            for(j = 0; j < m; j++)
                printf("%5.2f\t", dou[i][j]);
            printf("\n");
        }
    }
    void get_average(int n, int m, double (*dou)[COLS])
    {
        int r;
        int c;
        double total = 0;

        for(r = 0; r < n; r++)
        {
            printf("average of the %d sets of numbers: ", r + 1);
            for(c = 0, total = 0; c < m; c++)
                total += dou[r][c];
            printf("%5.2f\n", total / m);
        }
    }
    double get_all_average(int n, int m, double (*dou)[COLS])
    {
        int r;
        int c;
        double total = 0;

        for(r = 0; r < n; r++)
        {
            for(c = 0; c < m; c++)
                total += dou[r][c];
        }
        return total / (n * m);
    }
    double get_max(int n, int m, double (*dou)[COLS])
    {
        int r;
        int c;
        double max = dou[0][0];


        for(r = 0; r < n; r++)
        {
            for(c = 0; c < m; c++)
            {
                max = max > dou[r][c] ? max : dou[r][c];
            }
        }
        return max;
    }
    第二次更新如下,完全自己手寫:(可能對自己好理解一點)
    #include <stdio.h>
    void task_a(double arr[][5], int n);
    void task_b(double arr[][5], int n);
    double task_c(double arr[][5], int n);
    double task_d(double arr[][5], int n);
    void task_e(double arr[][5], int n);
    int main(void)
    {
        double array[3][5];

        task_a(array, 3);
        task_b(array, 3);
        printf("所有數值的平均數為:%.2f\n", task_c(array, 3));
        printf("這15個數中的最大值為:%.2f\n", task_d(array, 3));
        printf("該3x5數組為:\n");
        task_e(array, 3);
        printf("\n");
        return 0;
    }
    void task_a(double arr[][5], int n)
    {
        int i = 0;
        int count;
        double num;

        printf("請輸入3個數集\n");
        while(i < n)
        {
            printf("請輸入第%d個數集:\n", i + 1);
            count = 0;
            printf("請輸入第%d個數:", count + 1);
            while(scanf("%lf", &num) == 1 && count < 5)
            {
                arr[i][count] = num;
                if(count == 4)
                    break;
                count++;
                printf("請輸入第%d個數:", count + 1);
            }
            i++;
        }
    }
    void task_b(double arr[][5], int n)
    {
        double tot;

        for(int r = 0; r < n; r++)
        {
            tot = 0;
            for(int c = 0; c < 5; c++)
                tot += arr[r][c];
            printf("第%d個數集的平均值為: %.2f\n", r + 1, tot / 5);
        }
    }
    double task_c(double arr[][5], int n)
    {
        double total = 0;

        for(int r = 0; r < n; r++)
            for(int c = 0; c < 5; c++)
                total += arr[r][c];
        return total / 15;
    }
    double task_d(double arr[][5], int n)
    {
        double max;
        max = arr[0][0];

        for(int r = 0; r < n; r++)
            for(int c = 0; c < 5; c++)
                if(arr[r][c] > max)
                    max = arr[r][c];
        return max;
    }
    void task_e(double arr[][5], int n)
    {
        for(int r = 0; r < n; r++)
        {
            for(int c = 0; c < 5; c++)
                printf("%.2f ", arr[r][c]);
            printf("\n");
        }
    }
    13、
    同上,第二次更新如下:
    #include <stdio.h>
    void task_a(int n, int m, double arr[n][m]);
    void task_b(int n, int m, double arr[n][m]);
    double task_c(int n, int m, double arr[n][m]);
    double task_d(int n, int m, double arr[n][m]);
    void task_e(int n, int m, double arr[n][m]);
    int main(void)
    {
        double array[3][5];

        task_a(3, 5, array);
        task_b(3, 5, array);
        printf("所有數值的平均數為:%.2f\n", task_c(3, 5, array));
        printf("這15個數中的最大值為:%.2f\n", task_d(3, 5, array));
        printf("該3x5數組為:\n");
        task_e(3, 5, array);
        printf("\n");
        return 0;
    }
    void task_a(int n, int m, double arr[n][m])
    {
        int i = 0;
        int count;
        double num;

        printf("請輸入3個數集\n");
        while(i < n)
        {
            printf("請輸入第%d個數集:\n", i + 1);
            count = 0;
            printf("請輸入第%d個數:", count + 1);
            while(scanf("%lf", &num) == 1 && count < m)
            {
                arr[i][count] = num;
                if(count == 4)
                    break;
                count++;
                printf("請輸入第%d個數:", count + 1);
            }
            i++;
        }
    }
    void task_b(int n, int m, double arr[n][m])
    {
        double tot;

        for(int r = 0; r < n; r++)
        {
            tot = 0;
            for(int c = 0; c < m; c++)
                tot += arr[r][c];
            printf("第%d個數集的平均值為: %.2f\n", r + 1, tot / 5);
        }
    }
    double task_c(int n, int m, double arr[n][m])
    {
        double total = 0;

        for(int r = 0; r < n; r++)
            for(int c = 0; c < m; c++)
                total += arr[r][c];
        return total / 15;
    }
    double task_d(int n, int m, double arr[n][m])
    {
        double max;
        max = arr[0][0];

        for(int r = 0; r < n; r++)
            for(int c = 0; c < m; c++)
                if(arr[r][c] > max)
                    max = arr[r][c];
        return max;
    }
    void task_e(int n, int m, double arr[n][m])
    {
        for(int r = 0; r < n; r++)
        {
            for(int c = 0; c < m; c++)
                printf("%.2f ", arr[r][c]);
            printf("\n");
        }
    }
    首次做的如下:
    #include <stdio.h>
    #define ROWS 3
    #define COLS 5
    // 從鍵盤獲取一個double數
    double get_double(void);
    // 向source[ROWS][COLS]中輸入數值
    void input_double(int n, int m, double dou[n][m]);
    // 計算每個數集的平均值
    void get_average(int n, int m, double dou[n][m]);
    // 計算所有數值的平均值
    double get_all_average(int n, int m, double dou[n][m]);
    // 找出所有數中的最大值
    double get_max(int n, int m, double dou[n][m]);
    int main(void)
    {
        double source[ROWS][COLS];
        input_double(ROWS, COLS, source);
        printf("----------------------------------------------------\n");
        get_average(ROWS, COLS, source);
        printf("----------------------------------------------------\n");
        printf("Mean values of all the numbers: %5.2f\n", get_all_average(ROWS, COLS, source));
        printf("----------------------------------------------------\n");
        printf("Maximum value for all: %5.2f\n", get_max(ROWS, COLS, source));
        return 0;
    }
    double get_double(void)
    {
        double input;
        char ch;
        while(scanf("%lf", &input) != 1)
        {
            while((ch = getchar()) != '\n')
                putchar(ch);
            printf(" is not a double.\nPlease enter a ");
            printf("double value, such as 23.3, -12.1, or 3: \n");
        }
        return input;
    }
    void input_double(int n, int m, double dou[n][m])
    {
        int i, j;
        printf("Please enter data of %dx%d two dimensional array\n", n, m);
        for(i = 0; i < n; i++)
        {
            printf("Start with %d sets of numbers: \n", i+1);
            for(j = 0; j < m; j++)
            {
                printf("%d number: ", j+1); // 記住每次只能處理輸入一個數
                dou[i][j] = get_double();
            }
        }
        printf("Data entry is complete, as shown below: \n");
        for(i = 0; i < n; i++)
        {
            printf("%d sets of numbers: \n", i+1);
            for(j = 0; j < m; j++)
                printf("%5.2f\t", dou[i][j]);
            printf("\n");
        }
    }
    void get_average(int n, int m, double dou[n][m])
    {
        int r;
        int c;
        double total = 0;
        for(r = 0; r < n; r++)
        {
            printf("average of the %d sets of numbers: ", r + 1);
            for(c = 0, total = 0; c < m; c++)
                total += dou[r][c];
            printf("%5.2f\n", total / m);
        }
    }
    double get_all_average(int n, int m, double dou[n][m])
    {
        int r;
        int c;
        double total = 0;
        for(r = 0; r < n; r++)
        {
            for(c = 0; c < m; c++)
                total += dou[r][c];
        }
        return total / (n * m);
    }
    double get_max(int n, int m, double dou[n][m])
    {
        int r;
        int c;
        double max = dou[0][0];
        for(r = 0; r < n; r++)
        {
            for(c = 0; c < m; c++)
            {
                max = max > dou[r][c] ? max : dou[r][c];
            }
        }
        return max;
    }
    posted on 2015-11-24 22:31 李阿昀 閱讀(857) 評論(0)  編輯  收藏 所屬分類: C Primer Plus 復習題與編程練習
    主站蜘蛛池模板: 国产综合免费精品久久久| 成av免费大片黄在线观看| 国产又黄又爽胸又大免费视频| 最近中文字幕mv免费高清在线| 日韩视频免费一区二区三区| 亚洲色大成网站WWW久久九九| 亚洲av无码一区二区三区观看| 一级毛片**免费看试看20分钟| 亚洲视频免费在线看| 亚洲国产高清精品线久久| 亚洲首页在线观看| 日本视频免费观看| 国产成人精品免费午夜app | 亚洲国产精品第一区二区三区| 亚洲视频2020| 色噜噜狠狠色综合免费视频| 最近最新高清免费中文字幕| 亚洲国产精品尤物YW在线观看| 亚洲性猛交xx乱| 亚洲黄片手机免费观看| 最近中文字幕免费mv视频7| 亚洲精品亚洲人成人网| 亚洲精品无码专区在线播放| 久久国产色AV免费观看| 久久亚洲中文字幕精品一区四 | 成人免费无码H在线观看不卡| 无码视频免费一区二三区| 亚洲国产精品无码久久久不卡| 99亚洲乱人伦aⅴ精品| 中文字幕免费在线观看| 国产亚洲人成网站在线观看| 亚洲女女女同性video| 中文字幕免费在线| 日韩亚洲欧洲在线com91tv| 在线观看亚洲视频| 99视频在线精品免费观看6| 亚洲短视频男人的影院| 国产精品免费看久久久香蕉| 国产国产人免费视频成69大陆| 亚洲最大成人网色香蕉| 99视频精品全部免费观看|