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 2 (7th Edition)
Description: Solution for chapter 2 of C How to Program.
Description: Solution for chapter 2 of C How to Program.
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
2
What’s in a name?
That which we call a rose
By any other name would
smell as sweet
...
“I’ve had nothing yet,”
Alice replied in an offended
tone: “so I can’t take more
...
”
—Lewis Carroll
High thoughts must have high
language
...
Use simple input and output
statements
...
Learn computer memory
concepts
...
Learn the precedence of
arithmetic operators
...
Introduction to C
Programming—Solutions
Self-Review Exercises
2
Self-Review Exercises
2
...
2
Fill in the blanks in each of the following
...
a) Every C program begins execution at the function
ANS: main
...
b) Every function’s body begins with
ANS: left brace, right brace
...
c) Every statement ends with a(n)
ANS: semicolon
...
d) The
ANS: printf
...
ANS: newline
...
f) The
ANS: scanf
...
ANS: %d
...
value in that location
...
i) When a value is read from a memory location, the value in that location is preserved;
...
statement is used to make decisions
...
State whether each of the following is true or false
...
a) Function printf always begins printing at the beginning of a new line
...
Function printf always begins printing where the cursor is positioned,
and this may be anywhere on a line of the screen
...
ANS: False
...
They’re used to document programs and improve their readability
...
ANS: True
...
ANS: True
...
ANS: True
...
ANS: False
...
g) Definitions can appear anywhere in the body of a function
...
A variable’s definition must appear before its first use in the code
...
Later in the book we’ll discuss this in more depth as we
encounter additional C features that can affect this issue
...
3
Chapter 2
Introduction to C Programming—Solutions
h) All arguments following the format control string in a printf function must be preceded by an ampersand (&)
...
Arguments in a printf function ordinarily should not be preceded by an ampersand
...
We will discuss exceptions to these rules in
Chapter 6 and Chapter 7
...
ANS: True
...
ANS: False
...
k) A program that prints three lines of output must contain three printf statements
...
A printf statement with multiple \n escape sequences can print several lines
...
ANS: int c, thisVariable, q76354, number;
b) Prompt the user to enter an integer
...
ANS: printf( "Enter an integer: " );
c) Read an integer from the keyboard and store the value entered in integer variable a
...
"
ANS: if ( number != 7 ) {
printf( "The variable number is not equal to 7
...
" on one line
...
\n" );
f) Print the message "This is a C program
...
ANS: printf( "This is a C\nprogram
...
" with each word on a separate line
...
\n" );
h) Print the message "This is a C program
...
ANS: printf( "This\tis\ta\tC\tprogram
...
4
Write a statement (or comment) to accomplish each of the following:
a) State that a program will calculate the product of three integers
...
ANS: int x, y, z, result;
c) Prompt the user to enter three integers
...
ANS: scanf( "%d%d%d", &x, &y, &z );
e) Compute the product of the three integers contained in variables x, y and z, and assign
the result to the variable result
...
ANS: printf( "The product is %d\n", result );
Exercises
4
2
...
4, write a complete program that calculates
the product of three integers
...
6
// Calculate the product of three integers
#include
Correction: Eliminate the &
...
b) scanf( "%d%d", &number1, number2 );
ANS: Error: number2 does not have an ampersand
...
Later in the text we discuss exceptions to this
...
Correction: Remove the semicolon after the right parenthesis
...
The semicolon after the right parenthesis is considered an
empty statement—a statement that does nothing
...
Exercises
2
...
(Note: There may be
more than one error per statement
...
8
Fill in the blanks in each of the following:
are used to document a program and improve its readability
...
...
...
d) Calculations are normally performed by
statements
...
function inputs values from the keyboard
...
2
...
”
ANS: printf( “Enter two numbers\n” );
b) Assign the product of variables b and c to variable a
...
e
...
ANS: // Sample payroll calculation program
2
...
11
d) Input three integer values from the keyboard and place these values in integer variables
a, b and c
...
If false, explain your answer
...
ANS: False
...
b) The following are all valid variable names: _under_bar_, m928134, t5, j7, her_sales,
his_account_total, a, b, c, z, z2
...
c) The statement printf("a = 5;"); is a typical example of an assignment statement
...
The statement prints a = 5; on the screen
...
ANS: False
...
e) The following are all invalid variable names: 3g, 87, 67h2, h22, 2h
...
Only those beginning with a number are invalid
...
ANS: division, modulus
...
expression?
ANS: The innermost pair of parentheses
...
throughout the execution of a program is called a
ANS: variable
...
12 What, if anything, prints when each of the following statements is performed? If nothing
prints, then answer “Nothing
...
a) printf( "%d", x );
ANS: 2
b)
printf( "%d", x + x );
ANS: 4
c)
printf( "x=" );
ANS: x=
d)
printf( "x=%d", x );
e)
printf( "%d = %d", x + y, y + x );
ANS: x=2
ANS: 5 = 5
f)
z = x + y;
ANS: Nothing
...
g) scanf( "%d%d", &x, &y );
ANS: Nothing
...
h)
// printf( "x + y = %d", x + y );
ANS: Nothing
...
i)
printf( "\n" );
ANS: A newline character is printed, and the cursor is positioned at the beginning of the
next line on the screen
...
13 Which, if any, of the following C statements contain variables whose values are replaced?
a) scanf( "%d%d%d%d%d", &b, &c, &d, &e, &f );
b) p = i + j + k + 7;
c) printf( "%s", Values are replaced
...
2
...
2
...
a) x = 7 + 3 * 6 / 2 - 1;
ANS: * is first, / is second, + is third, - is fourth and = is last
...
b) x = 2 % 2 + 2 * 2 - 2 / 2;
ANS: % is first, * is second, / is third, + is fourth, - is fifth and = is last
...
7
Chapter 2
Introduction to C Programming—Solutions
c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );
ANS:
5
6
4
2
3
1
...
Value of x is 324
...
16 Write a program that asks the user to enter two numbers, obtains the two numbers from
the user and prints the sum, product, difference, quotient and remainder of the two numbers
...
16 Solution
#include
17 Write a program that prints the numbers 1 to 4 on the same line
...
a) Using one printf statement with no conversion specifiers
...
c) Using four printf statements
...
17 Solution
#include
18 Write a program that asks the user to enter two integers, obtains the numbers from the user,
then prints the larger number followed by the words “is larger
...
” Use only the single-selection form of the if statement you
learned in this chapter
...
18 Solution
#include
19 Write a program that inputs three different integers from the keyboard, then prints the sum,
the average, the product, the smallest and the largest of these numbers
...
The screen dialog should appear as follows:
Input three different integers: 13 27 14
Sum is 54
Average is 18
Product is 4914
Smallest is 13
Largest is 27
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 2
...
h>
int main( void )
{
int
int
int
int
int
a; // define first integer
b; // define second integer
c; // define third integer
smallest; // smallest integer
largest; // largest integer
printf( "%s", "Input three different integers: " ); // prompt user
scanf( "%d%d%d", &a, &b, &c ); // read three integers
// output sum, average and
printf( "Sum is %d\n", a +
printf( "Average is %d\n",
printf( "Product is %d\n",
product of the three integers
b + c );
( a + b + c ) / 3 );
a * b * c );
smallest = a; // assume first number is the smallest
if ( b < smallest ) { // is b smaller?
smallest = b;
} // end if
if ( c < smallest ) { // is c smaller?
smallest = c;
} // end if
printf( "Smallest is %d\n", smallest );
largest = a; // assume first number is the largest
if ( b > largest ) { // is b larger?
largest = b;
} // end if
if ( c > largest ) { // is c larger?
largest = c;
} // end if
printf( "Largest is %d\n", largest );
Exercises
43
10
} // end main
2
...
Use the constant value 3
...
Perform each of these calculations inside the
printf statement(s) and use the conversion specifier %f
...
In Chapter 3 we will discuss floating-point numbers, i
...
, values that can have decimal points
...
20 Solution
#include
14159 * radius );
printf( "The area is %f\n", 3
...
548620
The area is 254
...
21
Write a program that prints a box, an oval, an arrow and a diamond as follows:
*********
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*********
*
*
*
*
*
*
*
***
***
*
*
*
*
*
*
*
*
***
*****
*
*
*
*
*
*
*
* *
*
*
*
*
*
*
*
*
*
*
* *
*
ANS:
1
2
3
4
5
6
7
// Exercise 2
...
h>
int main( void )
{
printf( "%s", "*********
printf( "%s", "*
*
***
*
*
*
***
*\n" );
* *\n" );
11
8
9
10
11
12
13
14
15
Chapter 2
Introduction to C Programming—Solutions
printf( "%s",
printf( "%s",
printf( "%s",
printf( "%s",
printf( "%s",
printf( "%s",
printf( "%s",
} // end main
2
...
23 Write a program that reads in three integers and then determines and prints the largest and
the smallest integers in the group
...
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
// Exercise 2
...
h>
int main( void )
{
int largest; // largest integer
int smallest; // smallest integer
int int1; // define int1 for user input
int int2; // define int2 for user input
int int3; // define int3 for user input
int temp; // temporary integer for swapping
printf( "%s", "Input 3 integers: " ); // prompt user and read 3 ints
scanf( "%d%d%d%d%d", &largest, &smallest, &int1, &int2, &int3 );
if ( smallest > largest ) { // make comparisons
temp = largest;
largest = smallest;
smallest = temp;
} // end if
if ( int1 > largest ) {
largest = int1;
} // end if
if ( int1 < smallest ) {
smallest = int1;
} // end if
Exercises
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
12
if ( int2 > largest ) {
largest = int2;
} // end if
if ( int2 < smallest ) {
smallest = int2;
} // end if
if ( int3 > largest ) {
largest = int3;
} // end if
if ( int3 < smallest ) {
smallest = int3;
} // end if
printf( "The largest value is %d\n", largest );
printf( "The smallest value is %d\n", smallest );
} // end main
Input 5 integers: 9 4 5 8 7
The largest value is 9
The smallest value is 4
2
...
[Hint: Use the remainder operator
...
Any multiple of two leaves
a remainder of zero when divided by 2
...
24 Solution
#include
25 Print your initials in block letters down the page
...
PPPPPPPPP
P
P
P
P
P
P
P P
J
JJ
J
J
JJJJJJJ
DDDDDDDDD
D
D
D
D
D
D
DDDDD
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Exercise 2
...
h>
int main( void )
{
puts( "PPPPPPPPP" );
puts( "
P
P" );
puts( "
P
P" );
puts( "
P
P" );
puts( "
P P\n" );
puts( " JJ" );
puts( " J" );
puts( "J" );
puts( " J" );
puts( " JJJJJJJ\n" );
puts( "DDDDDDDDD" );
puts( "D
D" );
puts( "D
D" );
puts( " D
D" );
puts( " DDDDD" );
} // end main
2
...
[Hint: Use the remainder operator
...
26 Solution
Exercises
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
14
#include
27 Display the following checkerboard pattern with eight printf statements and then display
the same pattern with as few printf statements as possible
...
27 Solution
#include
28 Distinguish between the terms fatal error and nonfatal error
...
A nonfatal error occurs
when the logic of the program is incorrect, and the program does not work properly
...
A fatal error immediately lets you
know there is a problem with the program, whereas a nonfatal error can be subtle and
possibly go undetected
...
29 Here’s a peek ahead
...
C can also
represent uppercase letters, lowercase letters and a considerable variety of special symbols
...
The set of characters a computer uses
together with the corresponding integer representations for those characters is called that computer’s character set
...
As a minimum, determine the integer equivalents of the following:
A B C a b c 0 1 2 $ * + / and the blank character
...
29 Solution
#include
30 Write a program that inputs one five-digit number, separates the number into its individual
digits and prints the digits separated from one another by three spaces each
...
] For example, if the user types in 42139, the
program should print
4
2
1
3
9
ANS:
1
2
3
4
5
6
7
8
9
10
11
// Exercise 2
...
h>
int main( void )
{
int number; // number input by user
int temp; // temporary integer
printf( "%s", "Enter a five-digit number: " ); // prompt user
scanf( "%d", &number ); // read integer
17
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Chapter 2
Introduction to C Programming—Solutions
printf( "%d ", number / 10000 ); // print leftmost digit
temp = number % 10000;
printf( " %d ", temp / 1000 );
temp = temp % 1000;
printf( " %d ", temp / 100 );
temp = temp % 100;
printf( " %d ", temp / 10 );
temp = temp % 10;
printf( " %d\n", temp ); // print right-most digit
} // end main
Enter a five-digit number: 23456
2
3
4
5
6
2
...
31 Solution
#include
32 (Body Mass Index Calculator) We introduced the body mass index (BMI) calculator in
Exercise 1
...
The formulas for calculating BMI are
weightInPounds × 703
BMI = -----------------------------------------------------------------------------------heightInInches × heightInInches
or
weightInKi log rams
BMI = -------------------------------------------------------------------------------------heightInMeters × heightInMeters
Create a BMI calculator application that reads the user’s weight in pounds and height in inches
(or, if you prefer, the user’s weight in kilograms and height in meters), then calculates and displays
the user’s body mass index
...
5
between 18
...
9
between 25 and 29
...
The BMI calculations when done with int values will both produce whole-number results
...
When the BMI calculations
are performed with doubles, they’ll both produce numbers with decimal points—these are called
“floating-point” numbers
...
32 Solution: BMI
...
h>
//function main begins program execution
int main ( void )
{
int weight; // weight of the person
int height; // height of the person
int BMI; // user's BMI
// get user's height
printf( "%s", "Please enter your height (in inches): " );
scanf( "%d", &height );
// get user's weight
printf( "Please enter your weight (in pounds): " );
scanf( "%d", &weight );
BMI = weight * 703 / ( height * height ); // calculate BMI
printf( "Your BMI is %d\n\n", BMI ); // output BMI
// output data to user
puts( "BMI VALUES" );
puts( "Underweight:\tless than 18
...
5 and 24
...
9" );
puts( "Obese:\t\t30 or greater" );
} // end main
Making a Difference
20
Please enter your height (in inches): 69
Please enter your weight (in pounds): 155
Your BMI is 22
BMI VALUES
Underweight:
Normal:
Overweight:
Obese:
less than 18
...
5 and 24
...
9
30 or greater
2
...
Create an application
that calculates your daily driving cost, so that you can estimate how much money could be saved by
car pooling, which also has other advantages such as reducing carbon emissions and reducing traffic
congestion
...
b) Cost per gallon of gasoline
...
d) Parking fees per day
...
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
// Exercise 2
...
h>
// function main begins program execution
int main ( void )
{
int miles; // total miles driven per day
int gasCost; // cost per gallon of gasoline
int mpg; // average miles per gallon
int parkFee; // parking fees per day
int tolls; // tolls per day
int total; // total cost
// get total miles driven
printf( "%s", "Please enter the total miles driven per day: " );
scanf( "%d", &miles );
// get cost of gas
printf( "%s", "Please enter the cost per gallon of gasoline: " );
scanf( "%d", &gasCost );
// get average miles per gallon
printf( "%s", "Please enter average miles per gallon: " );
scanf( "%d", &mpg );
// get parking fees per day
printf( "%s", "Please enter the parking fees per day: " );
scanf( "%d", &parkFee );
// get cost of tolls per day
printf( "%s", "Please enter the tolls per day: " );
21
33
34
35
36
37
38
39
Chapter 2
Introduction to C Programming—Solutions
scanf( "%d", &tolls );
// calculate total cost
total = tolls + parkFee + ( miles / mpg ) * gasCost;
printf( "Your daily cost of driving to work is $%d\n", total );
} // end main
Please enter the total miles driven per day: 100
Please enter the cost per gallon of gasoline: 3
Please enter average miles per gallon: 19
Please enter the parking fees per day: 3
Please enter the tolls per day: 4
Your daily cost of driving to work is $22
Title: Solution - C How to Program chap 2 (7th Edition)
Description: Solution for chapter 2 of C How to Program.
Description: Solution for chapter 2 of C How to Program.