C Primer Plus Sixth Edition Programming Exercise
Selected Answers
Chapter 2 Programming Exercises
PE 2---1
/* Programming Exercise 2-1 */ #include
{ printf(\Mahler\\n\
printf(\printf(\
printf(\0; }
PE 2---3
/* Programming Exercise 2-3 */ #include
{ int ageyears; /* age in years */ int agedays; /* age in days */
/* large ages may require the long type */ ageyears = 101; agedays = 365 * ageyears;
printf(\return 0; }
PE 2---4
/* Programming Exercise 2-4 */ #include
jolly(); jolly(); deny(); return 0; }
void jolly(void) {
printf(\}
void deny(void) {
printf(\}
PE 2---6
/* Programming Exercise 2-6 */ #include
toes; toes = 10;
1
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
printf(\
printf(\
printf(\0; }
/* or create two more variables, set them to 2 * toes and toes * toes */
PE 2---8
/* Programming Exercise 2-8 */ #include
printf(\one_three();
printf(\0; }
void one_three(void) {
printf(\two();
printf(\}
void two(void) {
printf(\}
Chapter 3 Programming Exercises
PE 3---2
/* Programming Exercise 3-2 */ #include
int ascii;
printf(\scanf(\
printf(\return 0; }
PE 3---4
/* Programming Exercise 3-4 */ #include
printf(\scanf(\
printf(\printf(\printf(\}
2
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
PE 3---6
/* Programming Exercise 3-6 */ #include
float mass_mol = 3.0e-23; /* mass of water molecule in grams */ float mass_qt = 950; /* mass of quart of water in grams */ float quarts; float molecules;
printf(\scanf(\
molecules = quarts * mass_qt / mass_mol;
printf(\return 0; }
Chapter 4 Programming Exercises
PE 4---1
/* Programming Exercise 4-1 */ #include
fname[40]; char lname[40];
printf(\scanf(\your last name: \lname); printf(\fname); return 0; }
PE 4---4
/* Programming Exercise 4-4 */ #include
height; char name[40];
printf(\scanf(\name: \
printf(\ return 0; }
PE 4---7
/* Programming Exercise 4-7 */ #include
#include
3
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
{ float ot_f = 1.0 /
3.0; double ot_d = 1.0 / 3.0; printf(\values: \
printf(\printf(\
printf(\printf(\
printf(\}
Chapter 5 Programming Exercises
PE 5---1
/* Programming Exercise 5-1 */ #include
{ const int minperhour = 60; int minutes, hours, mins;
printf(\scanf(\ { hours = minutes /
minperhour; mins = minutes % minperhour;
printf(\printf(\&minutes); }
printf(\ return 0; }
PE 5---3
/* Programming Exercise 5-3 */ #include
{ const int daysperweek = 7; int days, weeks, day_rem;
printf(\scanf(\ { weeks = days /
daysperweek; day_rem = days % daysperweek;
printf(\days, weeks, day_rem);
printf(\scanf(\ }
4
C Primer Plus Sixth Edition Programming Exercise
Selected Answers
printf(\return 0; }
PE 5---5
/* Programming Exercise 5-5 */ #include
int main(void) /* finds sum of first n integers */ {
int count, sum; int n;
printf(\
scanf(\sum = 0; while (count++ < n) sum = sum +
count; printf(\return 0; }
PE 5---7
/* Programming Exercise 5-7 */ #include
int main(void) /* finds cube of entered number */ { double val;
printf(\scanf(\return 0; }
void showCube(double x) {
printf(\}
Chapter 6 Programming Exercises
PE 6---1
/* pe6-1.c */
/* this implementation assumes the character codes */ /* are sequential, as they are in ASCII. */ #include
PE 6---3
/* pe6-3.c */
5