天天看點

第十章 數組和指針

  • 程式清單10.1,day_mon1.c:
/* day_mon1.c -- 列印每個月的天數 */ 
#include <stdio.h>
#define MONTHS 12

int main(void)
{
    int days[MONTHS] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int index;
    
    for (index = 0; index < MONTHS; index++)
        printf("Month %2d has %2d days,\n", index + 1, days[index]);
        
    return 0;
} 
           

輸出結果:

第十章 數組和指針
  • 程式清單10.2,no_data.c:
/* no_data.c -- 未初始化數組 */ 
#include <stdio.h>
#define SIZE 4

int main(void)
{
    int no_data[SIZE];  /* 未初始化數組 */ 
    int i;
    
    printf("%2s%14s\n", "i", "no_data[i]");
    for (i = 0; i < SIZE; i++)
        printf("%2d%14d\n", i, no_data[i]);
        
    return 0;
}
           

輸出結果:

第十章 數組和指針
  • 程式清單10.3,some_data.c:
/* some_data.c -- 部分初始化數組 */ 
#include <stdio.h>
#define SIZE 4

int main(void)
{
    int some_data[SIZE] = {1492, 1066};
    int i;
    
    printf("%2s%14s\n", "i", "some_data[i]");
    for (i = 0; i < SIZE; i++)
        printf("%2d%14d\n", i, some_data[i]);
        
    return 0;
}
           

輸出結果:

第十章 數組和指針
  • 程式清單10.4,day_mon2.c:
/* day_mon2.c -- 讓編譯器計算元素個數 */ 
#include <stdio.h>

int main(void)
{
    const int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31 };
    int index;
    
    for (index = 0; index < (int) (sizeof days / sizeof days[0]); index++)
        printf("Month %2d has %d days.\n", index + 1, days[index]);
        
    return 0;
}
           

輸出結果:

第十章 數組和指針
  • 程式清單10.5,designate.c:
// designate.c -- 使用指定初始化器 
#include <stdio.h>
#define MONTHS 12

int main(void)
{
    int days[MONTHS] = { 31, 28, [4] = 31, 30, 31, [1] = 29 };
    int i;
    
    for (i = 0; i < MONTHS; i++)
        printf("%2d %d\n", i + 1, days[i]);
    
    return 0;
}
           

輸出結果:

第十章 數組和指針
  • 程式清單10.6,bounds.c:
// bounds.c -- 數組下标越界 
#include <stdio.h>
#define SIZE 4

int main(void)
{
    int value1 = 44;
    int arr[SIZE];
    int value2 = 88;
    int i;
    
    printf("valuel = %d, value2 = %d\n", value1, value2);
    for (i = -1; i <= SIZE; i++)
        arr[i] = 2 * i + 1;
    
    for (i = -1; i < 7; i++)
        printf("%2d %d\n", i, arr[i]);
    printf("value1 = %d, value2 = %d\n", value1, value2);
    printf("address of arr[-1]: %p\n", &arr[-1]);
    printf("address of arr[4]: %p\n", &arr[4]);
    printf("address of value1: %p\n", &value1);
    printf("address of value2: %p\n", &value2);
    
    return 0;
}
           

輸出結果:

第十章 數組和指針
  • 程式清單10.7,rain.c:
/* rain.c -- 計算每年的總降水量、年平均降水量和 5 年中每月的平均降水量 */ 
#include <stdio.h>
#define MONTHS 12 // 一年的月份數 
#define YEARS 5   // 年數 

int main(void)
{
    // 用 2020~2014年的降水量資料初始化數組 
    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;
    
    printf(" YEAR          RAINFALL (inches)\n");
    for (year = 0, total = 0; year < YEARS; year++)
    { // 每一年,各月的降水量總和 
        for (month = 0, subtot = 0; month < MONTHS; month++)
            subtot += rain[year][month];
        printf("%5d %15.1f\n", 2010 + year, subtot);
        total += subtot; // 5 年的總降水量 
    }
    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++)
    { // 每個月,5年的總降水量 
        for (year = 0, subtot = 0; year < YEARS; year++)
            subtot += rain[year][month];
        printf("%4.1f ", subtot / YEARS);
    }
    printf("\n");
    
    return 0;
}
           

