You are on page 1of 14

Solution Manual for A Firstof C++, 4th Edition

To download the complete and accurate content document, go to:


https://testbankbell.com/download/solution-manual-for-a-first-book-of-c-4th-edition/
Solution Manual for A Firstof C++, 4th Edition

A First Book of C++, 4th Edition


Chapter 6 Solutions

Exercises 6.1

1.
a. Requires one int value
b. Requires three values in this order: an int and two doubles
c. Requires three values in this order: an int and two doubles
d. Requires three values in this order: a char and two floats
e. Requires two doubles
f. Requires six values in this order: two ints, two chars and two doubles
g. Requires four values in this order: two ints and two chars
2.
a.
void check(int num1, double num2, double num3)
{
cout << "In check()\n";
cout << "The value of num1 is " << num1 << endl;
cout << "The value of num2 is " << num2 << endl;
cout << "The value of num3 is " << num3 << endl;
return;
}

b. See solution file pgm6-1ex2b.cpp.


3.
a.
void findAbs(double number)
{
cout << "The absolute value of " << number << " is: ";
if(number < 0)
number = -number;
cout << number << endl;

return;
}

b. See solution file pgm6-1ex3b.cpp.


4.
a.
void mult(double num1, float num2)
{
cout << num1 << " * " << num2 << " = "

Visit TestBankBell.com to get complete for all chapters


<< (num1 * num2) << endl;

return;
}

b. See solution file pgm6-1ex4b.cpp.


5.
a.
void sqrIt(double number)
{
cout << "The square of " << number << " is: "
<< (number * number) << endl;

return;
}

b. See solution file pgm6-1ex5b.cpp.


6.
a.
void powfun(int number, int pow)
{
long result = 1;
int i;

for(i=0; i < pow; i++)


{
result = result * number;
}

cout << number << " raised to the " << pow << " power is: "
<< result << endl;

return;
}

b. See solution file pgm6-1ex6b.cpp.


7.
a.
void displaySquaresAndCubes()
{
int num;

cout << "NUMBER SQUARE CUBE\n"


<< "------ ------ ----\n";

num = 1;
while (num < 11)
{
cout << setw(3) << num << " "
<< setw(3) << num * num << " "
<< setw(4) << num * num * num <<endl;
num++; // increment num
}

return;
}

b. See solution file pgm6-1ex7b.cpp.


8.
a.
void selTab(int start, int num, int increment)
{
int i;

cout << "NUMBER SQUARE CUBE\n"


<< "------ ------ ----\n";

for(i = 0; i < num; i++, start+=increment)


{
cout << setw(3) << start << " "
<< setw(3) << start * start << " "
<< setw(4) << start * start * start <<"\n";
}
return;
}

b. See solution file pgm6-1ex8b.cpp.


9.
a.
int totsec(int hours, int minutes, int seconds)
{
int sec;
sec = 3600 * hours + 60 * minutes + seconds;
return sec;
}

b. See solution file pgm6-1ex9b.cpp


10.
a.
void spherevol(double radius)
{
double volume;
volume = 4 * 3.1416 * pow(radius,3)/3;

cout << "The sphere's volume is " << volume << endl;
return;
}

b. See solution file pgm6-1ex10b.


11.
a. See solution file pgm6-1ex11a.
b. See solution file pgm6-1ex11b.
12.
a.
void evenOdd(int num)
{
if(num % 2 == 0)
cout << num << “ is even” << endl;
else
cout << num << “ is odd” << endl;
return;
}

b. See solution file pgm6-1ex12b.cpp.


13. See solution file pgm6-1ex13.
14.
a.
template <class T> // template prefix
void display(T value) // header line
{
cout << "The argument passed is: " << value << endl;

return;
}

b. See solution file pgm6-14b.

Exercises 6.2

1. See solution file pgm6-2ex1.cpp.


2.
a. void check(int num1, float num2, double num3)
b. double findAbs(double x)
c. float mult(float first, float second)
d. int square(int number)
e. int powfun(int num, int exponent)
f. void table(void) or void table()
3.
a.
double rightTriangle(double a, double b)
{
return sqrt((a * a) + (b * b));
}

b. See solution file pgm6-2ex3b.cpp.


4.
a.
double findAbs(double number)
{
if(number < 0)
number = -number;
return number;
}

b. See solution file pgm6-2ex4b.cpp.


5.
a.
double cylvol(double radius, double length)
{
return 3.1416 * radius * radius * length;
}

