C Primer Plus Sixth Edition Programming Exercise
Selected Answers
if (limit > 1)
printf(\else
printf(\for (num = 2; num <= limit; num++) {
for (div = 2, numIsPrime = true; (div * div) <= num; div++) if (num % div == 0) numIsPrime = false; if (numIsPrime)
printf(\ }
printf(\ }
printf(\return 0; }
PE 7---11
/* pe7-11.c */
/* Programming Exercise 7-11 */ #include
#include
const double price_artichokes = 2.05; const double price_beets = 1.15; const double price_carrots = 1.09; const double DISCOUNT_RATE = 0.05; const double under5 = 6.50; const double under20 = 14.00; const double base20 = 14.00; const double extralb = 0.50; char ch;
double lb_artichokes = 0; double lb_beets = 0; double lb_carrots = 0; double lb_temp; double lb_total; double
cost_artichokes; double cost_beets; double cost_carrots; double cost_total; double final_total; double discount; double shipping;
printf(\printf(\getchar()) != 'q' && ch != 'Q') { if (ch == '\\n') continue; while
(getchar() != '\\n') continue; ch =
tolower(ch); switch (ch) {
case 'a' : printf(\scanf(\+= lb_temp; break;
11
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
case 'b' : printf(\scanf(\+= lb_temp; break;
case 'c' : printf(\
carrots: \lb_carrots += lb_temp; break;
default : printf(\ }
printf(\printf(\ }
cost_artichokes = price_artichokes * lb_artichokes; cost_beets = price_beets * lb_beets; cost_carrots =
price_carrots * lb_carrots; cost_total = cost_artichokes + cost_beets + cost_carrots; lb_total = lb_artichokes + lb_beets + lb_carrots; if (lb_total <= 0) shipping = 0.0; else if (lb_total < 5.0) shipping = under5; else if (lb_total < 20) shipping = under20; else shipping = base20 + extralb * lb_total; if (cost_total > 100.0)
discount = DISCOUNT_RATE * cost_total; else discount = 0.0;
final_total = cost_total + shipping - discount; printf(\
printf(\lb_artichokes, price_artichokes, cost_artichokes);
printf(\lb_beets, price_beets, cost_beets); printf(\carrots at $%.2f per pound: $%.2f\\n\price_carrots, cost_carrots); printf(\
vegetables: $%.2f\\n\ printf(\printf(\
printf(\return 0; }
Chapter 8 Programming Exercises
PE 8---1
/* Programming Exercise 8-1 */ #include
int main(void) { int ch; int ct = 0; while ((ch = getchar()) != EOF) ct++;
printf(\ return 0; }
PE 8---3
/* Programming Exercise 8-3 */
/* Using ctype.h eliminates need to assume consecutive coding */ #include
#include
main(void) { int ch;
12
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
unsigned long uct = 0; unsigned long lct = 0; unsigned long oct = 0;
while ((ch = getchar()) != EOF) if (isupper(ch)) uct++; else if (islower(ch)) lct++; else oct++;
printf(\
printf(\other characters read\\n\ return 0; }
/* or you could use if
(ch >= 'A' && ch <= 'Z') uct++;
else if (ch >= 'a' && ch <= 'z') lct++; else oct++; */
PE 8---5
/* Programming Exercise 8-5 */
/* binaryguess.c -- an improved number-guesser */ /* but relies upon truthful, correct responses */ #include
{ int high = 100; int low = 1; int guess = (high + low) / 2; char response;
printf(\
printf(\h if it is high, and with an l if it is low.\\n\number %d?\\n\
while ((response = getchar()) != 'y') /* get response */ {
if (response == '\\n') continue;
if (response != 'h' && response != 'l') {
printf(\printf(\ }
if (response == 'h') high = guess - 1; else if (response == 'l') low = guess + 1; guess = (high + low) / 2;
printf(\ }
printf(\return 0; }
PE 8---7
/* Programming Exercise 8-7 */
13
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
#include
#define BASEPAY1 8.75 // $8.75 per hour #define BASEPAY2 9.33 // $9.33 per hour #define BASEPAY3 10.00 // $10.00 per hour #define BASEPAY4 11.20 // $11.20 per hour #define BASEHRS 40 // hours at basepay #define OVERTIME 1.5 // 1.5 time
#define AMT1 300 // 1st rate tier #define AMT2 150 // 2st rate tier
#define RATE1 0.15 // rate for 1st tier #define RATE2 0.20 // rate for 2nd tier #define RATE3 0.25 // rate for 3rd tier int getfirst(void); void menu(void); int main(void) { double
hours; double gross; double net; double taxes; double pay; char response; menu();
while ((response = getfirst()) != 'q') {
if (response == '\\n') /* skip over newlines */ continue;
response = tolower(response); /* accept A as a, etc. */ switch (response) {
case 'a': pay = BASEPAY1; break; case 'b': pay = BASEPAY2; break; case 'c': pay = BASEPAY3; break; case 'd': pay = BASEPAY4; break;
default : printf(\menu();
continue; // go to beginning of loop }
printf(\scanf(\gross = hours * pay; else
gross = BASEHRS * pay + (hours - BASEHRS) * pay * OVERTIME; if (gross <= AMT1) taxes = gross * RATE1; else if (gross <= AMT1 + AMT2)
taxes = AMT1 * RATE1 + (gross - AMT1) * RATE2; else
taxes = AMT1 * RATE1 + AMT2 * RATE2 + (gross - AMT1 - AMT2) * RATE3; net = gross - taxes;
printf(\net); menu(); } printf(\ return 0; }
void menu(void) {
printf(\ \
14
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
printf(\ \
printf(\BASEPAY2);
printf(\BASEPAY4); printf(\
printf(\ \}
int getfirst(void) { int ch;
ch = getchar(); while (isspace(ch)) ch = getchar(); while (getchar() != '\\n') continue; return ch; }
Chapter 9 Programming Exercises
PE 9---1
/* Programming Exercise 9-1 */ #include
double min(double, double); int main(void) {
double x, y; printf(\numbers (q to quit): \(scanf(\
{ printf(\
min(x,y)); printf(\\ }
printf(\ return 0; }
double min(double a, double b) {
return a < b ? a : b; }
/* alternative implementation double min(double a, double b) { if (a < b) return a; else return b; } */
15