Vous êtes sur la page 1sur 40

chtp6_03_IM.

fm Page 54 Thursday, November 12, 2009 6:19 PM

3 Structured Program
Development in C: Solutions

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 55 Thursday, November 12, 2009 6:19 PM

Self-Review Exercises 55

Self-Review Exercises
3.1 Fill in the blanks in each of the following questions.
a) A procedure for solving a problem in terms of the actions to be executed and the order
in which the actions should be executed is called a(n) .
ANS: algorithm.
b) Specifying the execution order of statements by the computer is called .
ANS: program control.
c) All programs can be written in terms of three types of control statements: ,
and .
ANS: sequence, selection, repetition.
d) The selection statement is used to execute one action when a condition is
true and another action when that condition is false.
ANS: if...else.
e) Several statements grouped together in braces ({ and }) are called a(n) .
ANS: compound statement.
f) The repetition statement specifies that a statement or group of statements
is to be executed repeatedly while some condition remains true.
ANS: while.
g) Repetition of a set of instructions a specific number of times is called rep-
etition.
ANS: counter-controlled.
h) When it is not known in advance how many times a set of statements will be repeated,
a(n) value can be used to terminate the repetition.
ANS: sentinel.
3.2 Write four different C statements that each add 1 to integer variable x.
ANS: x = x + 1;
x += 1;
++x;
x++;

3.3 Write a single C statement to accomplish each of the following:


a) Assign the sum of x and y to z and increment the value of x by 1 after the calculation.
ANS: z = x++ + y;
b) Multiply the variable product by 2 using the *= operator.
ANS: product *= 2;
c) Multiply the variable product by 2 using the = and * operators.
ANS: product = product * 2;
d) Test if the value of the variable count is greater than 10. If it is, print “Count is greater
than 10.”
ANS: if ( count > 10 )
printf( "Count is greater than 10.\n" );
e) Decrement the variable x by 1, then subtract it from the variable total.
ANS: total -= --x;
f) Add the variable x to the variable total, then decrement x by 1.
ANS: total += x--;
g) Calculate the remainder after q is divided by divisor and assign the result to q. Write
this statement two different ways.
ANS: q %= divisor;
q = q % divisor;
h) Print the value 123.4567 with 2 digits of precision. What value is printed?

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 56 Thursday, November 12, 2009 6:19 PM

56 Chapter 3 Structured Program Development in C: Solutions

ANS: printf( "%.2f", 123.4567 );


123.46 is displayed.
i) Print the floating-point value 3.14159 with three digits to the right of the decimal point.
What value is printed?
ANS: printf( "%.3f\n", 3.14159 );
3.142 is displayed.
3.4 Write a C statement to accomplish each of the following tasks.
a) Define variables sum and x to be of type int.
ANS: int sum, x;
b) Initialize variable x to 1.
ANS: x = 1;
c) Initialize variable sum to 0.
ANS: sum = 0;
d) Add variable x to variable sum and assign the result to variable sum.
ANS: sum += x; or sum = sum + x;
e) Print "The sum is: " followed by the value of variable sum.
ANS: printf( "The sum is: %d\n", sum );
3.5 Combine the statements that you wrote in Exercise 3.4 into a program that calculates the
sum of the integers from 1 to 10. Use the while statement to loop through the calculation and in-
crement statements. The loop should terminate when the value of x becomes 11.
ANS:

1 /* Calculate the sum of the integers from 1 to 10 */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int sum, x; /* define variables sum and x */
7
8 x = 1; /* initialize x */
9 sum = 0; /* initialize sum */
10
11 while ( x <= 10 ) { /* loop while x is less than or equal to 10 */
12 sum += x; /* add x to sum */
13 ++x; /* increment x */
14 } /* end while */
15
16 printf( "The sum is: %d\n", sum ); /* display sum */
17 return 0;
18 } /* end main function */

3.6 Determine the values of variables product and x after the following calculation is per-
formed. Assume that product and x each have the value 5 when the statement begins executing.
product *= x++;

ANS: product = 25, x = 6;


3.7 Write single C statements that
a) Input integer variable x with scanf.
ANS: scanf( "%d", &x );
b) Input integer variable y with scanf.

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 57 Thursday, November 12, 2009 6:19 PM

Self-Review Exercises 57

ANS: scanf( "%d", &y );


c) Initialize integer variable i to 1.
ANS: i = 1;
d) Initialize integer variable power to 1.
ANS: power = 1;
e) Multiply variable power by x and assign the result to power.
ANS: power *= x;
f) Increment variable i by 1.
ANS: i++;
g) Test i to see if it is less than or equal to y in the condition of a while statement.
ANS: while ( i <= y )
h) Output integer variable power with printf.
ANS: printf( "%d", power );
3.8 Write a C program that uses the statements in Exercise 3.7 to calculate x raised to the y pow-
er. The program should have a while repetition control statement.
ANS:

1 /* raise x to the y power */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int x, y, i, power; /* define variables */
7
8 i = 1; /* initialize i */
9 power = 1; /* initialize power */
10 scanf( "%d", &x ); /* read value for x from user */
11 scanf( "%d", &y ); /* read value for y from user */
12
13 while ( i <= y ) { /* loop while i is less than or equal to y */
14 power *= x; /* multiply power by x */
15 ++i; /* increment i */
16 } /* end while */
17
18 printf( "%d", power ); /* display power */
19 return 0;
20 } /* end main function */

3.9 Identify and correct the errors in each of the following:


a) while ( c <= 5 ) {
product *= c;
++c;
ANS: Error: Missing the closing right brace of the while body.
Correction: Add closing right brace after the statement ++c;.
b) scanf( "%.4f", &value );
ANS: Error: Precision used in a scanf conversion specification.
Correction: Remove .4 from the conversion specification.
c) if ( gender == 1 )
printf( "Woman\n" );
else;
printf( "Man\n" );

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 58 Thursday, November 12, 2009 6:19 PM

58 Chapter 3 Structured Program Development in C: Solutions

ANS: Error: Semicolon after the else part of the if…else statement results in a logic error.
The second printf will always be executed.
Correction: Remove the semicolon after else.
3.10 What is wrong with the following while repetition statement (assume z has value 100),
which is supposed to calculate the sum of the integers from 100 down to 1:
while ( z >= 0 )
sum += z;

ANS: The value of the variable z is never changed in the while statement. Therefore, an
infinite loop is created. To prevent the infinite loop, z must be decremented so that
its value eventually becomes less than zero.

Exercises
3.11 Identify and correct the errors in each of the following. [Note: There may be more than one
error in each piece of code.]
a) if ( age >= 65 );
printf( "Age is greater than or equal to 65\n" );
else
printf( "Age is less than 65\n" );
ANS:
if ( age >= 65 ) /* ; removed */
printf( "Age is greater than or equal to 65\n" );
else
printf( "Age is less than 65\n” );
b) int x = 1, total;

while ( x <= 10 ) {
total += x;
++x;
}
ANS:
int x = 1, total = 0; /* total not originally initialized */
while ( x <= 10 ) {
total += x;
++x;
}
c) While ( x <= 100 )
total += x;
++x;
ANS:
while ( x <= 100 ) { /* while changed to lowercase */
total += x;
++x;
} /* } added */
d) while ( y > 0 ) {
printf( "%d\n", y );
++y;
}
ANS:
while ( y > 0 ) {
printf( "%d\n", y );
--y; /* ++ changed to --, loop never ends otherwise */
}

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 59 Thursday, November 12, 2009 6:19 PM