b. See solution file pgm6-2ex5b.cpp.


6.
a.
double surfarea(double radius, double length)
{
return 3.1416 * 2 * radius * length;
}

b. See solution file pgm6-2ex6b.cpp.


7.
a.
double totamt(int quarters, int dimes, int nickels, int pennies)
{
double amount;
amount = 0.25*quarters + 0.10*dimes + 0.05*nickels + 0.01*pennies;
return amount;
}

b. See solution file pgm6-2ex7b.cpp.


8.
a.
int daycount(int month, int day, int year)
{
return ((year - 2000) * 365 + (month - 1) * 30 + day);
}

b. See solution file pgm6-2ex8b.cpp.


9.
a.
int convertdays(int month, int day, int year)
{
return (year * 10000 + month * 100 + day);
}

b. See solution file pgm6-2ex9b.cpp.


10.
a.
int readOneChar()
{
int key;

cout << "Press a key: ";


key = getch();

return key;
}

b. See solution file pgm6-2ex10b.cpp.


11. See solution file pgm6-2ex11.cpp.
12.
a.
int whole(double num) // header line
{
return int(num);
}

b. See solution file pgm6-2ex12b.cpp.


13.
a.
double fracpart(double x)
{
return (x - whole(x));
}

b. See solution file pgm6-2ex13b.cpp.


14.
a.
int leapyear(int year)
{
if ( (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) )
return 1;
else
return 0;
}

b. See solution file pgm6-2ex14b.cpp.


15.
a.
double polyTwo(double a, double b, double c, double x)
{
return (a * x * x + b * x + c);
}

b. See solution file pgm6-2ex15b.cpp.


16.
a.
void round(double num, int n)
{
double newNum;
int intPart;

newNum = num * (pow(10,n));


newNum = newNum + 0.5;
intPart = int(newNum);
newNum = double(intPart) / (pow(10,n));

cout << num << " rounded to " << n


<< " decimal places is: "
<< newNum << endl;

return;
}

b. See solution file pgm6-2ex16b.cpp.

Exercises 6.3

1.
a. doublet& amount
b. double& price
c. int& minutes
d. char& key
e. double& yield
2. void time(int& sec, int& min, int& hours)
3.
a.
void findMax(int x, int y, int& max)
{ // start of function body
int maxnum; // variable declaration

if (x >= y) // find the maximum number


maxnum = x;
else
maxnum = y;
max = maxnum;

return; // return statement


}

b. See solution file pgm6-3ex3.cpp.


4.
a.
void change(int amount, int& hundreds, int& fifties, int& twenties, int& tens,
int& fives, int& ones)
{
int newAmount;
hundreds = amount / 100;
newAmount = amount - hundreds * 100;

fifties = newAmount / 50;


newAmount = newAmount - fifties * 50;

twenties = newAmount / 20;


newAmount = newAmount - twenties * 20;

tens = newAmount / 10;


newAmount = newAmount - tens * 10;

fives = newAmount/5;
newAmount = newAmount - fives * 5;

ones = newAmount;

return;
}

b. See solution file pgm6-3ex4b.cpp.


5.
void time(int secsonds, int& hours, int& mins, int& secs)
{
int newTime;
hours = secsonds / 3600;
newTime = secsonds - hours * 3600;

mins = newTime / 60;


newTime = newTime - mins * 60;

secs = newTime / 1;

return;
}
6.
void yearCalc(long totDays, int& year, int& month, int& day)
{
long newDays;

year = (totDays / 365);


newDays = totDays - year * 365;
year += 1900; // to calculate the current year,
// not just the number of years

month = newDays / 30;


newDays = newDays - month * 30;
day = newDays / 1;
return;
}
7.
void liquid(long totCups, int& gallons, int& quarts, int& pints, int& cups)
{
long newCups;

gallons = totCups / 16;


newCups = totCups - gallons * 16;

quarts = newCups / 4;
newCups = newCups - quarts * 4;

pints = newCups / 2;
newCups = newCups - pints * 2;

cups = newCups / 1;

return;
}
8. In main(), the variables min and hour refer to integer quantities; in time(), the
variables min and hour refer to integers. Therefore, there are four distinct variables:
two known in main() and two known in time(). The variables in time() are, or
course, references to the variables in main() withg the same name. The compiler
keeps track of each variable with no confusion.

Exercises 6.4