輸出結果:

第十章 數組和指針
  • 程式清單10.8,pnt_add.c:
// pnt_add.c -- 指針位址 
#include <stdio.h>
#define SIZE 4

int main(void)
{
    short dates[SIZE];
    short * pti;
    short index;
    double bills[SIZE];
    double * ptf;
    
    pti = dates; // 把數組位址賦給指針 
    ptf = bills;
    
    printf("%23s %15s\n", "short", "double");
    for (index = 0; index < SIZE; index++)
        printf("pointers + %d: %10p %10p\n", index, pti + index, ptf + index);
    
    return 0;
}
           

輸出結果:

第十章 數組和指針
  • 程式清單10.8,day_mon3.c:
/* day_mon3.c -- uses pointer notation */ 
#include <stdio.h>
#define MONTHS 12

int main(void)
{
    int days[MONTHS] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int index;
    
    for (index = 0; index < MONTHS; index++)
        printf("Month %2d has %d days.\n", index + 1,
                    *(days + index));
    
    return 0;
}
           

輸出結果:

第十章 數組和指針
  • 程式清單10.10,sum_arr1.c:
// sum_arr1.c -- 數組元素之和
// 如果編譯器不支援 %zd, 用 %u 或 %lu 替換它 
#include <stdio.h>
#include <ctype.h>
#define SIZE 10

int sum(int ar[], int n);
int main(void)
{
    int marbles[SIZE] = { 20, 10, 5, 39, 4, 16, 19, 26, 31, 20 };
    long answer;
    
    answer = sum(marbles, SIZE);
    printf("The total number of marbles is %ld.\n", answer);
    printf("The size of marbles is %zd bytes.\n",
            sizeof marbles);
    
    return 0;
}

int sum(int ar[], int n) // 這個數組的大小是? 
{
    int i;
    int total = 0;
    
    for (i = 0; i < n; i++)
        total += ar[i];
    printf("The size of ar is %zd bytes.\n", sizeof ar);
    
    return total;
}
           

輸出結果:

第十章 數組和指針
  • 程式清單10.11,sum_arr2.c:
/* sum_arr2.c -- 數組元素之和 */ 
#include <stdio.h>
#define SIZE 10

int sump(int * start, int * end);

int main(void)
{
    int marbles[SIZE] = { 20, 10, 5, 39, 4, 16, 19, 26, 31, 20 };
    long answer;
    
    answer = sump(marbles, marbles + SIZE);
    printf("The total number of marbles is %ld.\n", answer);
    
    return 0;
}

/* 使用指針算法 */ 
int sump(int * start, int * end) //或者int sum(int start[], int * end) 
{
    int total = 0;
    
    while (start < end)
    {
        total += *start;  // 把數組元素的值加起來 
        start++;          // 讓指針指向下一個元素 
    }
    
    return total;
}
           

輸出結果:

第十章 數組和指針
  • 程式清單10.12,order.c:
/* order.c -- 指針運算中的優先級 */ 
#include <stdio.h>

int data[2] = { 100, 200 };
int moredata[2] = { 200, 400 };

int main(void)
{
    int * p1, *p2, *p3;
    
    p1 = p2 = data;
    p3 = moredata;
    printf(" *p1 = %d,  *p2 = %d,  *p3 = %d\n", *p1, *p2, *p3);
    printf("*p1++ = %d, *++p2 = %d, (*p3)++ = %d\n", *p1++, *++p2, (*p3)++);
    printf(" *p1 = %d,  *p2 = %d,  *p3 = %d\n", *p1, *p2, *p3);
    
    return 0;
}
           

輸出結果:

第十章 數組和指針
  • 程式清單10.13,ptr_ops.c:
// ptr_ops.c -- 指針操作 
#include <stdio.h>