Exercises 59

3.12 Fill in the blanks in each of the following:


a) The solution to any problem involves performing a series of actions in a specific
.
ANS: order.
b) A synonym for procedure is .
ANS: algorithm
c) A variable that accumulates the sum of several numbers is a .
ANS: total.
d) The process of setting certain variables to specific values at the beginning of a program
is called .
ANS: initialization.
e) A special value used to indicate “end of data entry” is called a , a
,a or a value.
ANS: sentinel value, dummy value, signal value, flag value.
f) A is a graphical representation of an algorithm.
ANS: flowchart.
g) In a flowchart, the order in which the steps should be performed is indicated by
symbols.
ANS: arrow (flowline).
h) The termination symbol indicates the and of every algorithm.
ANS: beginning, end.
i) Rectangle symbols correspond to calculations that are normally performed by
statements and input/output operations that are normally performed by
calls to the and standard library functions.
ANS: assignment, printf, scanf.
j) The item written inside a decision symbol is called a .
ANS: condition.
3.13 What does the following program print?

1 #include <stdio.h>
2
3 int main( void )
4 {
5 int x = 1, total = 0, y;
6
7 while ( x <= 10 ) {
8 y = x * x;
9 printf( "%d\n", y );
10 total += y;
11 ++x;
12 }
13
14 printf("Total is %d\n", total);
15 return 0;
16 }

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 60 Thursday, November 12, 2009 6:19 PM

60 Chapter 3 Structured Program Development in C: Solutions

ANS:

1
4
9
16
25
36
49
64
81
100
Total is 385

3.14 Write a single pseudocode statement that indicates each of the following:
a) Display the message "Enter two numbers".
ANS: print “Enter two numbers”
b) Assign the sum of variables x, y, and z to variable p.
ANS: p = x + y + z
c) The following condition is to be tested in an if…else selection statement: The current
value of variable m is greater than twice the current value of variable v.
ANS: if m is greater than twice v
do this ...
else
do this ...
d) Obtain values for variables s, r, and t from the keyboard.
ANS: input s, input r, input t
3.15 Formulate a pseudocode algorithm for each of the following:
a) Obtain two numbers from the keyboard, compute the sum of the numbers and display
the result.
ANS:
Input the first number
Input the second number
Add the two numbers
Output the sum

b) Obtain two numbers from the keyboard, and determine and display which (if either) is
the larger of the two numbers.
ANS:
Input the first number from the keyboard
Input the second number from the keyboard
If the first number is greater than the second number
Print it
Else if the second number is greater than the first number
Print it
Else
Print a message stating that the numbers are equal

c) Obtain a series of positive numbers from the keyboard, and determine and display the
sum of the numbers. Assume that the user types the sentinel value -1 to indicate “end
of data entry.”

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 61 Thursday, November 12, 2009 6:19 PM

Exercises 61

ANS:
Input a value from the keyboard
While the input value is not equal to -1
Add the number to the running total
Input the next number
Print the sum
3.16 State which of the following are true and which are false. If a statement is false, explain why.
a) Experience has shown that the most difficult part of solving a problem on a computer
is producing a working C program.
ANS: False. The algorithm is the hardest part of solving a problem.
b) A sentinel value must be a value that cannot be confused with a legitimate data value.
ANS: True.
c) Flowlines indicate the actions to be performed.
ANS: False. Flowlines indicate the order in which steps are performed.
d) Conditions written inside decision symbols always contain arithmetic operators (i.e., +,
-, *, /, and %).
ANS: False. They normally contain conditional operators.
e) In top-down, stepwise refinement, each refinement is a complete representation of the
algorithm.
ANS: True.

For Exercises 3.17 to 3.21, perform each of these steps:


1. Read the problem statement.
2. Formulate the algorithm using pseudocode and top-down, stepwise refinement.
3. Write a C program.
4. Test, debug and execute the C program.
3.17 Drivers are concerned with the mileage obtained by their automobiles. One driver has kept
track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. De-
velop a program that will input the miles driven and gallons used for each tankful. The program
should calculate and display the miles per gallon obtained for each tankful. After processing all input
information, the program should calculate and print the combined miles per gallon obtained for all
tankfuls. Here is a sample input/output dialog:

Enter the gallons used (-1 to end): 12.8


Enter the miles driven: 287
The miles / gallon for this tank was 22.421875

Enter the gallons used (-1 to end): 10.3


Enter the miles driven: 200
The miles / gallon for this tank was 19.417476

Enter the gallons used (-1 to end): 5


Enter the miles driven: 120
The miles / gallon for this tank was 24.000000

Enter the gallons used (-1 to end): -1

The overall average miles/gallon was 21.601423

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 62 Thursday, November 12, 2009 6:19 PM

62 Chapter 3 Structured Program Development in C: Solutions

ANS:
2)Top:
Determine the average miles/gallon for each tank of gas, and the overall miles/gallon
for an arbitrary number oftanks of gas

First refinement:
Initialize variables

Input the gallons used and the miles driven, and calculate and print the miles/gallon
for each tank of gas. Keep track of the total miles and the total gallons.

Calculate and print the overall average miles/gallon

Second refinement:
Initialize totalGallons to zero
Initialize totalMiles to zero

Input the gallons used for the first tank


While the sentinel value (-1) has not been entered for the gallons
Add gallons to the running total in totalGallons
Input the miles driven for the current tank
Add miles to the running total in totalMiles
Calculate and print the miles/gallon
Input the gallons used for the next tank

Set totalAverage to totalMiles divided by totalGallons


Print the average miles/gallon
3)

1 /* Exercise 3.17 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 float gallons; /* gallons used for current tank*/
7 float miles; /* miles driven for current tank*/
8 float totalGallons = 0.0; /* total gallons used */
9 float totalMiles = 0.0; /* total miles driven */
10 float totalAverage; /* average miles/gallon */
11
12 /* get gallons used for first tank */
13 printf( "Enter the gallons used ( -1 to end): " );
14 scanf( "%f", &gallons );
15
16 /* loop until sentinel value read from user */
17 while ( gallons != -1.0 ) {
18 totalGallons += gallons; /* add current tank gallons to total */
19
20 printf( "Enter the miles driven: " ); /* get miles driven */
21 scanf( "%f", &miles );
22 totalMiles += miles; /* add current tank miles to total */

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 63 Thursday, November 12, 2009 6:19 PM

Exercises 63

23
24 /* display miles per gallon for current tank */
25 printf( "The miles / gallon for this tank was %f\n\n",
26 miles / gallons );
27
28 /* get next tank's gallons */
29 printf( "Enter the gallons used ( -1 to end ): " );
30 scanf( "%f", &gallons );
31 } /* end while */
32
33 /* calculate average miles per gallon over all tanks */
34 totalAverage = totalMiles / totalGallons;
35 printf("\nThe overall average miles / gallon was %f\n", totalAverage );
36 return 0; /* indicate successful termination */
37 } /* end main */

3.18 Develop a C program that will determine if a department store customer has exceeded the
credit limit on a charge account. For each customer, the following facts are available:
a) Account number
b) Balance at the beginning of the month
c) Total of all items charged by this customer this month
d) Total of all credits applied to this customer's account this month
e) Allowed credit limit
The program should input each of these facts, calculate the new balance (= beginning balance
+ charges – credits), and determine if the new balance exceeds the customer's credit limit. For those
customers whose credit limit is exceeded, the program should display the customer's account num-
ber, credit limit, new balance and the message “Credit limit exceeded.” Here is a sample input/out-
put dialog:

