C Primer Plus 第六版 第12章 编程答案
1.第1题
#include
void critic(int * n);
int main(void)
{
int units = 0;
printf("How many pounds to a firkin of butter?\n");
scanf("%d", &units);
while (units != 56)
critic(&units);
printf("You must have looked it up!\n");
return 0;
}
void critic(int * n)
{
printf("No luck, my friend. Try again.\n");
scanf("%d", n);
}
2.第2题
#include
#include "book22_.h"
int main(void)
{
int mode;
printf("Enter 0 for metric mode, 1 for US mode: ");
scanf("%d", &mode);
while (mode >= 0)
{
get_mode(mode);
get_info();
show_info();
printf("Enter 0 for metric mode, 1 for US mode: ");
printf(" (-1 to quit): ");
scanf("%d", &mode);
}
printf("Done.\n");
return 0;
}
3.第3题
#include
#include "book23_.h"
int main(void)
{
int mode;
printf("Enter 0 for metric mode, 1 for US mode: ");
scanf("%d", &mode);
while (mode >= 0)
{
get_mode(mode);
get_info(mode);
show_info(mode);
printf("Enter 0 for metric mode, 1 for US mode: ");
printf(" (-1 to quit): ");
scanf("%d", &mode);
}
printf("Done.\n");
return 0;
}
4.第4题
#include
int xun(void);
int main(void)
{
int i;
for (i = 0; i < 100; i++)
{
xun();
}
printf("%d\n", xun());
return 0;
}
int xun(void)
{
static count = 0;
count++;
return count;
}
5.第5题
#include
#include
#include
#define SIZE 100
int main(void)
{
int st[SIZE];
int i, j, temp;
srand(time(0));
for (i = 0; i < SIZE; i++)
{
st[i] = rand() % 10 + 1;
}
for (i = 0; i < SIZE - 1; i++)
for (j = i + 1; j < SIZE; j++)
if (st[i] < st[j])
{
temp = st[i];
st[i] = st[j];
st[j] = temp;
}
for (i = 0; i < SIZE; i++)
{
printf("%5d", st[i]);
if (i % 10 == 9)
putchar('\n');
}
return 0;
}
6.第6题
#include
#include
#include
#define SIZE 100
int main(void)
{
int l1,l2,l3,l4,l5,l6,l7,l8,l9,l10;
l1=l2=l3=l4=l5=l6=l7=l8=l9=l10=0;
int i,j;
int st;
for (i = 0; i < 10; i++)
{
srand(100+i);
for (j = 0; j < SIZE; j++)
{
st = rand() % 10 + 1;
if (st == 1) l1++;
if (st == 2) l2++;
if (st == 3) l3++;
if (st == 4) l4++;
if (st == 5) l5++;
if (st == 6) l6++;
if (st == 7) l7++;
if (st == 8) l8++;
if (st == 9) l9++;
if (st == 10) l10++;
}
printf("第%d次\n",i+1);
puts(" 1 2 3 4 5 6 7 8 9 10 ");
printf("%5d%5d%5d%5d%5d%5d%5d%5d%5d%5d\n",l1,l2,l3,l4,l5,l6,l7,l8,l9,l10);
l1=l2=l3=l4=l5=l6=l7=l8=l9=l10=0;
}
return 0;
}
7.第7题
#include
#include
#include
int rollem(int);
int main(void)
{
int dice, count, roll;
int sides;
int set, sets;
srand((unsigned int) time(0));
printf("Enter the number of sets; enter q to stop: ");
while (scanf("%d", &sets) == 1)
{
printf("How many sides and how many dice? ");
if (scanf("%d %d", &sides, &dice) != 2)
{
puts("not integers -- terminating input loop.");
break;
}
printf("Here are %d sets of %d %d-sided throws.\n", sets, dice, sides);
for (set = 0; set < sets; set++)
{
for (roll = 0, count = 0; count < dice; count++)
roll += rollem(sides);
printf("%4d ", roll);
if (set % 15 == 14)
putchar('\n');
}
if (set % 15 != 0)
putchar('\n');
printf("How many sets? Enter q to stop: ");
}
puts("GOOD FORTUNE TO YOU!\n");
return 0;
}
int rollem(int sides)
{
int roll;
roll = rand() % sides + 1;
return roll;
}
8.第8题
#include
#include
int * make_array(int elem, int val);
void show_array(const int ar [], int n);
int main(void)
{
int * pa;
int size;
int value;
printf("Enter the number of elements: ");
while (scanf("%d", &size) == 1 && size > 0)
{
printf("Enter the initialization value: ");
scanf("%d", &value);
pa = make_array(size, value);
if (pa)
{
show_array(pa, size);
free(pa);
}
printf("Enter the number of elements (<1 to quit): ");
}
printf("Done.\n");
return 0;
}
int * make_array(int elem, int val)
{
int i;
int *pa = (int *)malloc(sizeof(int)*elem);
for (i = 0; i < elem; i++)
pa[i] = val;
return pa;
}
void show_array(const int ar [], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%5d",*(ar+i));
if (i % 8 == 7) putchar('\n');
}
if (i % 8 != 0) putchar('\n');
}
9.第9题
#include
#include
#include
int main(void)
{
char ** ch;
char * c;
int words;
int i;
int len;
printf("How many words do you wish to enter? ");
scanf("%d", &words);
ch = (char **)malloc(sizeof(char *) * words);
printf("Enter %d words now: \n", words);
for (i = 0; i < words; i++)
{
c = (char *)malloc(sizeof(char));
scanf("%s", c);
len = strlen(c);
ch[i] = (char *)malloc(sizeof(char) * len + 1);
strcpy(ch[i],c);
}
free(c);
for (i = 0; i < words; i++)
{
puts(ch[i]);
free(ch[i]);
}
free(ch);
return 0;
}