C Primer Plus Sixth Edition Programming Exercise
Selected Answers
ret_val = fgets(st, n, stdin); if (ret_val) {
find = strchr(st, '\\n'); // look for newline if (find) // if the address is not NULL, *find = '\\0'; // place a null character there else
while (getchar() != '\\n') continue; }
return ret_val; }
PE 11---12
/* pe11-12.c -- counts words and certain characters */ /* Programming Exercise 11-11 */ #include
#include
#include
char c; // read in character int
low_ct = 0; // number of lowercase characters
int up_ct = 0; // number of uppercase characters int dig_ct = 0; // number of digits int n_words = 0; // number of words
int punc_ct = 0; // number of punctuation marks bool inword = false; // == true if c is in a word
printf(\while ((c = getchar()) != EOF)
{ if (islower(c)) low_ct++; else if
(isupper(c)) up_ct++; else if (isdigit(c)) dig_ct++; else if (ispunct(c))
punc_ct++; if (!isspace(c) && !inword) {
inword = true; // starting a new word n_words++; // count word }
if (isspace(c) && inword)
inword = false; // reached end of word }
printf(\ \n_words,low_ct,up_ct, dig_ct, punc_ct); return 0; }
PE 11---14
/* Programming Exercise 11-14 */ #include
#include
26
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
{
double num, exp; if (argc != 3)
printf(\else { num = atof(argv[1]); exp = atof(argv[2]);
printf(\ } return 0; }
PE 11---16
/* Programming Exercise 11-16 */ #include
int main(int argc, char *argv[])
{ char mode =
'p'; int ok = 1; int ch;
if (argc > 2) {
printf(\ok = 0; /* skip processing input */ }
else if (argc == 2) {
if (argv[1][0] != '-') {
printf(\ok = 0; } else switch(argv[1][1])
{ case
'p' : case 'u' :
case 'l' : mode = argv[1][1]; break;
default : printf(\printf(\ } } if (ok)
while ((ch = getchar() ) != EOF) {
switch(mode) {
case 'p' : putchar(ch); break;
case 'u' : putchar(toupper(ch)); break;
case 'l' : putchar(tolower(ch)); } }
return 0; }
27
Chapter 12
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
Programming Exercises
PE 12---1
/* pe12-1.c -- deglobalizing global.c */ /* Programming Exercise 12-1 */ /* one of several approaches */ #include
int units; /* units now local */
printf(\butter?\\n\( units != 56) critic(&units);
printf(\return 0; }
void critic(int * u) {
printf(\scanf(\}
// or use a return value: // units = critic();
// and have critic look like this: /* int
critic(void) { int u;
printf(\scanf(\} */
// or have main() collect the next value for units
PE 12---3
//pe12-3a.h
#define METRIC 0 #define US 1
#define USE_RECENT 2
void check_mode(int *pm);
void get_info(int mode, double * pd, double * pf); void show_info(int mode, double distance, double fuel);
// pe12-3a.c
// compile with pe12-3b.c #include
28
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
void check_mode(int *pm) {
if (*pm != METRIC && *pm != US)
{ printf(\*pm); printf(\ *pm = USE_RECENT; } }
void get_info(int mode, double * pd, double * pf) {
if (mode == METRIC)
printf(\else
printf(\scanf(\
printf(\else
printf(\scanf(\}
void show_info(int mode, double distance, double fuel) {
printf(\if (mode == METRIC)
printf(\else
printf(\}
// pe12-3b.c
// compile with pe12-3a.c #include
#include \main(void)
{ int mode; int prev_mode = METRIC; double distance, fuel;
printf(\scanf(\
{ check_mode(&mode); if (mode ==
USE_RECENT) mode = prev_mode; prev_mode = mode; get_info(mode, &distance, &fuel); show_info(mode, distance, fuel); printf(\for metric mode, 1 for US mode\quit): \ }
printf(\ return 0; }
PE 12---5
/* pe12-5.c */
#include
void print(const int array[], int limit); void sort(int array[], int limit);
29
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
#define SIZE 100 int main(void)
{ int i; int arr[SIZE];
for (i = 0; i < SIZE; i++) arr[i] = rand() % 10 + 1; puts(\print(arr,SIZE); sort(arr,SIZE);
puts(\print(arr,SIZE); return 0; }
/* sort.c -- sorts an integer array in decreasing order */ void sort(int array[], int limit) {
int top, search, temp;
for (top = 0; top < limit -1; top++)
for (search = top + 1; search < limit; search++) if (array[search] > array[top]) {
temp = array[search]; array[search] = array[top]; array[top] = temp; } }
/* print.c -- prints an array */ void print(const int array[], int limit) {
int index;
for (index = 0; index < limit; index++) {
printf(\if (index % 10 == 9) putchar('\\n'); }
if (index % 10 != 0) // if last line not complete putchar('\\n'); }
PE 12---7
/* pe12-7.c */ #include
#include
#include
int main(void) {
int dice, count, roll; int sides; int set, sets;
30