Enter account number (-1 to end): 100


Enter beginning balance: 5394.78
Enter total charges: 1000.00
Enter total credits: 500.00
Enter credit limit: 5500.00
Account: 100
Credit limit: 5500.00
Balance: 5894.78
Credit Limit Exceeded.

Enter account number (-1 to end): 200


Enter beginning balance: 1000.00
Enter total charges: 123.45
Enter total credits: 321.00
Enter credit limit: 1500.00
Enter account number (-1 to end): 300
Enter beginning balance: 500.00
Enter total charges: 274.73
Enter total credits: 100.00
Enter credit limit: 800.00

Enter account number (-1 to end): -1

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 64 Thursday, November 12, 2009 6:19 PM

64 Chapter 3 Structured Program Development in C: Solutions

ANS:
2)Top:
Determine if each of an arbitrary number of department store customers has exceed-
ed the credit limit on a charge account

First refinement:
Input the account number, beginning balance, total charges, total credits, and credit
limit for a customer, calculate the customer’s new balance and determine if the bal-
ance exceeds the credit limit. Then process the next customer.

Second refinement:
Input the first customer’s account number

While the sentinel value (-1) has not been entered for the account number
Input the customer’s beginning balance
Input the customer’s total charges
Input the customer’s total credits
Input the customer’s credit limit
Calculate the customer’s new balance

If the balance exceeds the credit limit


Print the account number
Print the credit limit
Print the balance
Print “Credit Limit Exceeded”

Input the next customer’s account number


3)

1 /* Exercise 3.18 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int accountNumber; /* current account's number */
7 float balance; /* current account's starting balance */
8 float charges; /* current account's total charges */
9 float credits; /* current account's total credits */
10 float limit; /* current account's credit limit */
11
12 /* get account number */
13 printf( "\nEnter account number ( -1 to end): " );
14 scanf( "%d", &accountNumber );
15
16 /* loop until sentinel value read from user */
17 while ( accountNumber != -1 ) {
18 printf( "Enter beginning balance: " );
19 scanf( "%f", &balance );
20
21 printf( "Enter total charges: " );
22 scanf( "%f", &charges );

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 65 Thursday, November 12, 2009 6:19 PM

Exercises 65

23
24 printf( "Enter total credits: " );
25 scanf( "%f", &credits );
26
27 printf( "Enter credit limit: " );
28 scanf( "%f", &limit );
29
30 balance += charges - credits; /* calculate balance */
31
32 /* if balance is over limit, display account number
33 with credit limit and balance to two digits of precision */
34 if ( balance > limit ) {
35 printf( "%s%d\n%s%.2f\n%s%.2f\n%s\n",
36 "Account: ", accountNumber, "Credit limit: ", limit,
37 "Balance: ", balance, "Credit Limit Exceeded." );
38 } /* end if */
39
40 /* prompt for next account */
41 printf( "\nEnter account number ( -1 to end ): " );
42 scanf( "%d", &accountNumber );
43 } /* end while */
44
45 return 0; /* indicate successful termination */
46 } /* end main */

3.19 One large chemical company pays its salespeople on a commission basis. The salespeople
receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who
sells $5000 worth of chemicals in a week receives $200 plus 9% of $5000, or a total of $650. De-
velop a program that will input each salesperson’s gross sales for last week and will calculate and dis-
play that salesperson's earnings. Process one salesperson's figures at a time. Here is a sample input/
output dialog:

Enter sales in dollars (-1 to end): 5000.00


Salary is: $650.00

Enter sales in dollars (-1 to end): 1234.56


Salary is: $311.11

Enter sales in dollars (-1 to end): 1088.89


Salary is: $298.00

Enter sales in dollars (-1 to end): -1

ANS:
2)Top:
For an arbitrary number of salespeople, determine each salesperson’s earnings for the
last week

First refinement:
Input the salesperson’s sales for the week, calculate and print the salesperson’s wages
for the week, then process the next salesperson

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 66 Thursday, November 12, 2009 6:19 PM

66 Chapter 3 Structured Program Development in C: Solutions

Second refinement:
Input the first salesperson’s sales in dollars
While the sentinel value (-1) has not been entered for the sales
Calculate the salesperson’s wages for the week
Print the salesperson’s wages for the week
Input the next salesperson’s sales in dollars
3)

1 /* Exercise 3.19 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 float sales; /* gross weekly sales */
7 float wage; /* commissioned earnings */
8
9 /* get first sales */
10 printf( "Enter sales in dollars (-1 to end): " );
11 scanf( "%f", &sales );
12
13 /* loop until sentinel value read from user */
14 while ( sales >= 0.0 ) {
15 wage = 200.0 + 0.09 * sales; /* calculate wage */
16
17 /* display salary */
18 printf( "Salary is: $%.2f\n\n", wage );
19
20 /* prompt for next sales */
21 printf( "Enter sales in dollars (-1 to end): " );
22 scanf( "%f", &sales );
23 } /* end while */
24
25 return 0; /* indicate successful termination */
26 } /* end main */

3.20 The simple interest on a loan is calculated by the formula


interest = principal * rate * days / 365;

The preceding formula assumes that rate is the annual interest rate, and therefore includes the
division by 365 (days). Develop a program that will input principal, rate and days for several

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 67 Thursday, November 12, 2009 6:19 PM

Exercises 67

loans, and will calculate and display the simple interest for each loan, using the preceding formula.
Here is a sample input/output dialog:

Enter loan principal (-1 to end): 1000.00


Enter interest rate: .1
Enter term of the loan in days: 365
The interest charge is $100.00

Enter loan principal (-1 to end): 1000.00


Enter interest rate: .08375
Enter term of the loan in days: 224
The interest charge is $51.40

Enter loan principal (-1 to end): 10000.00


Enter interest rate: .09
Enter term of the loan in days: 1460
The interest charge is $3600.00

Enter loan principal (-1 to end): -1

ANS:
2) Top:
For an arbitrary number of loans determine the simple interest for each loan

First refinement:
Input the principal of the loan, the interest rate, and the term of the loan, calculate
and print the simple interest for the loan, and process the next loan

Second refinement:
input the first loan principal in dollars
While the sentinel value (-1) has not been entered for the loan principal
Input the interest rate
Input the term of the loan in days
Calculate the simple interest for the loan
Print the simple interest for the loan
Input the loan principal for the next loan

3)

1 /* Exercise 3.20 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 float principal; /* loan principal */
7 float rate; /* interest rate */
8 float interest; /* interest charge */
9 int term; /* length of loan in days */
10
11 /* get loan principal */
12 printf( "Enter loan principal (-1 to end): " );

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 68 Thursday, November 12, 2009 6:19 PM

68 Chapter 3 Structured Program Development in C: Solutions

13 scanf( "%f", &principal );


14
15 /* loop until sentinel value is read from user */
16 while ( principal >= 0.0 ) {
17 printf( "Enter interest rate: " ); /* get rate */
18 scanf( "%f", &rate );
19
20 printf( "Enter term of the loan in days: " ); /* get term */
21 scanf( "%d", &term );
22
23 /* calculate interest charge */
24 interest = principal * rate * term / 365.0;
25 printf( "The interest charge is $%.2f\n\n", interest );
26
27 /* get next loan principal */
28 printf( "Enter loan principal (-1 to end): " );
29 scanf( "%f", &principal );
30 } /* end while */
31
32 return 0; /* indicate successful termination */
33 } /* end main */

