C Primer Plus第十六章编程练习答案

这篇具有很好参考价值的文章主要介绍了C Primer Plus第十六章编程练习答案。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

学完C语言之后,我就去阅读《C Primer Plus》这本经典的C语言书籍,对每一章的编程练习题都做了相关的解答,仅仅代表着我个人的解答思路,如有错误,请各位大佬帮忙点出!

由于使用的是命令行参数常用于linux系统或者vscode,但此代码是运行于vs2022的,测试截图就不弄了。

2.两数的调和平均数这样计算:先得到两数的倒数,然后计算两个倒数 的平均值,最后取计算结果的倒数。使用#define指令定义一个宏“函数”,执 行该运算。编写一个简单的程序测试该宏。

#include <stdio.h>
#define HMEAN(X, Y) (2.0 * (X) * (Y) / ((X) + (Y)))
int main(void)
{
    double x, y, ans;

    printf("Enter a pair of numbers (q to quit): ");
    while (scanf("%lf %lf", &x, &y) == 2)
    {
        ans = HMEAN(x, y);
        printf("%g = harmonic mean of %g %g.\n", ans, x, y);
        printf("Enter a pair of numbers (q to quit): ");
    }
    printf("Done.\n");

    return 0;
}

C Primer Plus第十六章编程练习答案

3.极坐标用向量的模(即向量的长度)和向量相对x轴逆时针旋转的角 度来描述该向量。直角坐标用向量的x轴和y轴的坐标来描述该向量(见图 16.3)。编写一个程序,读取向量的模和角度(单位:度),然后显示x轴 和y轴的坐标。相关方程如下:

x = r*cos A y = r*sin A

需要一个函数来完成转换,该函数接受一个包含极坐标的结构,并返回 一个包含直角坐标的结构(或返回指向该结构的指针)。

C Primer Plus第十六章编程练习答案

#include <stdio.h>
#include <math.h>
#define PI 3.1415926
typedef struct
{
    double length;
    double angle;
} polar;
typedef struct
{
    double x;
    double y;
} rect;
rect polar_to_rect(const polar* temp)
{
    rect res;
    static const double rad = PI / 180.0;
    double ang = rad * temp->angle;

    res.x = temp->length * cos(ang);
    res.y = temp->length * sin(ang);

    return res;
}
int main(void)
{
    polar input;
    rect answer;

    printf("Enter magnitude and angle in degrees (q tu quit): ");
    while (scanf("%lf %lf", &input.length, &input.angle) == 2)
    {
        answer = polar_to_rect(&input);
        printf("polar coord: %g %g\n", input.length, input.angle);
        printf("rectangular coord: %g %g\n", answer.x, answer.y);
        printf("You can enter again (q tu quit): ");
    }
    printf("Done.\n");

    return 0;
}

C Primer Plus第十六章编程练习答案

4.ANSI库这样描述clock()函数的特性:

#include <time.h>

clock_t clock (void);

这里,clock_t是定义在time.h中的类型。该函数返回处理器时间,其单 位取决于实现(如果处理器时间不可用或无法表示,该函数将返回-1)。然 而,CLOCKS_PER_SEC(也定义在time.h中)是每秒处理器时间单位的数 量。因此,两个 clock()返回值的差值除以 CLOCKS_PER_SEC得到两次调用 之间经过的秒数。在进行除法运算之前,把值的类型强制转换成double类 型,可以将时间精确到小数点以后。编写一个函数,接受一个double类型的 参数表示时间延迟数,然后在这段时间运行一个循环。编写一个简单的程序 测试该函数。

#include <stdio.h>
#include <time.h>
void delay(const double second)
{
    clock_t start = clock();
    clock_t end = clock();

    while (((double)(end - start) / CLOCKS_PER_SEC) < second)
    {
        end = clock();
    }
    printf("Delay %g seconds.\n", (double)(end - start) / CLOCKS_PER_SEC);
}
int main(void)
{
    double n;

    printf("please enter a number (<0 or q to quit): ");
    while (scanf("%lf", &n) == 1)
    {
        delay(n);
        printf("You can enter again (<0 or q to quit): ");
    }
    printf("Done.\n");

    return 0;
}

C Primer Plus第十六章编程练习答案