int main(void)
{
    int urn[5] = { 100, 200, 300, 400, 500 };
    int * ptr1, * ptr2, * ptr3;
    
    ptr1 = urn;      //把第一個位址賦給指針
    ptr2 = &urn[2]; //把一個位址賦給指針
                      //解引用指針,以及獲得指針的位址 
    printf("pointer value, dereferenced pointer, poiner adress:\n");
    printf("ptr1 = %p, *ptr1 = %d, &ptr1 = %p\n", ptr1, *ptr1, &ptr1);
    
    //指針加法
    ptr3 = ptr1 + 4;
    printf("\nadding an int to a pointer:\n");
    printf("ptr1 + 4 = %p, *(ptr1 + 4) = %d\n", ptr1 + 4, *(ptr1 + 4));
    ptr1++;            //遞增指針 
    printf("\nvalue after ptr1++:\n");
    printf("ptr1 = %p, *ptr1 = %d, &ptr1 = %p\n", ptr1, *ptr1, &ptr1);
    ptr2--;            //遞減指針 
    printf("\nvalues after ptr--:\n");
    printf("ptr2 = %p, *ptr2 = %d, &ptr2 = %p\n", ptr2, *ptr2, &ptr2);
    --ptr1;            //恢複為初始值 
    ++ptr2;            //恢複為初始值
    printf("\nPointers reset to original values:\n");
    printf("ptr1 = %p, ptr2 = %p\n", ptr1, ptr2);
    //一個指針減去另一個指針
    printf("\nsubtracting one pointer from another:\n");
    printf("ptr2 = %p, ptr1 = %p, ptr2 - ptr1 = %zd\n", ptr2, ptr1, ptr2 - ptr1);
    //一個指針減去一個整數
    printf("\nsubtracting an int from a pointer:\n");
    printf("ptr3 = %p, ptr3 - 2 = %p\n", ptr3, ptr3 - 2);
    
    return 0;
}
           

輸出結果:

第十章 數組和指針
  • 程式清單10.14,arf.c:
/* arf.c -- 處理數組的函數 */ 
#include <stdio.h>
#define SIZE 5

void show_array(const double ar[], int n);
void mult_array(double ar[], int n, double mult); // 将數組所有值乘以mult 

int main(void)
{
    double dip[SIZE] = { 20.0, 17.66, 8.2, 15.3, 22.22 };
    
    printf("The original dip array:\n");
    show_array(dip, SIZE);
    mult_array(dip, SIZE, 2.5);
    printf("The dip array after calling mult_array():\n");
    show_array(dip, SIZE);
    
    return 0;
}

void show_array(const double ar[], int n)
{
    int i;
    
    for (i = 0; i < n; i++)
    {
        printf("%8.3f ", ar[i]);
        //printf("%8.3f ", *(ar + i));
    }
    putchar('\n');
}

void mult_array(double ar[], int n, double mult)
{
    int i;
    
    for (i = 0; i < n; i++)
        ar[i] *= mult;
}
           

輸出結果:

第十章 數組和指針
  • 程式清單10.15,zippol.c:
/* zippol.c -- zippo 的相關資訊 */ 
#include <stdio.h>

int main(void)
{
    int zippo[4][2] = { { 2, 4 }, { 6, 8 }, { 1, 3 }, { 5, 7 } };
    printf("   zippo = %p,     zippo + 1 = %p\n", zippo, zippo + 1);
    printf("zippo[0] = %p,    zippo[0] + 1 = %p\n", zippo[0], zippo[0] + 1);
    printf("  *zippo = %p,    *zippo + 1 = %p\n", *zippo, *zippo + 1);
    printf("zippo[0][0] = %d\n", zippo[0][0]);
    printf("  *zippo[0] = %d\n", *zippo[0]);
    printf("  **zippo = %d\n", **zippo);
    printf("     zippo[2][1] = %d\n", zippo[2][1]);
    printf("*(*(zippo + 2) + 1) = %d\n", *(*(zippo + 2) + 1));
    
    return 0;
}
           

