C Primer Plus 第6版 编程练习——第10章(上)

本章共12踢,分上下两篇。

1.修改程序清单10.7的rain.c程序,用指针进行计算(仍然要声明并初始化数组)。

cpp 复制代码
#define MONTHS 12 //一年的月份数
#define YEARS  5  //年数
int main(void)
{
    //用2010~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 }
    };
    float subtot = 0.0, total = 0.0;
    printf(" Year RAINFALL(inches)\n");
    for (int year = 0; year < YEARS; year++)
    {
        subtot = 0.0;
        for(int month = 0; month < MONTHS; month++)
            subtot += *(*(rain + year) + month);
        printf("%5d %15.1f\n", year + 2010, 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 (int month = 0; month < MONTHS; month++)
    {
        subtot = 0.0;
        for (int year = 0; year < YEARS; year++)
        {
            subtot += *(*(rain + year) + month);
        }
        printf(" % 4.1f ", subtot / YEARS);
    }
    printf("\n");
    return 0;
}

2.编写一个程序,初始化一个double数组,然后把该数组的内容拷贝至3个其他数组中(在main()中声明这4个数组)。使用带数组表示发的函数进行第1份拷贝。使用带指针表示法和指针递增的函数进行第2份拷贝。把目标数组名、源数组名和待拷贝的元素个数作为前两个函数的参数。第3个函数以目标数组名、源数组名和指向源数组最后一个元素后面的元素指针。也就是说,给定以下声明,则函数调用如下所示:

double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};

double target1[5];

double target2[5];

double target3[5];

copy_arr(target1, source, 5);

copy_ptr(target2, source, 5);

copy_ptrs(target3, source, source + 5);

cpp 复制代码
void copy_arr(double target[], double source[], int size)
{
    for (int i = 0; i < size; i++)
    {
        target[i] = source[i];
    }
}
void copy_ptr(double* target, double* source, int size)
{
    for (int i = 0; i < size; i++)
    {
        *target++ = *source++;
    }
}
void copy_ptrs(double* target, double* source, double* end)
{
    for (double *p = source; source < end; source++)
    {
        *target++ = *source;
    }
}
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];
    copy_arr(target1, source, 5);
    copy_ptr(target2, source, 5);
    copy_ptrs(target3, source, source + 5);
    for (int i = 0; i < 5; i++)
    {
        printf("%f %f %f\n", target1[i], target2[i], target3[i]);
    }

    return 0;
}

3.编写一个函数,返回存储在int类型数组中的最大值,并在一个简单的程序中测试该函数。

cpp 复制代码
int max_array(int a[], int n)
{
    int max = a[0];
    for (int i = 1; i < n; i++)
    {
        if (a[i] > max)
        {
            max = a[i];
        }
    }
    return max;
}
int main()
{
    int a[] = { 1, 2, 3, 7, 8, 9, 10, 4, 5, 6 };
    printf("max:%d\n", max_array(a, 10));
}

4.编写一个函数,返回存储在double类型数组中最大值的下标,并在一个简单的程序中测试该函数。

cpp 复制代码
int max_array(double a[], int n)
{
    int max_idx = 0;
    for (int i = 1; i < n; i++)
    {
        if (a[i] > a[max_idx])
        {
            max_idx = i;
        }
    }
    return max_idx;
}
int main()
{
    double a[10] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1 };
    printf("The index of max value is %d\n", max_array(a, 10));
}

5.编写一个函数,返回存储在double数组中最大值和最小值的差,并在一个简单的程序中测试该函数。

cpp 复制代码
double max_min_diff(double a[], int n)
{
    double max = a[0];
    double min = a[0];
    for (int i = 1; i < n; i++)
    {
        if (a[i] > max)
        {
            max = a[i];
        }
        else if (a[i] < min)
        {
            min = a[i];
        }
    }
    return max - min;
}
int main()
{
    double a[10] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1 };
    printf("max - min : %.2f", max_min_diff(a, 10));
}

6.编写一个程序,把double类型数组中的数据倒叙排列,并在一个简单的程序中测试该函数。

cpp 复制代码
void DescSort(double a[], int n)
{ 
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            if (a[i] < a[j])
            {
                double temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }
}
int main()
{
    double a[5] = { 2.2, 1.1, 4.4, 5.5, 3.3 };
    DescSort(a, 5);
    for (int i = 0; i < 5; i++)
    {
        printf("%f ", a[i]);
    }
    printf("\n");
}
相关推荐
艾莉丝努力练剑12 小时前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
Cx330❀13 小时前
【数据结构初阶】--排序(五):计数排序,排序算法复杂度对比和稳定性分析
c语言·数据结构·经验分享·笔记·算法·排序算法
..过云雨14 小时前
01.【数据结构-C语言】数据结构概念&算法效率(时间复杂度和空间复杂度)
c语言·数据结构·笔记·学习
谱写秋天16 小时前
在STM32F103上进行FreeRTOS移植和配置(STM32CubeIDE)
c语言·stm32·单片机·freertos
我不是板神16 小时前
程序设计|C语言教学——C语言基础2:计算与控制语句
c语言
基于python的毕设16 小时前
C语言栈的实现
linux·c语言·ubuntu
promising-w20 小时前
【嵌入式C语言】六
c语言·开发语言
ankleless21 小时前
C语言(11)—— 数组(超绝详细总结)
c语言·零基础·数组·二维数组·自学·一维数组
草莓熊Lotso1 天前
《吃透 C++ 类和对象(中):const 成员函数与取地址运算符重载解析》
c语言·开发语言·c++·笔记·其他
野生的编程萌新1 天前
从冒泡到快速排序:探索经典排序算法的奥秘(二)
c语言·开发语言·数据结构·c++·算法·排序算法