5.编写一个函数接受这些参数:内含int类型元素的数组名、数组的大小 和一个代表选取次数的值。该函数从数组中随机选择指定数量的元素,并打 印它们。每个元素只能选择一次(模拟抽奖数字或挑选陪审团成员)。另 外,如果你的实现有time()(第12章讨论过)或类似的函数,可在srand()中 使用这个函数的输出来初始化随机数生成器rand()。编写一个简单的程序测 试该函数。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define LEN 30
#define PICK 6
void random_pick(int ar[],int picks)
{
    int count = 0;
    int i, br[LEN];

    memcpy(br, ar, LEN * sizeof(int));
    srand((unsigned int)time(0));
    printf("Pick %d numbers:\n", picks);
    while (picks > 0)
    {
        i = rand() % LEN;
        if (0 == br[i])
        {
            continue;
        }
        else
        {
            printf("%-8d", br[i]);
            br[i] = 0;
            --picks;
        }
        if (++count % 10 == 0)
        {
            putchar('\n');
        }
    }
    printf("\n");
}
int main(void)
{
    int i, ch;
    int choices[LEN];

    for (i = 0; i < LEN; i++)
    {
        choices[i] = i + 1;
    }
    do
    {
        random_pick(choices, PICK);
        printf("Can you do again (y/n)? ");
        ch = getchar();
        while (getchar() != '\n')
            continue;
    } while ('y' == ch || 'Y' == ch);
    printf("Done.\n");

    return 0;
}

C Primer Plus第十六章编程练习答案

6.修改程序清单16.17,使用struct names元素(在程序清单16.17后面的 讨论中定义过),而不是double类型的数组。使用较少的元素,并用选定的 名字显式初始化数组。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 40
#define SLEN 5
struct names
{
    char first[LEN];
    char last[LEN];
};
int comp(const void* p1, const void* p2)
{
    const struct names* ps1 = (const struct names*)p1;
    const struct names* ps2 = (const struct names*)p2;

    if (strcmp(ps1->last, ps2->last) != 0)
    {
        return 0;
    }
    else
    {
        return strcmp(ps1->first, ps2->first);
    }
}
void show_names(const struct names* begin, int n)
{
    const struct names* end = begin + n;

    while (begin < end)
    {
        printf("%s %s\n", begin->first, begin->last);
        ++begin;
    }
    return;
}
int main(void)
{
    struct names staff[SLEN] ={{"Francy, card"},{"Coffee, cancy"},
                            {"Stephen, lory"},{"Jack, rosery"},{"Black, clover"}};
    printf("Random list:\n");
    show_names(staff, SLEN);
    qsort(staff, SLEN, sizeof(struct names), comp);
    printf("\nSorted list:\n");
    show_names(staff, SLEN);

    return 0;
}

C Primer Plus第十六章编程练习答案

7.下面是使用变参函数的一个程序段:

#include

#include

#include

void show_array(const double ar[], int n);  

double * new_d_array(int n, ...);

int main() {

double * p1;

double * p2;

p1 = new_d_array(5, 1.2, 2.3, 3.4, 4.5, 5.6);

p2 = new_d_array(4, 100.0, 20.00, 8.08, -1890.0);

show_array(p1, 5);

show_array(p2, 4);

free(p1);

free(p2);

return 0; }

new_d_array()函数接受一个int类型的参数和double类型的参数。该函数 返回一个指针,指向由malloc()分配的内存块。int类型的参数指定了动态数 组中的元素个数,double类型的值用于初始化元素(第1个值赋给第1个元 素,以此类推)。编写show_array()和new_d_array()函数的代码,完成这个 程序。

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
void show_array(const double ar[], int n)
{
    int i;

    printf("%d elements:\n", n);
    for (i = 0; i < n; i++)
    {
        printf("%-8g", ar[i]);
    }
    printf("\n");
}
double* new_d_array(int n, ...)
{
    int i;
    va_list ap;
    double* pt;

    va_start(ap, n);
    pt = (double*)malloc(n * sizeof(double));
    for (i = 0; i < n; i++)
    {
        pt[i] = va_arg(ap, double);
    }
    va_end(ap);
    return pt;
}
int main()
{
    double* p1;
    double* p2;

    p1 = new_d_array(5, 1.2, 2.3, 3.4, 4.5, 5.6);
    p2 = new_d_array(4, 100.0, 20.00, 8.08, -1890.0);
    show_array(p1, 5);
    show_array(p2, 4);
    free(p1);
    free(p2);

    return 0;
}

C Primer Plus第十六章编程练习答案文章来源地址https://www.toymoban.com/news/detail-471370.html