輸出結果:

第十章 數組和指針
  • 程式清單10.16,zippo2.c:
/* zippo2.c -- 通過指針擷取zippo 的資訊 */ 
#include <stdio.h>

int main(void)
{
    int zippo[4][2] = { { 2, 4 }, { 6, 8 }, { 1, 3 }, { 5, 7 } };
    int (*pz) [2]; // 數組的指針 
    pz = zippo;
    
    printf("&zippo = %p\n", &zippo);
    printf("&zippo[0] = %p\n", &zippo[0]);
    printf("   zippo = %p,     zippo + 1 = %p\n", zippo, zippo + 1);
    printf("zippo[0] = %p,    zippo[0] + 1 = %p\n", zippo[0], zippo[0] + 1);
    printf("  *zippo = %p,    *zippo + 1 = %p\n", *zippo, *zippo + 1);
    printf("zippo[0][0] = %d\n", zippo[0][0]);
    printf("  *zippo[0] = %d\n", *zippo[0]);
    printf("  **zippo = %d\n", **zippo);
    printf("     zippo[2][1] = %d\n", zippo[2][1]);
    printf("*(*(zippo + 2) + 1) = %d\n\n\n", *(*(zippo + 2) + 1));
    
    printf("&pz = %p\n", &pz);
    printf("   pz = %p,     pz + 1 = %p\n", pz, pz + 1);
    printf("pz[0] = %p,    pz[0] + 1 = %p\n", pz[0], pz[0] + 1);
    printf("  *pz = %p,    *pz + 1 = %p\n", *pz, *pz + 1);
    printf("pz[0][0] = %d\n", pz[0][0]);
    printf("  *pz[0] = %d\n", *pz[0]);
    printf("  **pz = %d\n", **pz);
    printf("     pz[2][1] = %d\n", pz[2][1]);
    printf("*(*(pz + 2) + 1) = %d\n", *(*(pz + 2) + 1));
    
    return 0;
}
           

輸出結果:

第十章 數組和指針
  • 程式清單10.17,array2d.c:
// array2d.c -- 處理二維數組的函數 
#include <stdio.h>
#define ROWS 3
#define COLS 4

void sum_rows(int ar[][COLS], int rows);
void sum_cols(int [][COLS], int);      // 省略形參名 
int sum2d(int (*ar)[COLS], int rows);  // 另一種文法 

int main(void)
{
    int junk[ROWS][COLS] = {
            { 2, 4, 6, 8 },
            { 3, 5, 7, 9 },
            { 12, 10, 8, 6}
    };
    
    sum_rows(junk, ROWS);
    sum_cols(junk, ROWS);
    printf("Sum of all elements = %d\n", sum2d(junk, ROWS));
    
    return 0;
}

void sum_rows(int ar[][COLS], int rows)
{
    int r;
    int c;
    int tot;
    
    for (r = 0; r < rows; r++)
    {
        tot = 0;
        for (c = 0; c < COLS; c++)
            tot += ar[r][c];
        printf("row %d: sum = %d\n", r, tot);
    }
}

void sum_cols(int ar[][COLS], int rows)
{
    int r;
    int c;
    int tot;
    
    for (c = 0; c < COLS; c++)
    {
        tot = 0;
        for (r = 0; r < rows; r++)
            tot += ar[r][c];
        printf("col %d: sum = %d\n", c, tot);
    }
}

int sum2d(int ar[][COLS], int rows)
{
    int r;
    int c;
    int tot = 0;
    
    for (r = 0; r < rows; r++)
        for (c = 0; c < COLS; c++)
            tot += ar[r][c];
    
    return tot;
}
           

輸出結果:

第十章 數組和指針
  • 程式清單10.18,vararr2d.c:
// vararr2d.c -- 使用變長數組的函數 
#include <stdio.h>
#define ROWS 3
#define COLS 4

int sum2d(int rows, int cols, int ar[rows][cols]);

