Chapter 2 Programming Exercises
PE 2--‐1
/* Programming Exercise 2-1 */ #include <> int main(void)
{ printf(\printf(\printf(\
printf(\0; }
PE 2--‐3
/* Programming Exercise 2-3 */ #include <> int main(void)
{ 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 <> void jolly(void); void deny(void); int main(void)
{ jolly(); jolly(); jolly(); deny(); return 0; }
void jolly(void) {
printf(\}
void deny(void) {
printf(\}
PE 2--‐6
/* Programming Exercise 2-6 */ #include <> int main(void)
{ int toes; toes = 10;
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 <> void
one_three(void); void two(void); int main(void) {
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 main(void) {
int ascii;
printf(\scanf(\
printf(\return 0; }
PE 3--‐4
/* Programming Exercise 3-4 */ #include <> int main(void) { float num;
printf(\scanf(\
printf(\printf(\printf(\}
PE 3--‐6
/* Programming Exercise 3-6 */ #include <> int main(void) {
float mass_mol = ; /* 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 <> int main(void) { char
fname[40]; char lname[40];
printf(\scanf(\your last name: \lname); printf(\fname); return 0; }
PE 4--‐4
/* Programming Exercise 4-4 */ #include <> int main(void)
{ float height; char name[40];
printf(\scanf(\name: \
printf(\ return 0; }
PE 4--‐7
/* Programming Exercise 4-7 */ #include <> #include <> int main(void)
{ float ot_f = / ; double ot_d = / ;
printf(\
printf(\printf(\
printf(\printf(\
printf(\}
Chapter 5 Programming Exercises
PE 5--‐1
/* Programming Exercise 5-1 */ #include <> int main(void)
{ 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 <> int main(void)
{ 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(\ }
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(\0; }
PE 5--‐7
/* Programming Exercise 5-7 */ #include <> void
showCube(double x);
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
/* */
/* this implementation assumes the character codes */ /* are sequential, as they are in ASCII. */ #include <> #define SIZE 26 int main( void ) { char
lcase[SIZE]; int i; for (i = 0; i < SIZE; i++) lcase[i] = 'a' + i; for (i = 0; i < SIZE; i++) printf(\printf(\return 0; }
PE 6--‐3
/* */
/* this implementation assumes the character codes */ /* are sequential, as they are in ASCII. */