Search for notes by fellow students, in your own course and all over the country.
Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.
Title: Solution - C How to Program chap 3 (7th Edition)
Description: Solution for chap 3 C How to Program (7th Edition)
Description: Solution for chap 3 C How to Program (7th Edition)
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
3
Structured Program
Development in C: Solutions
SOLUTIONS
3
...
and Pearson Education Inc
...
20 Structured Program Development in C: Solutions
Chapter 3
ANS:
while ( y > 0 ) {
printf( “%d\n”, y );
--y;
}
3
...
ANS: order
...
ANS: algorithm
c) A variable that accumulates the sum of several numbers is a
...
d) The process of setting certain variables to specific values at the beginning of a program is called
...
e) A special value used to indicate “end of data entry” is called a
, a
, a
or a
value
...
f) A
is a graphical representation of an algorithm
...
g) In a flowchart, the order in which the steps should be performed is indicated by
symbols
...
h) The termination symbol indicates the
and
of every algorithm
...
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
...
j) The item written inside a decision symbol is called a
...
3
...
h>
int main()
{
int x = 1, total = 0, y;
while ( x <= 10 ) {
y = x * x;
printf( "%d\n", y );
total += y;
++x;
}
printf("Total is %d\n", total);
return 0;
}
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
14
Write a single pseudocode statement that indicates each of the following:
a) Display the message "Enter two numbers"
...
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
...
else
do this
...
ANS: input s, input r, input t
3
...
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
...
Assume that
the user types the sentinel value -1 to indicate “end of data entry
...
16
State which of the following are true and which are false
...
a) Experience has shown that the most difficult part of solving a problem on a computer is producing a working C program
...
and Pearson Education Inc
...
22 Structured Program Development in C: Solutions
Chapter 3
ANS: False
...
b) A sentinel value must be a value that cannot be confused with a legitimate data value
...
c) Flowlines indicate the actions to be performed
...
Flowlines indicate the order in which steps are performed
...
e
...
ANS: False
...
e) In top-down, stepwise refinement, each refinement is a complete representation of the algorithm
...
For Exercises 3
...
21, perform each of these steps:
1
...
2
...
3
...
4
...
3
...
One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful
...
The program should calculate and display the miles per gallon obtained for each tankful
...
Here is a sample
input/output dialog:
...
8
Enter the miles driven: 287
The miles / gallon for this tank was 22
...
3
Enter the miles driven: 200
The miles / gallon for this tank was 19
...
000000
Enter the gallons used (-1 to end): -1
The overall average miles/gallon was 21
...
Keep
track of the total miles and the total gallons
...
Second refinement:
Initialize totalGallons to zero
...
Input the gallons used for the first tank
...
and Pearson Education Inc
...
Structured Program Development in C: Solutions 23
Chapter 3
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
...
17 Solution */
#include
0;
double totalMiles = 0
...
0 ) {
totalGallons += gallons; /* add current tank gallons to total */
printf( "Enter the miles driven: " ); /* get miles driven */
scanf( "%lf", &miles );
totalMiles += miles; /* add current tank miles to total */
/* display miles per gallon for current tank */
printf( "The Miles / Gallon for this tank was %f\n\n",
miles / gallons );
/* get next tank's gallons */
printf( "Enter the gallons used ( -1 to end ): " );
scanf( "%lf", &gallons );
} /* end while */
/* calculate average miles per gallon over all tanks */
totalAverage = totalMiles / totalGallons;
printf( "\nThe overall average Miles/Gallon was %f\n", totalAverage );
return 0; /* indicate successful termination */
} /* end main */
3
...
For each customer, the following facts are available:
1
...
Balance at the beginning of the month
3
...
Total of all credits applied to this customer's account this month
5
...
and Pearson Education Inc
...
24 Structured Program Development in C: Solutions
Chapter 3
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
...
” Here is a sample input/
output dialog:
Enter account number ( -1 to end): 100
Enter beginning balance: 5394
...
00
Enter total credits: 500
...
00
Account:
100
Credit limit: 5500
...
78
Credit Limit Exceeded
...
00
total charges: 123
...
00
credit limit: 1500
...
00
total charges: 274
...
00
credit limit: 800
...
First refinement:
Input the account number, beginning balance, total charges, total credits, and credit limit for a customer, calcu
late the customer’s new balance and determine if the balance exceeds the credit limit
...
Second refinement:
Input the first customer’s account number
...
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
18 Solution */
#include
2f\n%s%
...
" );
} /* end if */
/* prompt for next account */
printf( "\nEnter account number ( -1 to end ): " );
scanf( "%d", &accountNumber );
} /* end while */
return 0; /* indicate successful termination */
} /* end main */
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
19 One large chemical company pays its salespeople on a commission basis
...
For example, a salesperson who sells $5000 worth of chemicals in a week receives $200 plus 9% of
$5000, or a total of $650
...
Process one salesperson's figures at a time
...
00
Salary is: $650
...
56
Salary is: $311
...
89
Salary is: $298
...
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
...
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* Exercise 3
...
h>
int main()
{
double sales; /* gross weekly sales */
double wage; /* commissioned earnings */
/* get first sales */
printf( "Enter sales in dollars ( -1 to end): " );
scanf( "%lf", &sales );
/* loop until sentinel value read from user */
while ( sales != -1
...
0 + 0
...
2f\n\n", wage );
/* prompt for next sales */
printf( "Enter sales in dollars ( -1 to end ): " );
scanf( "%lf", &sales );
} /* end while */
return 0; /* indicate successful termination */
} /* end main */
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
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)
...
Here is a sample input/output dialog:
Enter loan principal ( -1 to end): 1000
...
1
Enter term of the loan in days: 365
The interest charge is $100
...
00
Enter interest rate:
...
40
Enter loan principal ( -1 to end ): 10000
...
09
Enter term of the loan in days: 1460
The interest charge is $3600
...
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
...
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
2
3
4
5
6
7
8
9
10
11
12
13
/* Exercise 3
...
h>
int main()
{
double principal;
double rate;
double interest;
int term;
/*
/*
/*
/*
loan principal */
interest rate */
interest charge */
length of loan in days */
/* get loan principal */
printf( "Enter loan principal ( -1 to end): " );
scanf( "%lf", &principal );
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
0 ) {
printf( "Enter interest rate: " ); /* get rate */
scanf( "%lf", &rate );
printf( "Enter term of the loan in days: " ); /* get term */
scanf( "%d", &term );
/* calculate interest charge */
interest = principal * rate * term / 365
...
2f\n\n", interest );
/* get next loan principal */
printf( "Enter loan principal ( -1 to end ): " );
scanf( "%lf", &principal );
} /* end while */
return 0; /* indicate successful termination */
} /* end main */
3
...
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
...
Your program should input this information for each employee, and should determine and display the employee's gross pay
...
00
Salary is $390
...
00
Enter number of hours worked ( -1 to end
Enter hourly rate of the worker ( $00
...
00
): 40
): 10
...
00
Salary is $415
...
00
Enter number of hours worked (
): -1
ANS:
2)
-1 to end
Top:
For an arbitrary number of employees, determine the gross pay for each employee
...
Second refinement:
Input the first employee’s number of hours worked
...
and Pearson Education Inc
...
Structured Program Development in C: Solutions 29
Chapter 3
3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* Exercise 3
...
h>
int main( void )
{
double hours; /* total hours worked */
double rate;
/* hourly pay rate */
double salary; /* gross pay */
/* get first employee's hours worked */
printf( "Enter number of hours worked (
scanf( "%lf", &hours );
-1 to end
): "
);
/* loop until sentinel value read from user */
while ( hours != -1
...
00
): "
);
/* if employee worked less than 40 hours */
if ( hours <= 40 ) {
salary = hours * rate;
} /* end if */
else { /* compute "time-and-a-half" pay */
salary = 40
...
0 ) * rate * 1
...
2lf\n\n", salary
/* prompt for next employee's data */
printf( "Enter number of hours worked (
scanf( "%lf", &hours );
} /* end while */
);
-1 to end
): "
);
return 0; /* indicate successful termination */
} /* end main */
3
...
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
/* Exercise 3
...
h>
int main()
{
int c; /* define c to use decrement operator */
c = 5;
printf( "%d\n", c );
printf( "%d\n", --c ); /* predecrement */
printf( "%d\n\n", c );
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
23 Write a program that utilizes looping to print the numbers from 1 to 10 side-by-side on the same line with 3 spaces between
each number
...
23 Solution */
#include
24 The process of finding the largest number (i
...
, the maximum of a group of numbers) is used frequently in computer applications
...
The salesperson who sells the most units wins the contest
...
[Hint: Your program should use three variables as follows]:
counter:
number:
largest:
A counter to count to 10 (i
...
, to keep track of how many numbers have
been input and to determine when all 10 numbers have been processed)
The current number input to the program
The largest number found so far
ANS:
Input the first number directly into the variable largest
Increment counter to 2
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
24 Solution */
#include
and Pearson Education Inc
...
32 Structured Program Development in C: Solutions
3
...
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
3
...
25 Solution */
#include
and Pearson Education Inc
...
Structured Program Development in C: Solutions 33
Chapter 3
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* Exercise 3
...
h>
int main()
{
int a = 3; /* counter */
/* display table headers */
printf( "A\tA+2\tA+4\tA+6\n\n" );
/* loop 5 times */
while ( a <= 15 ) {
/* calculate and display table values */
printf( "%d\t%d\t%d\t%d\n", a, a + 2, a + 4, a + 6 );
a += 3;
} /* end while */
return 0; /* indicate successful termination */
} /* end main */
3
...
24, find the two largest values of the 10 numbers
...
]
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* Exercise 3
...
h>
int main()
{
int
int
int
int
counter;
number;
largest;
secondLargest = 0;
/*
/*
/*
/*
counter for 10
current number
largest number
second largest
repetitions */
input */
found */
number found */
printf( "Enter the first number: " ); /* get first number */
scanf( "%d", &largest );
counter = 2;
/* loop 9 more times */
while ( counter <= 10 ) {
printf( "Enter next number: " ); /* prompt for next number */
scanf( "%d", &number );
/* if current number is greater than largest */
if ( number > largest ) {
/* update second largest with previous largest */
secondLargest = largest;
/* update largest with current number */
largest = number;
} /* end if */
else {
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
28 Modify the program in Figure 3
...
On any input, if the value entered is other than 1 or 2, keep looping
until the user enters a correct value
...
28 Solution */
#include
and Pearson Education Inc
...
Structured Program Development in C: Solutions 35
Chapter 3
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* if result 1, increment passes */
if ( result == 1 ) {
++passes;
} /* end if */
else { /* if result is not 1, increment failures */
++failures;
} /* end else */
++student;
} /* end while */
printf( "Passed %d\nFailed %d\n", passes, failures );
/* if more than eight students passed, print "raise tuition" */
if ( passes >= 8 ) {
printf( "Raise tuition\n" );
} /* end if */
return 0; /* indicate successful termination */
} /* end main */
Enter result (
Enter result (
Enter result (
Invalid result
Enter result (
Invalid result
Enter result (
Enter result (
Enter result (
Enter result (
Enter result (
Enter result (
Enter result (
Enter result (
Passed 6
Failed 4
3
...
h>
/* function main begins program execution */
int main()
{
int count = 1; /* initialize count */
while ( count <= 10 ) { /* loop 10 times */
/* output line of text */
printf( "%s\n", count % 2 ? "****" : "++++++++" );
count++; /* increment count */
} /* end while */
return 0; /* indicate program ended successfully */
} /* end function main */
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
What does the following program print?
#include
and Pearson Education Inc
...
Chapter 3
Structured Program Development in C: Solutions 37
Chapter 3
3
...
Note that the compiler ignores the indentation in a C program
...
Because, on first glance, the programmer may not be sure which if an else matches, this is referred to as the “dangling else” problem
...
[Hint: Apply indentation conventions you have learned
...
32 (Another Dangling Else Problem) Modify the following code to produce the output shown
...
You might not make any changes other than inserting braces
...
We have eliminated
the indentation from the following code to make the problem more challenging
...
]
if ( y == 8 )
if ( x == 5 )
printf( "@@@@@\n"
else
printf( "#####\n"
printf( "$$$$$\n"
printf( "&&&&&\n"
);
);
);
);
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
@@@@@
$$$$$
&&&&&
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” );
}
printf( “&&&&&\n” );
d) Assuming x = 5 and y = 7, the following output is produced
...
#####
$$$$$
&&&&&
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
33 Write a program that reads in the side of a square and then prints that square out of asterisks
...
For example, if your program reads a size of 4, it should print
****
****
****
****
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* Exercise 3
...
h>
int main()
{
int side;
/* side counter */
int temp;
/* temporary integer */
int asterisk; /* asterisk counter */
printf( "Enter the square side: " ); /* get size of square */
scanf( "%d", &side );
temp = side;
/* loop through rows of square */
while ( side-- > 0 ) {
asterisk = temp;
/* loop through columns of square */
while ( asterisk-- > 0 ) {
printf( "*" );
} /* end inner while */
putchar( '\n' );
} /* end outer while */
return 0; /* indicate successful termination */
} /* end main */
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
34 Modify the program you wrote in Exercise 3
...
For example, if your program reads a size
of 5, it should print
*****
*
*
*
*
*
*
*****
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* Exercise 3
...
h>
int main()
{
int side;
/* side counter */
int rowPosition; /* row counter */
int size;
/* length of side */
printf( "Enter the square side: " ); /* prompt for side length */
scanf( "%d", &side );
size = side; /* set size counter to length of side */
/* loop side number of times */
while ( side > 0 ) {
rowPosition = size; /* set row counter to length of size */
/* loop rowPosition number of times */
while ( rowPosition > 0 ) {
/* if side or row counter is 1 or size print an '*' */
if ( size == side ) {
printf( "*" );
} /* end if */
else if ( side == 1 ) {
printf( "*" );
} /* end else if */
else if ( rowPosition == 1 ) {
printf( "*" );
} /* end else if */
else if ( rowPosition == size ) {
printf( "*" );
} /* end else if */
else { /* otherwise, print a space */
printf( " " );
} /* end else */
--rowPosition; /* decrement row counter */
} /* end inner while */
printf( "\n" ); /* new line for next row */
--side; /* decrement side counter */
} /* end outer while */
return 0; /* indicate successful termination */
} /* end main */
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
35 A palindrome is a number or a text phrase that reads the same backwards as forwards
...
Write a program that reads in a five-digit integer and determines whether or not it is a palindrome
...
]
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* Exercise 3
...
h>
int main()
{
int number;
int temp1;
int temp2;
int firstDigit;
int secondDigit;
int fourthDigit;
int fifthDigit;
/*
/*
/*
/*
/*
/*
/*
input number */
first temporary integer */
second temporary integer */
first digit of input */
second digit of input */
fourth digit of input */
fifth digit of input */
printf( "Enter a five-digit number: " ); /* get number */
scanf( "%d", &number );
temp1 = number;
/* determine first digit by integer division by 10000 */
firstDigit = temp1 / 10000;
temp2 = temp1 % 10000;
/* determine second digit by integer division by 1000 */
secondDigit = temp2 / 1000;
temp1 = temp2 % 1000;
temp2 = temp1 % 100;
/* determine fourth digit by integer division by 10 */
fourthDigit = temp2 / 10;
temp1 = temp2 % 10;
fifthDigit = temp1;
/* if first and fifth digits are equal */
if ( firstDigit == fifthDigit ) {
/* if second and fourth digits are equal */
if ( secondDigit == fourthDigit ) {
/* number is a palindrome */
printf( "%d is a palindrome\n", number );
} /* end if */
else { /* number is not a palindrome */
printf( "%d is not a palindrome\n", number );
} /* end else */
} /* end if */
else { /* number is not a palindrome */
printf( "%d is not a palindrome\n", number );
} /* end else */
return 0; /* indicate successful termination */
} /* end main */
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
36 Input an integer containing only 0s and 1s (i
...
, a “binary” integer) and print its decimal equivalent
...
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, etc
...
Thus the decimal number 234 can be interpreted as 4 * 1 + 3 * 10 + 2 * 100
...
]
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* Exercise 3
...
h>
int main()
{
int binary;
int number;
int decimal = 0;
int highBit = 16;
int factor = 10000;
/*
/*
/*
/*
/*
current value of binary number */
input binary number */
current value of decimal number */
value of highest bit */
factor of 10 to pick off digits */
/* prompt for binary input */
printf( "Enter a binary number ( 5 digits maximum ): " );
scanf( "%d", &binary );
number = binary; /* save in number for final display */
/* loop 5 times using powers of 2 */
while ( highBit >= 1 ) {
/* update decimal value with decimal value corresponding
to current highest binary bit */
decimal += binary / factor * highBit;
/* reduce high bit by factor of 2, i
...
,
move one bit to the right */
highBit /= 2;
/* reduce binary number to eliminate current highest bit */
binary %= factor;
/* reduce factor by power of 10, i
...
,
move one bit to the right */
factor /= 10;
} /* end while */
/* display decimal value */
printf( "The decimal equivalent of %d is %d\n", number, decimal );
return 0; /* indicate successful termination */
} /* end main */
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
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
...
Use your watch to
time how long each million repetitions of the loop takes
...
37 Solution */
#include
38 Write a program that prints 100 asterisks, one at a time
...
(Hint: Count from 1 to 100
...
)
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* Exercise 3
...
h>
int main()
{
int count = 0; /* counter */
/* loop to 100 */
while( ++count <= 100 )
/* print a new line after every 10th asterisk */
count % 10 == 0 ? printf( "*\n" ) : printf( "*" );
return 0; /* indicate successful termination */
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
39
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Write a program that reads an integer and determines and prints how many digits in the integer are 7s
...
39 Solution */
#include
and Pearson Education Inc
...
Chapter 3
Structured Program Development in C: Solutions 45
Chapter 3
Enter a 5-digit number: 11727
The number 11727 has 2 seven(s) in it
3
...
40 Solution */
#include
and Pearson Education Inc
...
46 Structured Program Development in C: Solutions
Chapter 3
3
...
Your loop should not
terminate (i
...
, you should create an infinite loop)
...
e
...
On a 4-byte system, the largest integer value is 2147483647 and anything above
that is represented by a negative number, which fails the loop continuation test)
...
41 Solution */
#include
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
...
14159 for π
...
and Pearson Education Inc
...
Structured Program Development in C: Solutions 47
Chapter 3
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* Exercise 3
...
h>
int main()
{
float radius;
/* input radius */
float pi = 3
...
2f\n", radius * 2 );
/* compute and display circumference */
printf( "The circumference is %
...
2f\n", pi * radius * radius );
return 0; /* indicate successful termination */
} /* end main */
Enter the radius: 4
...
40
The circumference is 29
...
40
3
...
printf( "%d", ++( x + y ) );
ANS: printf( “%d”, 1 + x + y );
3
...
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* Exercise 3
...
h>
int main()
{
double a; /* first number */
double b; /* second number */
double c; /* third number */
/* input 3 numbers */
printf( "Enter three doubleing point numbers: " );
scanf( "%lf%lf%lf", &a, &b, &c);
/* use Pythagorean Theorem */
if ( c * c == a * a + b * b ) {
printf( "The three numbers could be sides of a triangle\n" );
} /* end if */
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
7 3
...
2
The three numbers probably are not the sides of a triangle
Enter three doubleing point numbers: 3
...
0 5
...
45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Write a program that reads three nonzero integers and determines and prints if they could be the sides of a right triangle
...
45 Solution */
#include
and Pearson Education Inc
...
Structured Program Development in C: Solutions 49
Chapter 3
3
...
All of their
data is transmitted as four-digit integers
...
Your program should read a four-digit integer and encrypt it as follows: Replace each digit by the remainder
after (the sum of that digit plus 7) is divided by 10
...
Then print the encrypted integer
...
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* Exercise 3
...
h>
int main()
{
int first; /* first digit replacement */
int second; /* second digit replacement */
int third; /* third digit replacement */
int fourth; /* fourth digit replacement */
int digit; /* input number */
int temp1; /* temporarily hold digit */
int temp2; /* temporarily hold digit */
int encryptedNumber; /* resulting encrypted number */
/* prompt for input */
printf( "Enter a four digit number to be encrypted: " );
scanf( "%d", &digit );
temp1 = digit;
/* retrieve each digit and replace with
(sum of digit and 7) mod 10 */
first = ( temp1 / 1000 + 7 ) % 10;
temp2 = temp1 % 1000;
second = ( temp2 / 100 + 7 ) % 10;
temp1 = temp2 % 100;
third = ( temp1 / 10 + 7 ) % 10;
temp2 = temp1 % 10;
fourth = ( temp2 + 7 ) % 10;
/* swap
temp1 =
first =
third =
first and third */
first;
third * 1000; /* multiply by 1000 for 1st digit component */
temp1 * 10; /* multiply by 10 for 3rd digit component */
/* swap second and fourth */
temp1 = second;
second = fourth * 100; /* multiply by 100 for 2nd digit component */
fourth = temp1 * 1;
/* add components to obtain encrypted number */
encryptedNumber = first + second + third + fourth;
/* display encrypted number */
printf( "Encrypted number is %d\n", encryptedNumber );
return 0; /* indicate successful termination */
} /* end main */
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
...
46 Part B Solution */
#include
and Pearson Education Inc
...
Chapter 3
Structured Program Development in C: Solutions 51
Chapter 3
1
...
For example, 5! = 5 · 4 · 3 · 2 · 1, which is 120
...
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* Exercise 3
...
h>
int main()
{
int n;
/* current multiplication factor */
int number = -1;
/* input number */
unsigned factorial = 1; /* resulting factorial */
/* loop until valid input */
do {
printf( "Enter a positive integer: " );
scanf( "%d", &number );
} while ( number < 0 ); /* end do
...
and Pearson Education Inc
...
52 Structured Program Development in C: Solutions
Chapter 3
b) Write a program that estimates the value of the mathematical constant e by using the formula:
1- 1- 1e = 1 + ---- + ---- + ---- + …
1! 2! 3!
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* Exercise 3
...
h>
int main()
{
int n = 0;
/*
int fact = 1;
/*
int accuracy = 10; /*
double e = 0;
/*
loop counter for accuracy */
current n factorial */
degree of accuracy */
current estimated value of e */
/* loop until degree of accuracy */
while( n <= accuracy ) {
if ( n == 0 ) {
fact *= 1;
} /* end if */
else {
fact *= n;
} /* end else */
e += 1
...
718282
c) Write a program that computes the value of ex by using the formula
2
3
x
x- x
e = 1 + ---- + ---- + x - + …
- ---1! 2! 3!
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* Exercise 3
...
h>
int main()
{
int n = 0;
int accuracy = 15;
int x = 3;
int times = 0;
int count;
double e = 1
...
0;
double fact = 1
...
and Pearson Education Inc
...
Structured Program Development in C: Solutions 53
Chapter 3
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* loop while less than degree of accuracy */
while( n <= accuracy ) {
count = n;
/* update n! */
if ( n == 0 ) {
fact *= 1
...
0;
exp *= x;
} /* end if */
else {
exp *= x;
} /* end else */
++times;
} /* end while */
e += exp / fact; /* update e raised to the x power */
++n;
} /* end while */
/* display result */
printf( "e raised to the %d power is %f\n", x, e );
return 0; /* indicate successful termination */
} /* end main */
e raised to the 3 power is 20
...
and Pearson Education Inc
...
54 Structured Program Development in C: Solutions
© Copyright 1992–2004 by Deitel & Associates, Inc
...
All Rights Reserved
Title: Solution - C How to Program chap 3 (7th Edition)
Description: Solution for chap 3 C How to Program (7th Edition)
Description: Solution for chap 3 C How to Program (7th Edition)