-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 6.4 (Completed)
258 lines (240 loc) · 11.7 KB
/
Exercise 6.4 (Completed)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
1. (Practice) a. For the following section of code, determine the data type and scope of all
declared variables and symbolic constants on a separate sheet of paper, using the column headings
shown in the following chart. (The entries for the first variable have been filled in.)
Variable or Constant Name Data Type Scope
PRICE int global to main(), roi(), and step()
#include <iostream>
using namespace std;
------------------------------------------------------------------|
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); | | | | |
} | | | | |
-----------------------------------------------| | | | |
---------------------------------------------------| | | |
----------------------------------------------------------| | |
--------------------------------------------------------------| |
------------------------------------------------------------------|
--------------------------------------------------------------------------
Variable or Constant Name | Data Type | Scope
--------------------------------------------------------------------------
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. Draw a box around the appropriate section of the preceding code to enclose the scope of
each variable or constant.
Done!! See layout above chart
c. Determine the data type of the arguments that the roi() and step() functions expect and
the data type of the value these functions return.
roi() expects an integer and step() expects a double.
2. (Practice) a. For the following section of code, determine the data type and scope of all
declared variables on a separate sheet of paper, using the column headings shown in the following
chart. (The entries for the first variable have been filled in.)
Variable Name Data Type Scope
key char global to main(), func1(), and func2()
#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;
float q;
.
.
return p;
}
double func2(double first, double last)
{
int a,b,c,o,p;
double r;
double s,t,x;
.
.
return s * t;
}
--------------------------------------------------------------------------
Variable or Constant Name | Data Type | Scope
--------------------------------------------------------------------------
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. Draw a box around the appropriate section of the preceding code to enclose the scope of
each variable or constant.
#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; | | | |
float q; | | | |
. | | | |
. | | | |
. | | | |
return p; | | | |
} | | | |
-----------------------------------------------| | | |
double func2(double first, double last) | | |
-----------------------------------------------| | | |
{ | | | |
int a,b,c,o,p; | | | |
double r; | | | |
double s,t,x; | | | |
. | | | |
. | | | |
. | | | |
return s * t; | | | |
} | | | |
-----------------------------------------------| | | |
| | |
----------------------------------------------------------| | |
--------------------------------------------------------------| |
------------------------------------------------------------------|
c. Determine the data type of the arguments that the func1() and func2() functions expect
and the data type of the value these functions return.
The func1() data type is an integer and it returns an integer
The func2() data type is an double and it returns a double
3. (Practice) The term “scope” can also apply to a function’s parameters. What do you think is
the scope of all function parameters?
All function parameters have local scope in their defined function. Note that although function
parameters assume a value that depends on the calling function, these parameters can change
values in their functions. This makes them behave as though they were local variables in the
called function.
4. (Practice) Define the scope of the parameter p2 and the variables a, b, c, d, e, f, m, n, p, d, q,
and r in the following program structure:
#include <iostream>
using namespace std;
int a, b;
double One(float);
void Two(void);
int main()
{
int c, d;
double e, f;
.
.
return 0;
}
double One(double p2)
{
char m, n;
.
.
}
void Two(void)
{
int p, d;
double q, r;
.
.
}
---------------------------------------------------
Variable or Constant Name | Scope
---------------------------------------------------
p2 local to One()
a,b global to main(), One(), and Two()
c,d,e,f local to main()
m,n local to One()
p,d,q,r local to Two()
5. (Desk check) Determine the values displayed by each cout statement in the following
program:
#include <iostream>
using namespace std;
int firstnum = 10; // declare and initialize a global variable
void display(); // function prototype
int main()
{
int firstnum = 20; // declare and initialize a local variable
cout << “\nThe value of firstnum is “ << firstnum << endl; // displays 20
display();
return 0;
}
void display(void)
{
cout << “The value of firstnum is now “ << firstnum << endl; // displays 10
return;
}
The computer displays 20 as the original number and then the firstnum value is reassigned to 10.