Lab 10-2.pdf
347
Use the program to calculate the diameter for a circle whose radius is 25.7. Thediameter is 51.4-
Change the getArea function to a yalue_returning function. Save and then run the pro-gram. Use the program to calculate the area of a circle whose *afw is 5.5. Then use it tocalculate the diameter of a circle whose radius is 25.7.
Passing Variables to a Function
In this lab, you will plan and create an algorithm for Patterson Windows. Theproblem specification and sample calcuiations are shown in Figure i0-18.
A
Create a program for patterson Windows, a compa ny that sells energy-effi cient replacementwindows for homes. The pr0gram should display the total amount a customer owes , given thenumber of windows ordered and the price per window The total owed is calculated by multiplyi ngthe number of windows ordered by the price per window. However , several times during the year,the company has a B0G0 (buy one, get one free) offe
,Exaqrole usins regulryicing
Number of windows: 11
Price per window: 300
Total owed XXXXXXXXXX): S330O.OO
,Exar,nole uling BOGO p cing
Number of windows: 15
Price per window: 200
Totalowed XXXXXXXXXX): XXXXXXXXXX
Figure 10-18 Problem specification and a sample calculation for Lab l0-2
First, analyze the problem, iooking for the output first and then for the input. in this case, theerogram needs to display the total amount the customer owes. io caiculate that amount, thecomputer will need to know the number of windows o.a"."d, the price per window and the
icing option (either regular or BOGO).
,\ftel.anallzing the problem, you plan the algorithm. In this case, in addition to the mai n:unction, the program will use three void functions named dispiayOptions, getRegular,and getBoGo. The di spl ayOpti ons function will display tfre piiclg options on the compure
:.J;,il ^
void function is appropriate for this task because the function will not need to return
- he getRegul ar function will cajculate the total owed using the regular pricing option. TheletBoco function, on the other hand, will calculate the totaiow"a uring the BOGO pricingrption. The getRegul ar and getBoCo functions could be coded as either value-returning or,'oid functions. For this lab, you will use void functions. (you ;ill .l"rg" the functions to yalue_::lurning functions in Lab 10-3.)
t
LAB 10-2 PIan and Create
I3.,
Void Functions
To calculate the total owed, the getRegul ar and getBoGo functions will need to know the
number of windows ordered and the price per window' The calling statement will pass that
information by valueto the functions' Because both functions will be void functions' they
will
also need the talling statement to pass them the address of a variable in which to store the
caiculated results. Figure 10-19 shows the completed IPO charts for the
program's four functions'
main function
lnput
prLcLwg oVtLow
wttwtber of wLwdows
wLwd'ow VrLce
Processing
? r C cess,,'r'l i.--av/"s :',t"0 v'a
OuQut
totaL owed
ALgorLthvw:
t. caLLthe dLsyLagopttows fuwcLLowLo dLsTLag
theprLcLwg oYtLows
2. qet the prLotwg oTtLow
=. Lf (the yrLcLwg optLow
Ls eLther t- or z)
get the wtweber of wLwdows awd the wLwdow TrLce
L{ (the prLcLwg aPLLow Ls t-)
caLL the gelRtgvtLar fvtwclLow to caLovrLate
the tltaL owed; ?ass the wwvwber of wLwdows
awd the wLwdow yrLce, as weLL as the addYess
of avarLabLeL0 stlYethetotaL lwed
eLse
caLL the gelBl q0 f *wctLo w to o aLcwLate
the totaL owed; ?ass the wvwber of wLwdows
awd the wLwdow 7rLce, as weLL as the address
of a varLabLe t0 stlYe the LlLal owed
ewd Lf
dLsyLagthetotaL owed
eLse
dLELa g' @v aLLd oYtLow' vwessa qe
ewd Lf
f igi;:'e 1S-19 IPO charts for the functions in the Lab10-2 program (conttnues)
349
Passing Variables to a Function
(continued)
IPO charts for the functions in the Lab10-2 program (continues)
T
T
start
call
displayOptions
get pricing
option
and window price
get numbe
of windows
pnclng
option
is1or2
display'lnvalid
option"
message pricing
option
isi
callgetRegular; pass
number of windows,
window price, and the
address of a variable to
store the total owed
call getBoGo; pass
number of windows,
window price, and the
address of a variable to
store the total owed
stop
ll
F
F
T ---t--]
lllt Void Functions
ontinued)
di spl ayoptions function
lnput
w0we
Processing
ProcessLwg Ltevws: wowe
ALgorLthrw:
t. dLspLag prLcLwg oqtLows
Output
prLutr"c cPt-i'a7v3
getRegular function
lnput
wu.wtber of wLwdows
wLwdovt VrLce
ad,dress of a varLabLelo
stoYe the totaL owed,
Processing
ProcessLwg Llew"s: wawe
Output
ic:q.t o','lea.
ALgorLthv*:
t. caLcLlale totaL owed : w,twbe
o{ wLwdows * wLwdow TrLce
= number of
window price
getBoGo function
lnput
wwwtber of wLwd'ows
wtwdovt prLce
add.ress of a varLabLeto
stoYe Lhe LotaL owed
Processing
ProcessLwg Llew-s: wowe
Output
.0--?.i ltriar,
ALgovLthw":
. caLu,tLate toLaL lwed : (w*n*ber of
wLwdows / 2 + the revv-aLwdev of
w,wwber of wLwdows / 2) * wLwdow TrLce
start
display pricing
options
start
stop
Fiqui'* 1l]'3-S IPO charts for the functions in the Labl0-2 program (continues)
stop
Passing Variables to a Function
t!
(conilnued)
start
total owed = (number of
windows /2+ the
emainder of number of
windows / 2) . window price
IPO charts for the functions in the Lab10-2 program
The third step in the problem-soiving process is to desk-check the algorithm. you will desk-
check the algorithms twice. For the first desk-check, you rvill use 1, 11, and 300 as the pricing
option, number of windows, and window price, respectively. Using these values, the total owed
should be $3300.00, as shown earlier in Figure XXXXXXXXXXFor the second desk-check, you will use
2, 15, and 200 as the pricing option, number of windows, and window price, respectively; the
total owed should be $1600.00, as shown earlier in Figure XXXXXXXXXXFigure 10-20 shows the
completed desk-check tabie.
Note: The names in black indicate items that belong to the mai n function. The names in red
indicate items that belong to the getRegul ar function. The names in blue indicate items that
elong to the getBoGo function.
prLcLwg optLow w*vwber of wLwdows wLwd,ow prLce
ts+elewed
totaL owed
-e#
1_boo.o
f ft
L5
3e#
2
WlW ,)a
U
lig:i
: 3 il-fii Completed desk-check table for Lab 10-2's algorithms
The fourth step in the problem-solving process is to code the algorithm into a program. The IpO
chart information and C++ instructions for the program are shown in Figure lO-21.
351
352
Void Functions
main function
IPO chaft information
lnout
prtai.r"0, cFiLcr"
'/.-/-
- C ai' cl'.,t tr"a cv t s
'11i.y"j,9'11
pri.L2
Processing
'"/u0,/v2
Output
.-c--0.: c"tie1.
Algorithm
t caLL the dLspLagoptLows fv+wctLow to dLsTLag
theprLcLwg oTtLows
2. get the prLcLwg optLow
=. Lf (the yrLcLwg optLow Ls eLLher t- or z)
main function
C++ instructions
int option = 0;
int numordered = 0;
double winPrice = 0.0i
double totalowed = 0.0;
displayOptionsO;
cout
"Pricing option? ";
cin
option;
if (optioh =: 1 | I option == 2)
i
cout
"Number of windows: ":
cin
numOrdered;
cout
"Price Per window: ";
ci n
wi nPri ce;
if (option == 1)
getRegul ar (numOrdered,
wi nPri ce, total Owed) ;
el se
getBoco (numorde red ,
winPrice, totalOwed);
/end if
cout
"Total owed-----> $"
totalOwed
endl
endl;
]
el se
cout
"Invalid option"
endl;
end if
displayoptions fu nction
G++ instructions
get the wuvv.ber of wLwdows awd the
wLwd,ow prLce
Lf (tneprLcLwg optLow Ls t)
c aLL th e gf
Lhe totaL owed; ?ass the w,+wtber of w,nAows
a^1, the wLn dow yrtce, as weLL as the address
o{ a v ad"abt"e. to store the tDtaL Dwed
eLse
caLLthe getalqo {uwtbwto cabtiale
theftaL owed;passthe wtrwber of wtndows
a^l the wL^dow yy'we, as weLL as the ad.dress
of a varvabLeLo storethetotaL owed
ewd Lf
dLspLag the totaL owed
eLse
dLspLag' t wv aLLd optLow' w"essage
ewd, Lf
displayoptio ns fu nction
IPO chart information
lnput
w0we
Processing
w0we
IPO chart information and C++ instructions for Lab 10-2's program (continues)
353
Passing Variables to a Function
(conilnued)
Outout
prLcLwg oTtLows
Algorithm
t. d,LspLag VrLcLwg oTtLows cout
"Pricing options:',
endl;
cor.rt
"1 Regular pricing"
endl;cotlt
"2 BOGO pricing',
endl;
getRegular function
C++ instructions
getRegular function
IPO chart information
lnout
wun*ber of wLwdows (format paranteter)
wLwdow prLce (f orvv"aL paran,-eter)
address of a varLabLe to store the
totaL ow ed (f orn "at paraw,eLer)
Processing
w0we
int windows
double price
double &total
Outout
totaL owed stored Lw the totaL forn*aL ?ayavwetey
Algorithm
t. caLcutLate totaL owed, : wxvuber of
wLwd,ows * wLwdow prLce
setBoGo function
IPO chart information
lnput
wu.wtber of wLwd ows (f o
*aL paraw-eLer)
wLwdow VrLce fforw,aL paravweter)
total = windows ,k price;
getBoGo function
C++ instructions
int windows
double price
address of a varLabLeto
store the totaL owed (forwtaL paravueter)
Processing
w0we
double &total
Output
totaL owed stored Lw the totaL forvwaL ?aravwete
Algorithm
t. caLctLLale totaL owed, : (waw"ber of
wLwdows / 2 + the rentaLwder of
w,twtber of wLwdows / z) * wLwdow
total=(windows/Z+
windows %21 * price;
TrLce
F:iq*re l*-?t lPO chart information and C++ instructions for Lab l0_2,s program
Void Functions
The fifth step in the problem-solving process is to desk-check the program. Figure 10-22 shows
the entire program, and Figure 10-23 shows the completed desk-check table.
LabLO-Z.cpp displays total owed
2
Created
evised by
on ent date
3
4
5
6
7
8
9
t-0
11
L2
13
L4
L5
1_6
L7
18
1_9
?o
2L
22
23
24
25
26
27
28
29
30
3l-
32
33
34
35
35
37
38
39
40
4L
42
43
44
45
46
47
48
49
50
51
52
53
54
#include #i nc1 ude using namespace std;
function prototypes
void display0ptionsO;
voi d getRegul ar(i nt wi ndows , doubl e pri ce, doubl e &total ) ;
void getBoGo(int windows, double price, double &total);
int maino
i
int option = 0;
int numOrdered = 0;
double winPrice = 0.0i
double totalOwed = 0.0;
cout
fixed
setprecision(2);
di spl ay0pti ons O ;
cout
"Pricing option? ";
ci n
opti on;
if (option == 1
{
I I option == 2)
cout
"Number of windows:
cin
numOrdered;
cout
"Price per window:
ci n
wi nPri ce;
i f (opti on == 1)
getRegular(numOrdered, winPrice, totalOwed) ;
el se
getBoCo(numOrdered, wi nPri ce, totalOwed) ;
end if
cout
"Total owed-----> $"
totalOwed
endl
endl;
)
el se
cout
"Invalid option"
endl;
end if
eturn 0;
j
end of main function
/-**-:t*xfuncti on defi ni ti ons*-:'r*'*-t
void display0ptionso
i
cout
"Pri ci ng opti ons : "
endl ;
cout
"1 Regular pricing"
endl;
cout
"2 B0C0 pricing"
endl;
)
end