int main(void)
{
    int i, j;
    int rs = 3;
    int cs = 10;
    int junk[ROWS][COLS] = {
            { 2, 4, 6, 8 },
            { 3, 5, 7, 9 },
            { 12, 10, 8, 6}
    };
    
    int morejunk[ROWS - 1][COLS + 2] = {
        { 20, 30, 40, 50, 60, 70 },
        { 5, 6, 7, 8, 9, 10 }
    };
    
    int varr[rs][cs];  // 變長數組(VLA) 
    
    for (i = 0; i < rs; i++)
        for (j = 0; j <cs; j++)
                varr[i][j] = i * j + j;
    
    printf("3x5 array\n");
    printf("Sum of all elements = %d\n", sum2d(ROWS, COLS, junk));
    
    printf("2x6 array\n");
    printf("Sum of all elements = %d\n", sum2d(ROWS - 1, COLS + 2, morejunk));
    
    printf("3x10 array\n");
    printf("Sum of all elements = %d\n", sum2d(rs, cs, varr));
    
    return 0;
}

// 帶變長數組形參的函數 
int sum2d(int rows, int cols, int ar[rows][cols])
{
    int r;
    int c;
    int tot = 0;
    
    for (r = 0; r < rows; r++)
        for (c = 0; c < cols; c++)
            tot += ar[r][c];
    
    return tot;
}
           

輸出結果:

第十章 數組和指針
  • 程式清單10.19,flc.c:
// flc.c -- 有趣的常量 
#include <stdio.h>
#define COLS 4

int sum2d(const int ar[][COLS], int rows);
int sum(const int ar[], int n);

int main(void)
{
    int total1, total2, total3;
    int * pt1;
    const int (*pt2)[COLS];
    
    pt1 = (int[2]) { 10, 20 };
    pt2 = (const int[2][COLS]) { { 1, 2, 3, -9 }, { 4, 5, 6, -8 } };
    
    total1 = sum(pt1, 2);
    total2 = sum2d(pt2, 2);
    total3 = sum((int []){ 4, 4, 4, 5, 5, 5}, 6);
    printf("total1 = %d\n", total1);
    printf("totll2 = %d\n", total2);
    printf("total3 = %d\n", total3);
    
    return 0;
}

int sum(const int ar [], int n)
{
    int i;
    int total = 0;
    
    for (i = 0; i < n; i++)
        total += ar[i];
        
    return total;
}

int sum2d(const int ar [][COLS], int rows)
{
    int r;
    int c;
    int tot = 0;
    
    for (r = 0; r < rows; r++)
        for (c = 0; c < COLS; c++)
            tot += ar[r][c];
    
    return tot;
}
           

輸出結果:

第十章 數組和指針
  • 程式設計練習
第十章 數組和指針
第十章 數組和指針
第十章 數組和指針

題目1,方法:用指針計算rain.c,示例代碼10_1.c:

#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;
    
    printf(" YEAR          RAINFALL (inches)\n");
    for (year = 0, total = 0; year < YEARS; year++)
    {
        for (month = 0, subtot = 0; month < MONTHS; month++)
            subtot += *(*(rain + year) + month);
        printf("%5d %15.1f\n", 2010 + 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 += *(*(rain + year) + month);
        printf("%4.1f ", subtot / YEARS);
    }
    printf("\n");
    
    return 0;
}
           

輸出結果:

第十章 數組和指針

題目2,方法:用不同的方法拷貝數組。示例代碼10_2.c:

#include <stdio.h>

void copy_arr(double ar[], const double original[], int n);
void copy_ptr(double * ar, const double * original, int n);
void copy_ptrs(double * ar, const double * original, const double * end);
void show_arr(const double [], int n);

int main(void)
{
    double source[5] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
    double target1[5];
    double target2[5];
    double target3[5];
    
    printf("Source array:\n");
    show_arr(source, 5);
    printf("Target1 array:\n");
    copy_arr(target1, source, 5);
    show_arr(target1, 5);
    printf("Target2 array:\n");
    copy_ptr(target2, source, 5);
    show_arr(target2, 5);
    printf("Target3 array:\n");
    copy_ptrs(target3, source, source + 5);
    show_arr(target3, 5);
    
    return 0;
}

