Chapter 2 Programming Exercises
PE 2--‐1
/* Programming Exercise 2-1 */ #include
{ printf(\printf(\printf(\
printf(\}
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(\}
PE 2--‐4
/* Programming Exercise 2-4 */ #include
{ jolly(); jolly(); jolly(); deny(); return 0; }
void jolly(void) {
printf(\}
void deny(void) {
printf(\}
PE 2--‐6
/* Programming Exercise 2-6 */ #include
{ int toes; toes = 10;
printf(\
printf(\squared = %d\\n\}
/* or create two more variables, set them to 2 * toes and toes * toes */
PE 2--‐8
/* Programming Exercise 2-8 */ #include
two(void); int main(void) {
printf(\
one_three(); printf(\return 0; }
void one_three(void) {
printf(\ printf(\}
void two(void) {
printf(\}
Chapter 3
Programming Exercises
PE 3--‐2
/* Programming Exercise 3-2 */ #include
int ascii;
printf(\&ascii);
printf(\}
PE 3--‐4
/* Programming Exercise 3-4 */ #include
{ float num;
printf(\ printf(\printf(\printf(\
}
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(\&quarts);
molecules = quarts * mass_qt / mass_mol;
printf(\}
Chapter 4
Programming Exercises
PE 4--‐1
/* Programming Exercise 4-1 */ #include
{ char fname[40]; char lname[40];
printf(\scanf(\last name: \printf(\0; }
PE 4--‐4
/* Programming Exercise 4-4 */ #include
{ float height; char name[40];
printf(\scanf(\\
printf(\ return 0; }
PE 4--‐7
/* Programming Exercise 4-7 */ #include
#include
{ float ot_f = 1.0 / 3.0; double ot_d = 1.0 / 3.0; printf(\
printf(\\
printf(\
printf(\DBL_DIG); return 0; }
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(\ }
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(\&days); }
printf(\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(\= %d\\n\}
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. */