1.
a.
Variable or Constant Data Type Scope
Name
PRICE int global to main(), roi(), and step()
YEARS long int global to main(), roi(), and step()
YIELD double global to main(), roi(), and step()
bondtype int local to main()
interest double local to main()
coupon double local to main()
mat1 int local to roi()
mat2 int local to roi()
count int local to roi()
effectiveRate double local to roi()
first double local to step()
last double local to step()
numofyrs int local to step()
fracpart double local to step()

b. See boxes around the following code:


const int PRICE;
const long YEARS;
const double YIELD;

int main()
{
int bondtype;
double interest, coupon;
.
.

return 0;
}

double roi(int mat1, int mat2)


{
int count;
double effectiveRate;
.
.
return(effectiveRate);
}

int step(double first, double last)


{
int numofyrs;
double fracpart;
.
.
return(10*numofyrs);
}

c. roi()—Expects two int values; returns a double value.


step()—Expects two double values; returns an int value.
2.
a.
Variable or Constant Data Type Scope
Name
KEY char global to main(), func1(), and
func2()
NUMBER long int global to main(), func1(), and
func2()
a, b, c int local to main()
x, y double local to main()
secnum double global to func1() and func2()
num1, num2 int local to func1()
o, p int local to func1()
q float local to func1()
first, last double local to func2()
a, b, c, o, p int local to func2()
r double local to func2()
s, t, x double local to func2()

b. The boxes appear below:


#include <iostream>
using namespace std;

const char KEY;


const long NUMBER;

int main()
{
int a,b,c;
double x,y;
.
.

return 0;
}

double secnum;

int func1(int num1, int num2)


{
int o,p;
double q;
.
.
return(p);
}

double func2(double first, double last)


{
int a,b,c,o,p;
float r;
double s,t,x;
.
.
return(s * t);
}

c. func1()—Expects two int values; returns an int value.


func2()—Expects two double values; returns a double value.
3. All function parameters have local scope in their defined functions. Note that although
function parameters assume a value that depends on the calling function, these
parameters can change values in their respective functions. This makes them behave as
though they’re local variables in the called function.
4.
Variable Name Data Type Scope
p2 double local to One()
a, b int global to main(), One(), and Two()
c, d int local to main()
e, f double local to main()
m, n char local to One()
p, d int local to Two()
q, r double local to Two()
5. The output of the program is as follows:
The value of firstnum is 20
The value of firstnum is now 10

Exercises 6.5

1.
a. The storage categories available to local variables are auto, static, and
register.
b. The storage categories available to global variables are extern and static. A
local auto variable is unique to the function in which it’s declared. Every time the
function is called, the auto variable is re-created, as though it never existed. A
local static variable is also unique to the function in which it’s declared.
However, a static variable retains its last value and isn’t re-created when its
function is called again.
2. The first function declares yrs to be a static variable and assigns a value of 1 to it
only once, when the function is compiled. Each time the function is called thereafter,
the value in yrs is increased by 2. The second function also declares yrs to be
static but assigns it the value 1 every time it’s called, and the value of yrs after the
function is finished will always be 3. By resetting the value of yrs to 1 each time it’s
called, the second function defeats the purpose of declaring the variable to be static.
3.
a. A static global variable is declared outside the body of a function (but not
necessarily at the top of the file) and can always be initialized. If it isn’t explicitly
initialized, the compiler initializes it to 0 or a null value (for characters or character
arrays). The definition of a static global variable also creates storage for the
variable. A global extern declaration doesn’t create storage for the variable and,
therefore, can never be used to initialize a variable. The extern declaration
simply alerts the compiler that the variable was created (defined) elsewhere.
Additionally, static global variables are private to the file in which they’re
defined. Therefore, static global variables can’t be made extern in another
file. Both static and extern declarations can be made in a function, in which
case they’re no longer global declarations.
Solution Manual for A Firstof C++, 4th Edition

b. The actual storage for the variable must be created somewhere else in the program,
using one, and only one, global declaration statement in which the keyword
extern isn’t used.
4. The location of the variable declaration determines its scope.
5.
a. extern char choice; Place at the top of file2.
b. extern int flag; Placed in average().
c. extern long date; Placed above average() but below roi().
d. extern long date; Placed in roi().
e. extend double coupon; Placed in roi().
f. extern char bondtype; Placed at the top of file1.
g. extern double maturity; Placed above watts() but below main().

Visit TestBankBell.com to get complete for all chapters

You might also like