3.21 Develop a program that will determine the gross pay for each of several employees. The
company pays “straight time” for the first 40 hours worked by each employee and pays “time-and-
a-half” for all hours worked in excess of 40 hours. You are given a list of the employees of the com-
pany, the number of hours each employee worked last week and the hourly rate of each employee.
Your program should input this information for each employee, and should determine and display
the employee's gross pay. Here is a sample input/output dialog:

Enter # of hours worked (-1 to end): 39


Enter hourly rate of the worker ($00.00): 10.00
Salary is $390.00

Enter # of hours worked (-1 to end): 40


Enter hourly rate of the worker ($00.00): 10.00
Salary is $400.00

Enter # of hours worked (-1 to end): 41


Enter hourly rate of the worker ($00.00): 10.00
Salary is $415.00

Enter # of hours worked (-1 to end): -1

ANS:
2)Top:
For an arbitrary number of employees, determine the gross pay for each employee

First refinement:
Input the number of hours worked for the employee, enter the employee’s hourly
wage, calculate and print the employee’s gross pay, and process the next employee

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 69 Thursday, November 12, 2009 6:19 PM

Exercises 69

Second refinement:
Input the first employee’s number of hours worked
While the sentinel value (-1) has not been entered for the hours worked
Input the employee’s hourly wage
Calculate the employee’s gross pay with overtime for hours over 40
Print the employee’s gross pay
Input the number of hours worked for the next employee

3)

1 /* Exercise 3.21 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 float hours; /* total hours worked */
7 float rate; /* hourly pay rate */
8 float salary; /* gross pay */
9
10 /* get first employee's hours worked */
11 printf( "Enter number of hours worked ( -1 to end ): " );
12 scanf( "%f", &hours );
13
14 /* loop until sentinel value read from user */
15 while ( hours >= 0.0 ) {
16
17 /* get hourly rate */
18 printf( "Enter hourly rate of the worker ( $00.00 ): " );
19 scanf( "%f", &rate );
20
21 /* if employee worked less than 40 hours */
22 if ( hours <= 40 ) {
23 salary = hours * rate;
24 } /* end if */
25 else { /* compute "time-and-a-half" pay */
26 salary = 40.0 * rate + ( hours - 40.0 ) * rate * 1.5;
27 } /* end else */
28
29 /* display gross pay */
30 printf( "Salary is $%.2f\n\n", salary );
31
32 /* prompt for next employee's data */
33 printf( "Enter number of hours worked ( -1 to end ): " );
34 scanf( "%f", &hours );
35 } /* end while */
36
37 return 0; /* indicate successful termination */
38 } /* end main */

3.22 Write a program that demonstrates the difference between predecrementing and post-
decrementing using the decrement operator --.

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 70 Thursday, November 12, 2009 6:19 PM

70 Chapter 3 Structured Program Development in C: Solutions

ANS:

1 /* Exercise 3.22 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int c; /* define c to use decrement operator */
7
8 c = 5;
9 printf( "%d\n", c );
10 printf( "%d\n", --c ); /* predecrement */
11 printf( "%d\n\n", c );
12
13 c = 5;
14 printf( "%d\n", c );
15 printf( "%d\n", c-- ); /* postdecrement */
16 printf( "%d\n\n", c );
17
18 return 0; /* indicate successful termination */
19 } /* end main */

5
4
4

5
5
4

3.23 Write a program that utilizes looping to print the numbers from 1 to 10 side by side on the
same line with three spaces between numbers.
ANS:

1 /* Exercise 3.23 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int i = 0; /* initialize i */
7
8 /* loop while i is less than 11 */
9 while ( ++i < 11 ) {
10 printf( "%d ", i );
11 } /* end while */
12
13 printf( "\n" );
14 return 0; /* indicate successful termination */
15 } /* end main */

1 2 3 4 5 6 7 8 9 10

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 71 Thursday, November 12, 2009 6:19 PM

Exercises 71

3.24 The process of finding the largest number (i.e., the maximum of a group of numbers) is
used frequently in computer applications. For example, a program that determines the winner of a
sales contest would input the number of units sold by each salesperson. The salesperson who sold
the most units wins the contest. Write a pseudocode program and then a program that inputs a se-
ries of 10 numbers and determines and prints the largest of the numbers. [Hint: Your program
should use three variables as follows]:
counter: A counter to count to 10 (i.e., to keep track of how many numbers have
been input and to determine when all 10 numbers have been processed)
number: The current number input to the program
largest: The largest number found so far
ANS:
Input the first number directly into the variable largest
Increment counter to 2

While counter is less than or equal to 10


Input a new variable into the variable number
If number is greater than largest
Replace largest with number
Increment counter

Print the value of the largest (while condition false when counter is 11)

1 /* Exercise 3.24 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int counter; /* counter for 10 repetitions */
7 int number; /* current number input */
8 int largest; /* largest number found so far */
9
10 /* get first number */
11 printf( "Enter the first number: " );
12 scanf( "%d", &largest );
13 counter = 2;
14
15 /* loop 9 more times */
16 while ( counter <= 10 ) {
17 printf( "Enter next number: " ); /* get next number */
18 scanf( "%d", &number );
19
20 /* if current number input is greater than largest number,
21 update largest */
22 if ( number > largest ) {
23 largest = number;
24 } /* end if */
25
26 counter++;
27 } /* end while */
28
29 printf( "Largest is %d\n", largest ); /* display largest number */

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 72 Thursday, November 12, 2009 6:19 PM

72 Chapter 3 Structured Program Development in C: Solutions

30 return 0; /* indicate successful termination */


31 } /* end main */

Enter the first number: 7


Enter next number: 37
Enter next number: 78
Enter next number: 2
Enter next number: 437
Enter next number: 72
Enter next number: 1
Enter next number: 4
Enter next number: 36
Enter next number: 100
Largest is 437

3.25 Write a program that utilizes looping to print the following table of values:

N 10*N 100*N 1000*N

1 10 100 1000
2 20 200 2000
3 30 300 3000
4 40 400 4000
5 50 500 5000
6 60 600 6000
7 70 700 7000
8 80 800 8000
9 90 900 9000
10 100 1000 10000

The tab escape sequence, \t, may be used in the printf statement to separate the columns with
tabs.
ANS:

1 /* Exercise 3.25 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int n = 0; /* counter */
7
8 /* display table headers */
9 printf( "\tN\t\t10 * N\t\t100 * N\t\t1000 * N\n\n" );
10
11 /* loop 10 times */
12 while ( ++n <= 10 ) {
13
14 /* calculate and display table values */
15 printf( "\t%d\t\t%d\t\t%d\t\t%d\n",
16 n, 10 * n, 100 * n, 1000 * n );
17 } /* end while */
18
19 return 0; /* indicate successful termination */
20 } /* end main */

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 73 Thursday, November 12, 2009 6:19 PM

Exercises 73

3.26 Write a program that utilizes looping to produce the following table of values:

A A+2 A+4 A+6

3 5 7 9
6 8 10 12
9 11 13 15
12 14 16 18
15 17 19 21

ANS:

1 /* Exercise 3.26 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int a = 3; /* counter */
7
8 /* display table headers */
9 printf( "A\tA+2\tA+4\tA+6\n\n" );
10
11 /* loop 5 times */
12 while ( a <= 15 ) {
13
14 /* calculate and display table values */
15 printf( "%d\t%d\t%d\t%d\n", a, a + 2, a + 4, a + 6 );
16 a += 3;
17 } /* end while */
18
19 return 0; /* indicate successful termination */
20
21 } /* end main */

