1、面向对象的C+程序设计 第六版 课后习题答案第四章Chapter 4Procedural Abstraction and Functions That Return a Value1. Solutions to and Remarks on Selected Programming Problems1. No solution provided.2. No solution provided.3. Value of Stock HoldingsThis program asks for input of the price of a stock, the number of stocks ow
2、ned, and outputs the value of the stock holding in dollars and cents. The program should define a function that accepts the price of the stock in three int values: whole dollars, numerator of fraction, and denominator of the fraction, then outputs the value of the stock as a double. For example, for
3、 a stock whose price was 7 7/8 dollars, the input would be 7 7/8 and the output would be 7.87 (using a 5s round odd rule.) In supervised student labs, I find the error of having a function prototype different in some essential way than the function definition to be a prevalent error.The Algorithm in
4、 Pseudocode and in code:/required function:/return value = dollars+num/denom with appropriate casts/double convert (int dollars, int num, int denom)/get price with fraction, number of shares/call function to convert price with fraction to price as double/value = price * (number of shares)/output val
5、ue/Program:/file Ch3Prob2.cc/problem: take input of number of shares, price as dollars,/numerator and denominator of fractional part of price, give/value of holdings.double convert(int dollars, int num, int den);/accept as input the stock price with fraction, return/price as double#include using nam
6、espace std;int main()int dollars, numer, denom, shares;double price, value;cout Enter stock price and number of shares, please.n Enter price as integers: dollars, numerator, “ “denominator. dollars numer denom;cout Enter number of shares held. shares;price = convert(dollars, numer, denom);value = pr
7、ice * shares;cout.setf(ios:fixed);cout.setf(ios:showpoint);cout.precision(2);cout shares shares of stock with market price dollars numer / denom endl have value $ value endl;return 0;double convert (int dollars, int num, int den) return dollars + double(num)/den;A typical run follows.14:24:24:/AW$ a
8、.outEnter stock price and number of shares, please.Enter price as integers: dollars, numerator, denominator.10 5 8Enter number of shares held.100100 shares of stock with market price 10 5/8have value $1062.5014:24:32:/AW$4. No solution provided.5. No solution provided.6. Credit Card InterestThis pro
9、gram computes interest on a credit card balance. The task is done with a function that accepts initial balance, monthly interest rate, number of months for which the interest must be paid. The value returned is the interest due. Allow repeat at users option. NB Interest is compounded. Function is to
10、 be embed in a program that acquires these values and outputs the interest due. Repeat of computation at users option is to be allowed.Algorithm in pseudocode:Function name: interest input: fetch values for: double initBalance, double rate, int months.process: declare balance = initBalance, interest
11、; declare index = 0; while (index months) balance = balance * (1 + rate); months+; interest = balance - initBalance; output: interest main: declare balance rate, interestEarned, months fetch balance, rate, months interestEarned = interest(balance, rate, months) output interestEarnedCode: /file: ch4P
12、rog5.cc #include using namespace std; /Problem: /given initial balance, rate and months to /run, how much interest accrues on a credit card? /allow repetition at users option. /compute compound credit card interest double interest (double initBalance, double rate, int months) double balance = initBa
13、lance; int i = 0; while (i months) balance = balance * (1 + rate); i+; return balance - initBalance; int main() double balance, rate, interestEarned; int months; char ans; cout.setf(ios:showpoint); cout.setf(ios:fixed); cout.precision(2); do cout Credit card interest endl Enter doubles: initial bala
14、nce, “ “monthly interest rate as n a decimal fraction, e.g. for 1.5% “ ”per month write 0.015n and int months the bill has run.n I will give you the interest that “ balance rate months; interestEarned = interest(balance, rate, months); cout Interest accumulated = $ interestEarned endl; cout Y or y r
15、epeats, any other character” “ quits ans; while (Y = ans | y = ans); return 0; A typical run follows:15:19:13:/AW$ a.outCredit card interestEnter doubles: initial balance, monthly interest rate asa decimal fraction, e.g. for 1.5% per month write 0.015and int months the bill has run.I will give you t
16、he interest that has accumulated.1000.01512Interest accumulated = $195.62Y or y repeats, any other character quitsyCredit card interestEnter doubles: initial balance, monthly interest rate asa decimal fraction, e.g. for 1.5% per month write 0.015and int months the bill has run.I will give you the in
17、terest that has accumulated.1000.02112Interest accumulated = $283.24Y or y repeats, any other character quitsq15:19:35:/AW$7. No solution provided8. No solution provided9. Clothes size calculationThe major problem for students in this as in many word problems is determining the formulas from the pro
18、blem statement.Given height, weight, age, compute clothes sizes:hatSize = weight (lbs.) / height (in.) * 2.9jacketSize (chest size, in.) = height * weight / 288 +(1/8)*(age-30)/10Note carefully that the adjustment only occurs for complete 10 year interval after age 30, i.e., if age 40, there is no a
19、djustment! 40 = age 49 gets 1/8 in. adjustment, etc.waist (in.) = weight / 5.7 + (1/10) * (age - 28)/2NB: adjustment only occurs for complete 2 year interval after 28age = 29, no adjustment30 = age 40) jacket = jacket + (age - 30)/10 * 0.125;This depends on the behavior of the C/C+ language to obtai
20、n the results required. The (age-30/10) arithmetic will be done as int, since there is nothing to require type change. The int result will be then converted to double in the multiplication by the 0.125 (which is 1/8 as a decimal.)Waist Size Calculation: size = weight/5.7;if (age=30) size = size + (a
21、ge - 28)/2 * 0.1;Again, the weight will be converted to double in the division by 5.7. The expression, (age - 28)/2, will be computed as an int, then be promoted to double in the multiplication by 0.1./file: ch4Prb8.cc/problem: Clothes size calculation:/given height (inches) weight (pounds) and age
22、(years)/compute jacket size, waist size, in inches, and hat size:/returns hat size in standard hat size units#include using namespace std;double hatSize (int height, int weight) return 2.9 * double(weight) / height;/returns jacketSize in inches at the chestdouble jacketSize (int height, int weight,
23、int age) double jacket = double(height) * weight / 288; if (age 40) jacket = jacket + (age - 30)/10 * 0.125; return jacket;/ returns waist size in inchesdouble waistSize (int height, int weight, int age) double size = weight/5.7; if (age=30) size = size + (age - 28)/2 * 0.1; return size;int main() i
24、nt height, weight, age; double hat, jacket, waist; char ans; do cout Give me your height in inches, weight in pounds, and age in years endl and I will give you your hat size, jacket size(inches at chest) endl and your waist size in inches. height weight age; hat = hatSize (height, weight); jacket =
25、jacketSize (height, weight, age); waist = waistSize (height, weight, age); cout.setf(ios:showpoint); cout.setf(ios:fixed); cout.precision(2); cout hat size = hat endl; cout jacket size = jacket endl; cout waist size = waist endl; cout endl enter Y or y to repeat, “ “any other character ends. ans; wh
26、ile (Y = ans | y = ans); return 0;A typical run follows:17:07:37:/AW$ a.outGive me your height in inches, weight in pounds, and age in yearsand I will give you your hat size, jacket size (inches at chest)and your waist size in inches.69 185 50hat size = 7.78jacket size = 44.57waist size = 33.56enter
27、 Y or y to repeat, any other character ends.yGive me your height in inches, weight in pounds, and age in yearsand I will give you your hat size, jacket size (inches at chest)and your waist size in inches.67 200 58hat size = 8.66jacket size = 46.78waist size = 36.59enter Y or y to repeat, any other character ends.n17:08:55:/AW$No solutions provided for problems 10-12.13./ */ Ch4Proj13.cpp/ This program outputs the 99 bottles of beer on the wall song./ A simple loop calls a function that outputs each stanza/ for a particu
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1