void copy_arr(double ar[], const double original[], int n)
{
    int index;
    
    for (index = 0; index < n; index++)
        ar[index] = original[index];
}

void copy_ptr(double * ar, const double * original, int n)
{
    int index;
    
    for (index = 0; index < n; index++)
        *(ar + index) = *(original + index);
}

void copy_ptrs(double * ar, const double * original, const double * end)
{
    int i;
    
    for (i = 0; i  + original < end; i++)
        *(ar + i) = *(original + i);
}

void show_arr(const double ar[], int n)
{
    int index;
    
    for (index = 0; index < n; index++)
        printf("%-4g", ar[index]);
    putchar('\n');
}
           

輸出結果:

第十章 數組和指針

題目3,方法:求一個int類型數組中的最大值。示例代碼10_3.c:

#include <stdio.h>

int int_max(int * ar, int n);

int main(void)
{
    int a[] = { 1, 3, 3, 4, 9 };
    int n;
    
    n = sizeof a / sizeof a[0];
    printf("The array has %d of elements.\n", n);
    printf("The max num in the array is:%d\n", int_max(a, n));
    printf("&a = %p\n", &a);
    printf("a = %p\n", a);
    
    return 0;
}

int int_max(int * ar, int n)
{
    int index, max;
    
    printf("&ar = %p\n", &ar);
    printf("ar = %p\n", ar);
    max = ar[0];
    for (index = 0; index < n; index++)
    {
        if (ar[index] > max)
            max = ar[index]; 
    }
    
    return max;
}
           

輸出結果:

第十章 數組和指針

題目4,方法:求出數組中最大值的下标。示例代碼10_4.c:

#include <stdio.h>

int subscript(double *, int n);

int main(void)
{
    int num;
    double a[] = { 1.1, 3.4, 5.3, 2.1 };
    
    num = sizeof a / sizeof a[0];
    printf("The subscript of the max number of the array a is %d\n", subscript(a, num));
    
    return 0;
}

int subscript(double * ar, int n)
{
    int i, subid;
    double max;
    
    max = *ar;
    for (i = 0; i < n; i++)
    {
        if (*(ar + i) > max)
        {
            max = *(ar + i);
            subid = i;
        }
    }
    
    return subid;
}
           

輸出結果:

第十章 數組和指針

題目5,方法:計算數組中最大值和最小值的內插補點。示例代碼10_5.c:

#include <stdio.h>

double diference(double *, int n);

int main(void)
{
    int num;
    double a[] = { 1.1, 3.4, 5.3, 0.5 };
    
    num = sizeof a / sizeof a[0];
    printf("The diference of the max number and min number in the array a is %g\n", diference(a, num));
    
    return 0;
}

double diference(double * ar, int n)
{
    int i;
    double max, min;
    
    max = min = *ar;
    for (i = 0; i < n; i++)
    {
        if (*(ar + i) > max)
            max = *(ar + i);
        if (*(ar + i) < min)
            min = *(ar + i);
    }
    
    return max - min;
}
           

輸出結果:

第十章 數組和指針

題目6,方法:倒序排列數組元素。示例代碼10_6.c:

#include <stdio.h>

void reverse(double *, int n);

int main(void)
{
    int num, i;
    double a[] = { 1.1, 2.2, 3.3, 4.4 };
    
    num = sizeof a / sizeof a[0];
    reverse(a, num);
    printf("After reverse the array is:\n");
    for (i = 0; i < num; i++)
        printf("%g ", *(a + i));
    
    return 0;
}

void reverse(double * ar, int n)
{
    double item;
    int i;
    
    for (i = 0; i < n / 2; i++)
    {
        item = *(ar + i);
        *(ar + i) = *(ar + n - 1 - i);
        *(ar + n - 1 - i) = item;
    }
}
           