3.27 Using an approach similar to Exercise 3.24, find the two largest values of the 10 numbers.
[Note: You may input each number only once.]
ANS:

1 /* Exercise 3.27 Solution */


2 #include <stdio.h>
3
4 int main( void )
5
6 {
7 int counter; /* counter for 10 repetitions */
8 int number; /* current number input */
9 int largest; /* largest number found */
10 int secondLargest = 0; /* second largest number found */
11
12 printf( "Enter the first number: " ); /* get first number */
13 scanf( "%d", &largest );

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 74 Thursday, November 12, 2009 6:19 PM

74 Chapter 3 Structured Program Development in C: Solutions

14 counter = 2;
15
16 /* loop 9 more times */
17 while ( counter <= 10 ) {
18 printf( "Enter next number: " ); /* prompt for next number */
19 scanf( "%d", &number );
20
21 /* if current number is greater than largest */
22 if ( number > largest ) {
23
24 /* update second largest with previous largest */
25 secondLargest = largest;
26
27 /* update largest with current number */
28 largest = number;
29 } /* end if */
30 else {
31
32 /* if number is between secondLargest and largest */
33 if ( number > secondLargest ) {
34 secondLargest = number;
35 } /* end if */
36
37 } /* end else */
38
39 ++counter;
40 } /* end while */
41
42 /* display largest two numbers */
43 printf( "Largest is %d\n", largest );
44 printf( "Second largest is %d\n", secondLargest );
45
46 return 0; /* indicate successful termination */
47
48 } /* end main */

Enter the first number: 100


Enter next number: 102
Enter next number: 83
Enter next number: 3883
Enter next number: 328
Enter next number: 28
Enter next number: 839
Enter next number: 2398
Enter next number: 182
Enter next number: 0
Largest is 3883
Second largest is 2398

3.28 Modify the program in Figure 3.10 to validate its inputs. On any input, if the value entered
is other than 1 or 2, keep looping until the user enters a correct value.
ANS:

1 /* Exercise 3.28 Solution */


2 #include <stdio.h>

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 75 Thursday, November 12, 2009 6:19 PM

Exercises 75

3
4 int main( void )
5 {
6 int passes = 0; /* number of passes */
7 int failures = 0; /* number of failures */
8 int student = 1; /* student counter */
9 int result; /* one exam result */
10
11 /* process 10 students using counter-controlled loop */
12 while ( student <= 10 ) {
13
14 /* prompt user for input and obtain value from user */
15 printf( "Enter result ( 1=pass, 2=fail ): " );
16 scanf( "%d", &result );
17
18 /* if result 1, increment passes */
19 if ( result == 1 ) {
20 ++passes;
21 ++student;
22 } /* end if */
23 else if ( result == 2 ) { /* if result is 2, increment failures */
24 ++failures;
25 ++student;
26 } /* end else if */
27 else { /* if result is not 1 or 2, result is invalid */
28 printf( "Invalid result\n" );
29 } /* end else */
30
31 } /* end while */
32
33 printf( "Passed %d\nFailed %d\n", passes, failures );
34
35 /* if more than eight students passed, print "raise tuition" */
36 if ( passes > 8 ) {
37 printf( "Raise tuition\n" );
38 } /* end if */
39
40 return 0; /* indicate successful termination */
41 } /* end main */

Enter result ( 1=pass, 2=fail ): 1


Enter result ( 1=pass, 2=fail ): 2
Enter result ( 1=pass, 2=fail ): 3
Invalid result
Enter result ( 1=pass, 2=fail ): 4
Invalid result
Enter result ( 1=pass, 2=fail ): 2
Enter result ( 1=pass, 2=fail ): 2
Enter result ( 1=pass, 2=fail ): 2
Enter result ( 1=pass, 2=fail ): 1
Enter result ( 1=pass, 2=fail ): 1
Enter result ( 1=pass, 2=fail ): 1
Enter result ( 1=pass, 2=fail ): 1
Enter result ( 1=pass, 2=fail ): 1
Passed 6
Failed 4

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 76 Thursday, November 12, 2009 6:19 PM

76 Chapter 3 Structured Program Development in C: Solutions

3.29 What does the following program print?

1 #include <stdio.h>
2
3 /* function main begins program execution */
4 int main( void )
5 {
6 int count = 1; /* initialize count */
7
8 while ( count <= 10 ) { /* loop 10 times */
9
10 /* output line of text */
11 printf( "%s\n", count % 2 ? "****" : "++++++++" );
12 count++; /* increment count */
13 } /* end while */
14
15 return 0; /* indicate program ended successfully */
16 } /* end function main */

ANS:

****
++++++++
****
++++++++
****
++++++++
****
++++++++
****
++++++++

3.30 What does the following program print?

1 #include <stdio.h>
2
3 /* function main begins program execution */
4 int main( void )
5 {
6 int row = 10; /* initialize row */
7 int column; /* define column */
8
9 while ( row >= 1 ) { /* loop until row < 1 */
10 column = 1; /* set column to 1 as iteration begins */
11
12 while ( column <= 10 ) { /* loop 10 times */
13 printf( "%s", row % 2 ? "<": ">" ); /* output */
14 column++; /* increment column */
15 } /* end inner while */
16
17 row--; /* decrement row */
18 printf( "\n" ); /* begin new output line */
19 } /* end outer while */

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 77 Thursday, November 12, 2009 6:19 PM

Exercises 77

20
21 return 0; /* indicate program ended successfully */
22 } /* end function main */

ANS:

>>>>>>>>>>
<<<<<<<<<<
>>>>>>>>>>
<<<<<<<<<<
>>>>>>>>>>
<<<<<<<<<<
>>>>>>>>>>
<<<<<<<<<<
>>>>>>>>>>
<<<<<<<<<<

3.31 (Dangling Else Problem) Determine the output for each of the following when x is 9 and y is
11, and when x is 11 and y is 9. Note that the compiler ignores the indentation in a C program. Also,
the compiler always associates an else with the previous if unless told to do otherwise by the place-
ment of braces {}. Because, on first glance, you may not be sure which if an else matches, this is
referred to as the “dangling else” problem. We eliminated the indentation from the following code
to make the problem more challenging. [Hint: Apply indentation conventions you have learned.]
a) if ( x < 10 )
if ( y > 10 )
printf( "*****\n" );
else
printf( "#####\n" );
printf( "$$$$$\n" );
ANS:
x = 9, y = 11

*****
$$$$$

x = 11, y = 9

$$$$$

b) if ( x < 10 ) {
if ( y > 10 )
printf( "*****\n" );
}
else {
printf( "#####\n" );
printf( "$$$$$\n" );
}

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 78 Thursday, November 12, 2009 6:19 PM

78 Chapter 3 Structured Program Development in C: Solutions

ANS: x = 9, y = 11

*****

x = 11, y = 9

#####
$$$$$

3.32 (Another Dangling Else Problem) Modify the following code to produce the output shown.
Use proper indentation techniques. You may not make any changes other than inserting braces. The
compiler ignores the indentation in a program. We eliminated the indentation from the following
code to make the problem more challenging. [Note: It is possible that no modification is necessary.]
if ( y == 8 )
if ( x == 5 )
printf( "@@@@@\n" );
else
printf( "#####\n" );
printf( "$$$$$\n" );
printf( "&&&&&\n" );

a) Assuming x = 5 and y = 8, the following output is produced.

