Professional Documents
Culture Documents
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;
}
return;
}
return;
}
return;
}
cout << number << " raised to the " << pow << " power is: "
<< result << endl;
return;
}
num = 1;
while (num < 11)
{
cout << setw(3) << num << " "
<< setw(3) << num * num << " "
<< setw(4) << num * num * num <<endl;
num++; // increment num
}
return;
}
cout << "The sphere's volume is " << volume << endl;
return;
}
return;
}
Exercises 6.2
return key;
}
return;
}
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
fives = newAmount/5;
newAmount = newAmount - fives * 5;
ones = newAmount;
return;
}
secs = newTime / 1;
return;
}
6.
void yearCalc(long totDays, int& year, int& month, int& day)
{
long newDays;
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()
int main()
{
int bondtype;
double interest, coupon;
.
.
return 0;
}
int main()
{
int a,b,c;
double x,y;
.
.
return 0;
}
double secnum;
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().