輸出結果:

第十章 數組和指針

題目7,方法:拷貝二維數組。示例代碼10_7.c:

#include <stdio.h>
#define ROWS 3
#define COLS 4

void copy_ptr(double * ar, const double * original, int n);
void show_arr(int m, int n, double (*)[n]);

int main(void)
{
    double source[ROWS][COLS] = {
            { 2.3, 4.5, 6.7, 8.6 },
            { 3.3, 5.2, 7.1, 9.9 },
            { 12.3, 10.0, 8.4, 6.5}
    };
    double target[ROWS][COLS];
    int i;
    
    for (i = 0; i < ROWS; i++)
        copy_ptr(*(target + i), *(source + i), COLS);
    printf("After copy,target is:\n");
    show_arr(ROWS, COLS, target);
    
    return 0;
}

void copy_ptr(double * ar, const double * original, int n)
{
    int index;
    
    for (index = 0; index < n; index++)
        *(ar + index) = *(original + index);
}

void show_arr(int m, int n, double (*ptr)[n])
{
    int i, j;
    
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
            printf("%g ", *(*(ptr + i) + j));
        putchar('\n');
    }
}
           

輸出結果:

第十章 數組和指針

題目8,方法:拷貝數字到數組中。示例代碼10_8.c:

#include <stdio.h>

void copy_arr(double ar[], const double original[], int n);
void show_arr(const double [], int n);

int main(void)
{
    double a[] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };
    double b[3];
    
    copy_arr(b, a + 2, 3);
    printf("After copy, b is:\n");
    show_arr(b, 3);
    
    return 0;
}

void copy_arr(double * ar, const double * original, int n)
{
    int index;
    
    for (index = 0; index < n; index++)
        *(ar + index) = *(original + index);
}

void show_arr(const double ar[], int n)
{
    int index;
    
    for (index = 0; index < n; index++)
        printf("%g ", ar[index]);
    putchar('\n');
}
           

輸出結果:

第十章 數組和指針

題目9,方法:用變長數組拷貝二維數組。示例代碼10_9.c:

#include <stdio.h>

void copy2d(int m, int n, const double (*)[n], double (*)[n]);
void show2d(int m, int n, const double [][n]);

int main(void)
{
    const double source[][5] = { 
            {1.1, 2.2, 3.3, 4.4, 5.5 },
            {5.5, 4.4, 3.3, 2.2, 1.1 },
            {1.1, 2.2, 3.3, 4.4, 5.5 }
    };
    double target[3][5];
    printf("The source array is:\n");
    show2d(3, 5, source);
    printf("The target array is:\n");
    copy2d(3, 5, source, target);
    show2d(3, 5, target);
    
    return 0;
}

void copy2d(int m, int n, const double (* ar)[n], double (* tr)[n])
{
    int i, j;
    
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
            *(*(tr + i) + j) = *(*(ar + i) + j);
    }
}

void show2d(int m, int n, const double ar[][n])
{
    int i, j;
    
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
            printf("%g ", ar[i][j]);
        putchar('\n');
    }
}
           

輸出結果:

第十章 數組和指針

題目10,方法:将兩個數組對應元素之和相加并存儲到第三個數組中。示例代碼10_10.c:

#include <stdio.h>

void add2_ar(const int *, const int *, int [], int n);
void show_ar(const int *, int n);

int main(void)
{
    const int a[] = { 2, 4, 5, 8 };
    const int b[] = { 1, 0, 4, 6 };
    int c[4];
    
    printf("The array a is:\n");
    show_ar(a, 4);
    printf("The array b is:\n");
    show_ar(b, 4);
    printf("The array c is:\n");
    add2_ar(a, b, c, 4);
    show_ar(c, 4);
    
    return 0;
}

void add2_ar(const int * ar, const int * br, int c[], int n)
{
    int index;
    
    for (index = 0; index < n; index++)
        c[index] = *(ar + index) + *(br + index);
}