@@@@@
$$$$$
&&&&&

ANS:
if ( y == 8 ) {
if ( x == 5 )
printf( "@@@@@\n" );
else
printf( "#####\n" );
printf( "$$$$$\n" );
printf( "&&&&&\n" );
}

b) Assuming x = 5 and y = 8, the following output is produced.

@@@@@

ANS:
if ( y == 8 )
if ( x == 5 )
printf( "@@@@@\n" );
else {
printf( "#####\n" );
printf( "$$$$$\n" );

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 79 Thursday, November 12, 2009 6:19 PM

Exercises 79

printf( "&&&&&\n" );
}

c) Assuming x = 5 and y = 8, the following output is produced.

@@@@@
&&&&&

ANS:
if ( y == 8 )
if ( x == 5 )
printf( "@@@@@\n" );
else {
printf( "#####\n" );
printf( "$$$$$\n" );
}
printf( "&&&&&\n" );

d) Assuming x = 5 and y = 7, the following output is produced. [Note: The last three printf
statements are all part of a compound statement.]

#####
$$$$$
&&&&&

ANS:
if ( y == 8 ) {
if ( x == 5 )
printf( "@@@@@\n" );
}
else {
printf( "#####\n" );
printf( "$$$$$\n" );
printf( "&&&&&\n" );
}

3.33 Write a program that reads in the side of a square and then prints that square out of aster-
isks. Your program should work for squares of all side sizes between 1 and 20. For example, if your
program reads a size of 4, it should print

****
****
****
****

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 80 Thursday, November 12, 2009 6:19 PM

80 Chapter 3 Structured Program Development in C: Solutions

ANS:

1 /* Exercise 3.33 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int side; /* side integer */
7 int row_num; /* row counter */
8 int asterisk; /* asterisk counter */
9
10 printf( "Enter the square side: " ); /* get size of square */
11 scanf( "%d", &side );
12
13 row_num = side;
14
15 /* loop through rows of square */
16 while ( row_num < side ) {
17 asterisk = side;
18
19 /* loop through columns of square */
20 while ( asterisk > 0 ) {
21 printf( "*" );
22 --asterisk;
23 } /* end inner while */
24
25 printf( "\n" );
26 ++row_num;
27 } /* end outer while */
28
29 return 0; /* indicate successful termination */
30
31 } /* end main */

3.34 Modify the program you wrote in Exercise 3.33 so that it prints a hollow square. For exam-
ple, if your program reads a size of 5, it should print

*****
* *
* *
* *
*****

ANS:

1 /* Exercise 3.34 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int side; /* length of side */

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 81 Thursday, November 12, 2009 6:19 PM

Exercises 81

7 int rowPosition; /* row counter */


8 int colPosition; /* column counter */
9
10 printf( "Enter the square side: " ); /* prompt for side length */
11 scanf( "%d", &side );
12
13 colPosition = side; /* set size counter to length of side */
14
15 /* loop side number of times */
16 while ( colPosition > 0 ) {
17 rowPosition = side; /* set row counter to length of side */
18
19 /* loop rowPosition number of times */
20 while ( rowPosition > 0 ) {
21
22 /* if side or row counter is 1 or size print an '*' */
23 if ( colPosition == side ) {
24 printf( "*" );
25 } /* end if */
26 else if ( colPosition == 1 ) {
27 printf( "*" );
28 } /* end else if */
29 else if ( rowPosition == 1 ) {
30 printf( "*" );
31 } /* end else if */
32 else if ( rowPosition == side ) {
33 printf( "*" );
34 } /* end else if */
35 else { /* otherwise, print a space */
36 printf( " " );
37 } /* end else */
38
39 --rowPosition; /* decrement row counter */
40 } /* end inner while */
41
42 printf( "\n" ); /* new line for next row */
43 --colPosition; /* decrement size counter */
44 } /* end outer while */
45
46 return 0; /* indicate successful termination */
47 } /* end main */

3.35 A palindrome is a number or a text phrase that reads the same backward as forward. For
example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611.
Write a program that reads in a five-digit integer and determines whether or not it is a palindrome.
[Hint: Use the division and remainder operators to separate the number into its individual digits.]
ANS:

1 /* Exercise 3.35 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 82 Thursday, November 12, 2009 6:19 PM

82 Chapter 3 Structured Program Development in C: Solutions

6 int number; /* input number */


7 int temp; /* temporary integer */
8 int firstDigit; /* first digit of input */
9 int secondDigit; /* second digit of input */
10 int fourthDigit; /* fourth digit of input */
11 int fifthDigit; /* fifth digit of input */
12
13 printf( "Enter a five-digit number: " ); /* get number */
14 scanf( "%d", &number );
15
16 temp = number;
17
18 /* determine first digit by integer division by 10000 */
19 firstDigit = temp / 10000;
20 temp = number % 10000;
21
22 /* determine second digit by integer division by 1000 */
23 secondDigit = temp / 1000;
24 temp = number % 100;
25
26 /* determine fourth digit by integer division by 10 */
27 fourthDigit = temp / 10;
28
29 fifthDigit = number % 10;
30
31 /* if first and fifth digits are equal */
32 if ( firstDigit == fifthDigit ) {
33
34 /* if second and fourth digits are equal */
35 if ( secondDigit == fourthDigit ) {
36
37 /* number is a palindrome */
38 printf( "%d is a palindrome\n", number );
39 } /* end if */
40 else { /* number is not a palindrome */
41 printf( "%d is not a palindrome\n", number );
42 } /* end else */
43
44 } /* end if */
45 else { /* number is not a palindrome */
46 printf( "%d is not a palindrome\n", number );
47 } /* end else */
48
49 return 0; /* indicate successful termination */
50 } /* end main */

Enter a five-digit number: 18181


18181 is a palindrome

Enter a five-digit number: 16738


16738 is not a palindrome

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 83 Thursday, November 12, 2009 6:19 PM

Exercises 83

3.36 Input an integer containing only 0s and 1s (i.e., a “binary” integer) and print its decimal
equivalent. [Hint: Use the remainder and division operators to pick off the “binary” number’s digits
one at a time from right to left. Just as in the decimal number system, in which the rightmost digit
has a positional value of 1, and the next digit left has a positional value of 10, then 100, then 1000,
and so on, in the binary number system the rightmost digit has a positional value of 1, the next digit
left has a positional value of 2, then 4, then 8, and so on. Thus the decimal number 234 can be
interpreted as 4 * 1 + 3 * 10 + 2 * 100. The decimal equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1
* 4 + 1 * 8 or 1 + 0 + 4 + 8 or 13.]
ANS:

1 /* Exercise 3.36 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int binary; /* current value of binary number */
7 int number; /* input binary number */
8 int decimal = 0; /* current value of decimal number */
9 int highBit = 16; /* value of highest bit */
10 int factor = 10000; /* factor of 10 to pick off digits */
11
12 /* prompt for binary input */
13 printf( "Enter a binary number ( 5 digits maximum ): " );
14 scanf( "%d", &binary );
15
16 number = binary; /* save in number for final display */
17
18 /* loop 5 times using powers of 2 */
19 while ( highBit >= 1 ) {
20
21 /* update decimal value with decimal value corresponding
22 to current highest binary bit */
23 decimal += binary / factor * highBit;
24
25 /* reduce high bit by factor of 2, i.e.,
26 move one bit to the right */
27 highBit /= 2;
28
29 /* reduce binary number to eliminate current highest bit */
30 binary %= factor;
31
32 /* reduce factor by power of 10, i.e.,
33 move one bit to the right */
34 factor /= 10;
35 } /* end while */
36
37 /* display decimal value */
38 printf( "The decimal equivalent of %d is %d\n", number, decimal );
39
40 return 0; /* indicate successful termination */
41 } /* end main */

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 84 Thursday, November 12, 2009 6:19 PM

