好文档 - 专业文书写作范文服务资料分享网站

《C Primer Plus》第六版 第十二章编程练习答案

天下 分享 时间: 加入收藏 我要投稿 点赞

printf(\ }

getchar(); return 0; }

//*********fun.h***********// #include #include

int rand0(void); void sort(int *str);

//************fun.c***************// #include #include\

int rand0(void) {

intnum;

num=rand()+1; returnnum; }

void sort(int *str) {

inti, j, temp;

for (i=0; i<100; i++) {

for (j=0; j<100-i-1; j++) {

if (str[j]

temp=str[j]; str[j]=str[j+1]; str[j+1]=temp; } } } }

6.

#include #include

int rand0();

int main(void) {

intStr[10]={0}; int count; int value;

for(count=0; count<1000; count++) {

value=rand0(); Str[value-1]++; }

for (count=0; count<10; count++) {

printf(\, Str[count]); }

getchar(); return 0; }

int rand0() {

intnum;

num=rand()+1; returnnum; }

7.

//****************main.c*************// #include #include #include #include\

int main(void) {

int dice, roll; int sides, sets; int status; inti;

srand( (unsignedint) time(0) );

printf(\); while(scanf(\, &sets)==1 && sets>0) {

if (sets=='q') {

break; }

printf(\); if(scanf(\, &sides)==1 && sides>0) {

printf(\);

if( (status =scanf(\, &dice))!=1) {

if (status==EOF) {

break; } else {

printf(\); printf(\); while(getchar()!='\\n') {

continue; }

printf(\); continue; } }

printf(\, sets, dice, sides); for (i=0; i

roll=roll_n_dice(dice, sides); printf(\, roll ); }

putchar('\\n');

printf(\); } }

printf(\, roll_count); printf(\); getchar(); return 0; }

//**************diceroll.h**************// externintroll_count;

introll_n_dice(int dice, int sides);

//*************diceroll.c**************// #include\ #include #include

introll_count=0;

staticintrollem(intsides) {

int roll;

roll=rand()%sides+1; roll_count++;

return roll; }

introll_n_dice(intdice, intsides) {

int d;

int total=0; if(sides<2) {

printf(\); return -2; }

if (dice<1) {

}

printf(\); return -1; }

for (d=0; d

total +=rollem(sides); }

return total;

8.

#include #include

int *make_array(intelem, intval); voidshow_array(constintar[], int n); int main(void) {

int *pa; int size; int value;

printf(\); while(scanf(\, &size)==1 && size>0) {

printf(\); scanf(\, &value);

pa=make_array(size, value); if (pa) {

show_array(pa, size); free(pa); }

printf(\); }

printf(\); getchar(); return 0;

《C Primer Plus》第六版 第十二章编程练习答案

printf(\}getchar();return0;}//*********fun.h***********//#include#includeintrand0(void);voidsort(int*str);//************fun.c***
推荐度:
点击下载文档文档为doc格式
6i33s0zkr75136q5t3t485bn78arf200cjq
领取福利

微信扫码领取福利

微信扫码分享