|
|
|
|
|
|
![]() Chapter 16Answers to Selected Exercises2. [was #2; modified] (a)
struct {
double real, imaginary;
} c1, c2, c3;
(b)
struct {
double real, imaginary;
} c1 = {0.0, 1.0}, c2 = {1.0, 0.0}, c3;
(c) Only one statement is necessary: c1 = c2; (d) c3.real = c1.real + c2.real; c3.imaginary = c1.imaginary + c2.imaginary; 4. [was #4; modified] (a)
typedef struct {
double real, imaginary;
} Complex;
(b) (c)
Complex make_complex(double real, double imaginary)
{
Complex c;
c.real = real;
c.imaginary = imaginary;
return c;
}
(d)
Complex add_complex(Complex c1, Complex c2)
{
Complex c3;
c3.real = c1.real + c2.real;
c3.imaginary = c1.imaginary + c2.imaginary;
return c3;
}
11. [was #10; modified] The 14. [was #12; modified] (a)
double area(struct shape s)
{
if (s.shape_kind == RECTANGLE)
return s.u.rectangle.height * s.u.rectangle.width;
else
return 3.14159 * s.u.circle.radius * s.u.circle.radius;
}
(b)
struct shape move(struct shape s, int x, int y)
{
struct shape new_shape = s;
new_shape.center.x += x;
new_shape.center.y += y;
return new_shape;
}
(c)
struct shape scale(struct shape s, double c)
{
struct shape new_shape = s;
if (new_shape.shape_kind == RECTANGLE) {
new_shape.u.rectangle.height *= c;
new_shape.u.rectangle.width *= c;
} else
new_shape.u.circle.radius *= c;
return new_shape;
}
15. [was #14]
(a)
17. [was #16] All the statements are legal, since C allows integers and enumeration values to be mixed
without restriction. Only (a), (d), and (e) are safe. (b) is not meaningful if Answers to Selected Programming Projects1. [was #6; modified]
#include <stdio.h>
#define COUNTRY_COUNT \
((int) (sizeof(country_codes) / sizeof(country_codes[0])))
struct dialing_code {
char *country;
int code;
};
const struct dialing_code country_codes[] =
{{"Argentina", 54}, {"Bangladesh", 880},
{"Brazil", 55}, {"Burma (Myanmar)", 95},
{"China", 86}, {"Colombia", 57},
{"Congo, Dem. Rep. of", 243}, {"Egypt", 20},
{"Ethiopia", 251}, {"France", 33},
{"Germany", 49}, {"India", 91},
{"Indonesia", 62}, {"Iran", 98},
{"Italy", 39}, {"Japan", 81},
{"Mexico", 52}, {"Nigeria", 234},
{"Pakistan", 92}, {"Philippines", 63},
{"Poland", 48}, {"Russia", 7},
{"South Africa", 27}, {"South Korea", 82},
{"Spain", 34}, {"Sudan", 249},
{"Thailand", 66}, {"Turkey", 90},
{"Ukraine", 380}, {"United Kingdom", 44},
{"United States", 1}, {"Vietnam", 84}};
int main(void)
{
int code, i;
printf("Enter dialing code: ");
scanf("%d", &code);
for (i = 0; i < COUNTRY_COUNT; i++)
if (code == country_codes[i].code) {
printf("The country with dialing code %d is %s\n",
code, country_codes[i].country);
return 0;
}
printf("No corresponding country found\n");
return 0;
}
3. [was #8]
#include <stdio.h>
#include "readline.h"
#define NAME_LEN 25
#define MAX_PARTS 100
struct part {
int number;
char name[NAME_LEN+1];
int on_hand;
};
int find_part(int number, const struct part inv[], int np);
void insert(struct part inv[], int *np);
void search(const struct part inv[], int np);
void update(struct part inv[], int np);
void print(const struct part inv[], int np);
/**********************************************************
* main: Prompts the user to enter an operation code, *
* then calls a function to perform the requested *
* action. Repeats until the user enters the *
* command 'q'. Prints an error message if the user *
* enters an illegal code. *
**********************************************************/
int main(void)
{
char code;
struct part inventory[MAX_PARTS];
int num_parts = 0;
for (;;) {
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != '\n') /* skips to end of line */
;
switch (code) {
case 'i': insert(inventory, &num_parts);
break;
case 's': search(inventory, num_parts);
break;
case 'u': update(inventory, num_parts);
break;
case 'p': print(inventory, num_parts);
break;
case 'q': return 0;
default: printf("Illegal code\n");
}
printf("\n");
}
}
/**********************************************************
* find_part: Looks up a part number in the inv array. *
* Returns the array index if the part number *
* is found; otherwise, returns -1. *
**********************************************************/
int find_part(int number, const struct part inv[], int np)
{
int i;
for (i = 0; i < np; i++)
if (inv[i].number == number)
return i;
return -1;
}
/**********************************************************
* insert: Prompts the user for information about a new *
* part and then inserts the part into the inv *
* array. Prints an error message and returns *
* prematurely if the part already exists or the *
* array is full. *
**********************************************************/
void insert(struct part inv[], int *np)
{
int part_number;
if (*np == MAX_PARTS) {
printf("Database is full; can't add more parts.\n");
return;
}
printf("Enter part number: ");
scanf("%d", &part_number);
if (find_part(part_number, inv, *np) >= 0) {
printf("Part already exists.\n");
return;
}
inv[*np].number = part_number;
printf("Enter part name: ");
read_line(inv[*np].name, NAME_LEN);
printf("Enter quantity on hand: ");
scanf("%d", &inv[*np].on_hand);
(*np)++;
}
/**********************************************************
* search: Prompts the user to enter a part number, then *
* looks up the part in the inv array. If the *
* part exists, prints the name and quantity on *
* hand; if not, prints an error message. *
**********************************************************/
void search(const struct part inv[], int np)
{
int i, number;
printf("Enter part number: ");
scanf("%d", &number);
i = find_part(number, inv, np);
if (i >= 0) {
printf("Part name: %s\n", inv[i].name);
printf("Quantity on hand: %d\n", inv[i].on_hand);
} else
printf("Part not found.\n");
}
/**********************************************************
* update: Prompts the user to enter a part number. *
* Prints an error message if the part can't be *
* found in the inv array; otherwise, prompts the *
* user to enter change in quantity on hand and *
* updates the array. *
**********************************************************/
void update(struct part inv[], int np)
{
int i, number, change;
printf("Enter part number: ");
scanf("%d", &number);
i = find_part(number, inv, np);
if (i >= 0) {
printf("Enter change in quantity on hand: ");
scanf("%d", &change);
inv[i].on_hand += change;
} else
printf("Part not found.\n");
}
/**********************************************************
* print: Prints a listing of all parts in the inv array, *
* showing the part number, part name, and *
* quantity on hand. Parts are printed in the *
* order in which they were entered into the *
* array. *
**********************************************************/
void print(const struct part inv[], int np)
{
int i;
printf("Part Number Part Name "
"Quantity on Hand\n");
for (i = 0; i < np; i++)
printf("%7d %-25s%11d\n", inv[i].number,
inv[i].name, inv[i].on_hand);
}
Copyright © 2008, 1996 W. W. Norton & Company, Inc. All rights reserved. |