84 Chapter 3 Structured Program Development in C: Solutions

Enter a binary number ( 5 digits maximum ): 10111


The decimal equivalent of 10111 is 23

Enter a binary number ( 5 digits maximum ): 1101


The decimal equivalent of 1101 is 13

3.37 How can you determine how fast your own computer really operates? Write a program with
a while loop that counts from 1 to 300,000,000 by 1s. Every time the count reaches a multiple of
100,000,000, print that number on the screen. Use your watch to time how long each 100 million
repetitions of the loop takes.
ANS:

1 /* Exercise 3.37 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int count = 1; /* counter */
7
8 /* loop to 300,000,000 */
9 while( count <= 300000000 ) {
10
11 if ( count % 100000000 == 0 ) {
12 printf( "Multiple is %d\n", count / 100000000 );
13 } /* end if */
14
15 ++count; /* increment count */
16 } /* end while */
17
18 return 0; /* indicate successful termination */
19 } /* end main */

Multiple is 1
Multiple is 2
Multiple is 3

3.38 Write a program that prints 100 asterisks, one at a time. After every tenth asterisk, your pro-
gram should print a newline character. [Hint: Count from 1 to 100. Use the remainder operator to
recognize each time the counter reaches a multiple of 10.]
ANS:

1 /* Exercise 3.38 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int count = 0; /* counter */
7

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 85 Thursday, November 12, 2009 6:19 PM

Exercises 85

8 /* loop to 100 */
9 while( ++count <= 100 )
10
11 /* print a new line after every tenth asterisk */
12 count % 10 == 0 ? printf( "*\n" ) : printf( "*" );
13
14 return 0; /* indicate successful termination */
15 } /* end main */

**********
**********
**********
**********
**********
**********
**********
**********
**********
**********

3.39 Write a program that reads an integer and determines and prints how many digits in the
integer are 7s.
ANS:

1 /* Exercise 3.39 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int number; /* user input */
7 int numCopy; /* copy of number */
8 int factor = 10000; /* set factor to pick off digits */
9 int digit; /* individual digit of number */
10 int sevens = 0; /* sevens counter */
11
12 printf( "Enter a 5-digit number: " ); /* get number from user */
13 scanf( "%d", &number );
14
15 if ( number < 0 )
16 return 1;
17 if ( number > 99999 )
18 return 1;
19
20 numCopy = number;
21
22 /* loop through each of the 5 digits */
23 while ( factor >= 1 ) {
24 digit = numCopy / factor; /* pick off next digit */
25
26 if ( digit == 7 ) { /* if digit equals 7, increment sevens */
27 ++sevens;
28 } /* end if */
29
30 numCopy %= factor;

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 86 Thursday, November 12, 2009 6:19 PM

86 Chapter 3 Structured Program Development in C: Solutions

31 factor /= 10;
32 } /* end while */
33
34 /* output number of sevens */
35 printf( "The number %d has %d seven(s) in it\n", number, sevens );
36
37 return 0; /* indicate successful termination */
38 } /* end main */

Enter a 5-digit number: 17737


The number 17737 has 3 seven(s) in it

Enter a 5-digit number: 11727


The number 11727 has 2 seven(s) in it

3.40 Write a program that displays the following checkerboard pattern:

* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *

Your program must use only three output statements, one of each of the following forms:
printf( "* " );
printf( " " );
printf( "\n" );

ANS:

1 /* Exercise 3.40 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int row = 0; /* row counter */
7 int col; /* column counter */
8
9 /* loop 8 times */
10 while ( row < 8 ) {
11 col = 0; /* reset column counter */
12
13 /* if odd row, begin with a space */
14 if ( row % 2 != 0 ) {
15 printf( " " );
16 } /* end if */
17

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 87 Thursday, November 12, 2009 6:19 PM

Exercises 87

18 /* loop 8 times */
19 while ( col < 8 ) {
20 printf( "* " );
21 ++col;
22 } /* end while */
23
24 printf( "\n" ); /* go to next line */
25 ++row;
26 } /* end while */
27
28 return 0; /* indicate successful termination */
29
30 } /* end main */

3.41 Write a program that keeps printing the multiples of the integer 2, namely 2, 4, 8, 16, 32,
64, and so on. Your loop should not terminate (i.e., you should create an infinite loop). What hap-
pens when you run this program?
ANS: Program execution terminates when largest integer is exceeded. (That is, the loop
continuation test fails when the maximum value for an integer is exceeded. On a 4-
byte system, the largest integer value is 2147483647 and anything above that is rep-
resented by a negative number, which fails the loop continuation test.)

1 /* Exercise 3.41 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int multiple = 1; /* counter */
7
8 /* infinite loop */
9 while ( multiple > 0 ) {
10
11 /* calculate the next power of two */
12 multiple *= 2;
13 printf( "%d\n", multiple );
14 } /* end while */
15
16 return 0; /* indicate successful termination */
17 } /* end main */

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 88 Thursday, November 12, 2009 6:19 PM

88 Chapter 3 Structured Program Development in C: Solutions

2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
-2147483648

3.42 Write a program that reads the radius of a circle (as a float value) and computes and prints
the diameter, the circumference and the area. Use the value 3.14159 for π.
ANS:

1 /* Exercise 3.42 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 float radius; /* input radius */
7 float pi = 3.14159; /* value for pi */
8
9 printf( "Enter the radius: "); /* get radius value */
10 scanf( "%f", &radius );
11
12 /* compute and display diameter */
13 printf( "The diameter is %.2f\n", radius * 2 );
14
15 /* compute and display circumference */
16 printf( "The circumference is %.2f\n", 2 * pi * radius );
17
18 /* compute and display area */
19 printf( "The area is %.2f\n", pi * radius * radius );
20
21 return 0; /* indicate successful termination */
22
23 } /* end main */

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 89 Thursday, November 12, 2009 6:19 PM

Exercises 89

Enter the radius: 4.7


The diameter is 9.40
The circumference is 29.53
The area is 69.40

3.43 What is wrong with the following statement? Rewrite the statement to accomplish what the
programmer was probably trying to do.
printf( "%d", ++( x + y ) );

ANS: printf( “%d”, 1 + x + y );


3.44 Write a program that reads three nonzero float values and determines and prints if they
could represent the sides of a right triangle.
ANS:

1 /* Exercise 3.44 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 float a; /* first number */
7 float b; /* second number */
8 float c; /* third number */
9
10 /* input 3 numbers */
11 printf( "Enter three floating point numbers: " );
12 scanf( "%f%f%f", &a, &b, &c);
13
14 /* use Pythagorean Theorem */
15 if ( c * c == a * a + b * b ) {
16 printf( "The three numbers could be sides of a right triangle\n" );
17 } /* end if */
18 else if ( b * b == a * a + c * c ) {
19 printf( "The three numbers could be sides of a right triangle\n" );
20 } /* end if */
21 else if ( a * a == b * b + c * c ) {
22 printf( "The three numbers could be sides of a right triangle\n" );
23 } /* end if */
24 else {
25 printf( "The three numbers could");
26 printf( " not be the sides of a right triangle\n" );
27 } /* end if */
28
29 return 0; /* indicate successful termination */
30
31 } /* end main */