void show_ar(const int * ar, int n)
{
    int i;
    
    for (i = 0; i < n; i++)
        printf("%3d", *(ar + i));
    putchar('\n');
}
           

輸出結果:

第十章 數組和指針

題目11,方法:将二維數組中的元素的值翻倍。示例代碼10_11.c:

#include <stdio.h>

void times2(int m, int n, int (*)[n]);
void show2d(int m, int n, int (*)[n]);

int main(void)
{
    int a[][5] = {
        { 1, 2, 3, 4, 5 },
        { 5, 4, 3, 2, 1 },
        { 1, 2, 3, 4, 5 }
    };
    
    printf("Before times:\n");
    show2d(3, 5, a);
    printf("After times:\n");
    times2(3, 5, a);
    show2d(3, 5, a);
    
    return 0;
}

void times2(int m, int n, int(* ar)[n])
{
    int i, j;
    
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
            *(*(ar + i) + j) *= 2;
    }
}

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

輸出結果:

第十章 數組和指針

題目12,方法:用函數改寫程式清單10.7.示例代碼10_12.c:

#include <stdio.h>
#define MONTHS 12
#define YEARS 5

float get_year(int m, int n, const float (*)[n]);
void get_month(int m, int n, const float (*)[n]);

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 }    
    };
    
    printf(" YEAR          RAINFALL (inches)\n");
    printf("\nThe yearly average is %.1f inches.\n\n", get_year(YEARS, MONTHS, rain) / YEARS);
    get_month(YEARS, MONTHS, rain);
    
    return 0;
}

float get_year(int m, int n, const float (* ar)[n])
{
    int i, j;
    float subtot, total;
    
    for (i = 0, total = 0; i < m; i++)
    {
        for (j = 0, subtot = 0; j < n; j++)
            subtot += *(*(ar + i) + j);
        printf("%5d %15.1f\n", 2010 + i, subtot);
        total += subtot;
    }
    
    return total;
}

void get_month(int m, int n, const float (* ar)[n])
{
    int i, j;
    float subtot;
    
    printf("MONTHLY AVERAGES:\n\n");
    printf(" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct ");
    printf(" Nov  Dec\n");
    for (i = 0; i < n; i++)
    {
        for (j = 0, subtot = 0; j < m; j++)
            subtot += *(*(ar + j) + i);
        printf("%4.1f ", subtot / YEARS);
    }
    putchar('\n');
}
           

輸出結果:

第十章 數組和指針

題目13,14,方法:按要求完成計算(已使用變長數組)。示例代碼10_13.c:

#include <stdio.h>
#define ROWS 3
#define COLS 5

void show2d(int m, int n, double [][n]);
void function(int m, int n, double (*)[n]);

int main(void)
{
    double a[ROWS][COLS] = { { 0.0 } };
    int i, j;
    
    for (i = 0; i < ROWS; i++)
    {
        printf("Please input %dth of 5 double numbers: ", i + 1);
        for (j = 0; j < COLS; j++)
            scanf("%lf", *(a + i) + j);    
    }
    printf("The array of a is:\n");
    show2d(ROWS, COLS, a);
    function(ROWS, COLS, a);    
    
    return 0;
}

void show2d(int m, int n, double ar[][n])
{
    int i, j;
    
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
            printf("%g ", ar[i][j]);
        putchar('\n');
    }
}

void function(int m, int n, double (* ar)[n])
{
    double average_5, average_all, max;
    int i, j;
    
    max = **ar;
    for (i = 0, average_all = 0; i < m; i++)
    {
        for (j = 0, average_5 = 0; j < n; j++)
        {
            average_5 += *(*(ar + i) + j);
            if (ar[i][j] > max)
                max = ar[i][j];
        }
        average_all += average_5;
        printf("The average of %dth of 5 numbers is %g\n", i + 1, average_5 / COLS);
    }
    printf("The average of the array is %g\n", average_all / (ROWS * COLS));
    printf("The maximum of the array is %g\n", max);
}
           

輸出結果:

第十章 數組和指針

繼續閱讀