到了这里,关于C Primer Plus第十六章编程练习答案的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • C++ Primer Plus第二章编程练习答案

    答案仅供参考,实际运行效果取决于运行平台和运行软件 1.编写一个C++程序,它显示您的姓名和地址。 2.编写一个C程序它要求用户输入一个以 long 为单位的距离,然后将它转换为码(- ng等于220码) 3.编写1个C++程序它使用3个用户定义的函数(括mai()),并生成下面的输出Three blind

    2024年02月09日
    浏览(44)
  • C++ primer plus第七章编程练习答案

    1.编写一个程序,不断要求用户输入两个数,直到其中的一个为 0。对于每两个数,程序将使用一个南数来计算它们的调和平均数,并将结果返回给 main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下: 调和平均数=2.0*xy/(x + y) 2.编写一个程序,要求用

    2024年02月10日
    浏览(32)
  • C Primer Plus第九章编程练习答案

    学完C语言之后,我就去阅读《C Primer Plus》这本经典的C语言书籍,对每一章的编程练习题都做了相关的解答,仅仅代表着我个人的解答思路,如有错误,请各位大佬帮忙点出! 1.设计一个函数min(x, y),返回两个double类型值的较小值。在一个简单 的驱动程序中测试该函数。 2

    2024年02月06日
    浏览(28)
  • C++ Primer Plus第五章编程练习答案

    答案仅供参考,实际运行效果取决于运行平台和运行软件 1.编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是2和则程序将出29之间所有整数的和为44 2.使用array对

    2024年02月09日
    浏览(38)
  • C Primer Plus (中文版)第10章编程练习 参考答案(仅供参考~)

    🌴 C Primer Plus第10章编程练习~ 加油加油!🍭 ☘️欢迎大家讨论 批评指正~ 🍎1.修改程序清单10.7的rain.c程序,用指针进行计算(仍然要声明并初始化数组)。计算每年的总降水量、年平均降水量和5年中每月的平均降水量 🍐编写一个程序,初始化一个double类型的数组,然后把

    2024年02月04日
    浏览(40)
  • 《TCP IP网络编程》第十六章

              「分离 I/O 流」是一种常用表达。有 I/O 工具可区分二者,无论采用哪种方法,都可以认为是分离了 I/O 流。 2次 I/O 流分离: 第一种是第 10 章的「TCP I/O 过程」分离。通 过调用 fork 函数复制出一个文件描述符,以区分输入和输出中使用的文件描述符。虽然文件描

    2024年02月13日
    浏览(32)
  • C Primer Plus 第6版 编程练习 chapter 16

    开发一个包含你需要的预处理定义的头文件。 test.c diceroll.h 两个数的调和平均数这样计算:先得到两数的倒数,然后计算两个倒数的平均值,最后取计算结果的倒数。使用#define指令定义一个宏函数,,执行该运算。编写一个简单的程序测试该宏。 极坐标向量的模(即向量的

    2024年01月19日
    浏览(32)
  • C Primer Plus(第六版)12.9 编程练习 第5题

    #include stdlib.h   #include stdio.h   #include time.h   #define TIMES 100 void bubble_sort(int arr[], int len); int main (void) {     int i;     int roll[TIMES];     srand((unsigned int)time (NULL));     for(i=0;iTIMES;i++)     {         roll[i] = rand()%10+1 ;         printf(\\\"roll[%d]=%dn\\\",i,roll[i]);     }     bubble_s

    2024年01月20日
    浏览(33)
  • 【Linux命令行与Shell脚本编程】第十六章 Shell函数

    脚本函数基础 函数返回值 在函数中使用变量 数组变量和函数 函数递归 创建库 在命令行中使用函数 可以将shell脚本代码放入函数中封装起来,这样就能在脚本的任意位置多次使用. 函数是一个脚本代码块,可以并在脚本中的任何位置重用它。当需要在脚本中使用该代码块时

    2024年02月14日
    浏览(41)
  • C Primer Plus(第六版)13.11 编程练习 第7题

    #include stdio.h #include stdlib.h   #include string.h  #define LEN 40 //abc-1.txt abc-2.txt int main() {     FILE  *in, *in1;        int ch1,ch2,i;     char name[LEN];     char name1[LEN];          fprintf(stdout, \\\"input your filename,name1 name2:\\\");     scanf(\\\"%s %s\\\",name,name1) ;     if ((in = fopen(name, \\\"r\\\")) == NULL)     {    

    2024年01月23日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包