Enter three floating point numbers: 5.7 3.6 2.2


The three numbers could not be the sides of a right triangle

Enter three floating point numbers: 3.0 4.0 5.0


The three numbers could be sides of a right triangle

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 90 Thursday, November 12, 2009 6:19 PM

90 Chapter 3 Structured Program Development in C: Solutions

3.45 Write a program that reads three nonzero integers and determines and prints if they could
be the sides of a right triangle.
ANS:

1 /* Exercise 3.45 Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int a; /* first number */
7 int b; /* second number */
8 int c; /* third number */
9
10 /* input three numbers */
11 printf( "Enter three integers: ");
12 scanf( "%d%d%d", &a, &b, &c );
13
14 /* use Pythagorean theorem */
15 if ( c * c == a * a + b * b ) {
16 printf( "The three integers are the sides of" );
17 printf( " a right triangle\n" );
18 } /* end if */
19 else if ( b * b == a * a + c * c ) {
20 printf( "The three integers are the sides of" );
21 printf( " a right triangle\n" );
22 } /* end if */
23 else if ( a * a == b * b + c * c ) {
24 printf( "The three integers are the sides of" );
25 printf( " a right triangle\n" );
26 } /* end if */
27 else {
28 printf( "The three integers are not the sides" );
29 printf( " of a right triangle\n" );
30 } /* end else */
31
32 return 0; /* indicate successful termination */
33 } /* end main */

Enter three integers: 3 4 5


The three integers are the sides of a right triangle

Enter three integers: 9 4 1


The three integers are not the sides of a right triangle

3.46 The factorial of a nonnegative integer n is written n! (pronounced “n factorial”) and is de-
fined as follows:
n! = n · (n - 1) · (n - 2) · … · 1 (for values of n greater than or equal to 1)
and
n! = 1 (for n = 0).
For example, 5! = 5 · 4 · 3 · 2 · 1, which is 120.
a) Write a program that reads a nonnegative integer and computes and prints its factorial.

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 91 Thursday, November 12, 2009 6:19 PM

Exercises 91

ANS:

1 /* Exercise 3.47 Part A Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int n; /* current multiplication factor */
7 int number = -1; /* input number */
8 int factorial = 1; /* resulting factorial */
9
10 /* loop until valid input */
11 while ( number < 0 ) {
12 printf( "Enter a positive integer: " );
13 scanf( "%d", &number );
14 } /* end while */
15
16 n = number;
17
18 /* compute factorial */
19 while ( n >= 0 ) {
20
21 if ( n == 0 ) {
22 factorial *= 1;
23 } /* end if */
24 else {
25 factorial *= n;
26 } /* end else */
27
28 --n;
29 } /* end while */
30
31 /* display factorial */
32 printf( "%d! is %d\n", number, factorial );
33
34 return 0; /* indicate successful termination */
35 } /* end main */

Enter a positive integer: 5


5! is 120

Enter a positive integer: 9


9! is 362880

Enter a positive integer: -8


Enter a positive integer: 0
0! is 1

b) Write a program that estimates the value of the mathematical constant e by using the
formula:
1 1 1
e = 1 + ----- + ----- + ----- + …
1! 2! 3!

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 92 Thursday, November 12, 2009 6:19 PM

92 Chapter 3 Structured Program Development in C: Solutions

ANS:

1 /* Exercise 3.47 Part B Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int n = 0; /* loop counter for accuracy */
7 int fact = 1; /* current n factorial */
8 int accuracy = 10; /* degree of accuracy */
9 float e = 0; /* current estimated value of e */
10
11 /* loop until degree of accuracy */
12 while ( n <= accuracy ) {
13
14 if ( n == 0 ) {
15 fact *= 1;
16 } /* end if */
17 else {
18 fact *= n;
19 } /* end else */
20
21 e += 1.0 / fact;
22 ++n;
23 } /* end while */
24
25 printf( "e is %f\n", e ); /* display estimated value */
26 return 0; /* indicate successful termination */
27 } /* end main */

e is 2.718282

c) Write a program that 2computes


3
the value of ex by using the formula
x x x x
e = 1 + ----- + ----- + ----- + …
1! 2! 3!
ANS:

1 /* Exercise 3.47 Part C Solution */


2 #include <stdio.h>
3
4 int main( void )
5 {
6 int n = 1; /* counter */
7 int accuracy = 15; /* degree of accuracy */
8 int x = 3; /* exponent */
9 float e = 1.0; /* e raised to the x power */
10 float numerator = 1.0; /* numerator of each term */
11 float denominator = 1.0; /* denominator of each term */
12
13 /* add together the number of terms specified by accuracy */
14 while ( n <= accuracy ) {
15 numerator *= x;

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
chtp6_03_IM.fm Page 93 Thursday, November 12, 2009 6:19 PM

Making a Difference 93

16 denominator *= n;
17 e += numerator / denominator;
18 ++n;
19 }
20
21 printf( "e raised to the %d power is %f\n", x, e ); /* display value */
22
23 return 0; /* indicate successful termination */
24
25 } /* end main */

e raised to the 3 power is 20.085534

Making a Difference
3.47 (World Population Growth Calculator) Use the web to determine the current world pop-
ulation and the annual world population growth rate. Write an application that inputs these values,
then displays the estimated world population after one, two, three, four and five years.
ANS: See the answer in the file WorldPop.c with this chapter’s solutions.
3.48 (Target-Heart-Rate Calculator) While exercising, you can use a heart-rate monitor to see
that your heart rate stays within a safe range suggested by your trainers and doctors. According to the
American Heart Association (AHA) (www.americanheart.org/presenter.jhtml?identifier=4736),
the formula for calculating your maximum heart rate in beats per minute is 220 minus your age in
years. Your target heart rate is a range that is 50–85% of your maximum heart rate. [Note: These for-
mulas are estimates provided by the AHA. Maximum and target heart rates may vary based on the
health, fitness and gender of the individual. Always consult a physician or qualified health care pro-
fessional before beginning or modifying an exercise program.] Create a program that reads the user’s
birthday and the current day (each consisting of the month, day and year). Your program should cal-
culate and display the person’s age (in years), the person’s maximum heart rate and the person’s target
heart rate range.
ANS: See the answer in the file TargetHeartRate.c with this chapter’s solutions.
3.49 (Enforcing Privacy with Cryptography) The explosive growth of Internet communications
and data storage on Internet-connected computers has greatly increased privacy concerns. The field
of cryptography is concerned with coding data to make it difficult (and hopefully—with the most
advanced schemes—impossible) for unauthorized users to read. In this exercise you’ll investigate a
simple scheme for encrypting and decrypting data. A company that wants to send data over the In-
ternet has asked you to write a program that will encrypt it so that it may be transmitted more se-
curely. All the data is transmitted as four-digit integers. Your application should read a four-digit
integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 7
to the digit and getting the remainder after dividing the new value by 10. Then swap the first digit
with the third, and swap the second digit with the fourth. Then print the encrypted integer. Write
a separate application that inputs an encrypted four-digit integer and decrypts it (by reversing the
encryption scheme) to form the original number. [Optional reading project: Research “public key
cryptography” in general and the PGP (Pretty Good Privacy) specific public key scheme. You may
also want to investigate the RSA scheme, which is widely used in industrial-strength applications.]
ANS: See the answer in the files Cryptography.c and ReverseCryptography.c with this
chapter’s solutions.

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

Vous aimerez peut-être aussi