Programming Exercise 2-12
# Named constants
COMMISSION_RATE = 0.03
NUM_SHARES = 2000
PURCHASE_PRICE = 40.0
SELLING_PRICE = 42.75
# Variables
amountPaidForStock = 0.0 # Amount paid for the stock
purchaseCommission = 0.0 # Commission paid to purchase stock
totalPaid = 0.0 # Total amount paid
stockSoldFor = 0.0 # Amount stock sold for
sellingCommission = 0.0 # Commission paid to sell stock
totalReceived = 0.0 # Total amount received
profitOrLoss = 0.0 # Amount of profit or loss
# Calculate the amount that Joe paid for the stock, not
# including the commission.
amountPaidForStock = NUM_SHARES * PURCHASE_PRICE
# Calculate the amount of commission that Joe paid his broker
# when he bought the stock.
purchaseCommission = COMMISSION_RATE * amountPaidForStock
# Calculate the total amount that Joe paid, which is the amount
# he paid for the stock plus the commission he paid his broker.
totalPaid = amountPaidForStock + purchaseCommission
# Calcualate the amount that Joe sold the stock for.
stockSoldFor = NUM_SHARES * SELLING_PRICE
# Calculate the amount of commission that Joe paid his broker
# when he sold the stock.
sellingCommission = COMMISSION_RATE * stockSoldFor
# Calculate the amount of money left over, after Joe paid
# his broker.
totalReceived = stockSoldFor – sellingCommission
# Calculate the amount of profit or loss. If this amount is a
# positive number, it is profit. If this is a negative number it
# is a loss.
profitOrLoss = totalReceived – totalPaid
# Print the required data.
print (“Amount paid for the stock: $”, format(amountPaidForStock, ‘.2f’))
print (“Commission paid on the purchase: $”, format(purchaseCommission, ‘.2f’))
print (“Amount the stock sold for: $”, format(stockSoldFor, ‘.2f’))
print (“Commission paid on the sale: $”, format(sellingCommission, ‘.2f’))
print (“Profit (or loss if negative): $”, format(profitOrLoss, ‘.2f’))
Constant Real
COMMISSION_RATE =
0.03
Constant Integer
NUM_SHARES = 2000
Constant Real
PURCHASE_PRICE =
40.0
Constant Real
SELLING_PRICE = 42.75
Start
Declare Real
amountPaidForStock
Declare Real
purchaseCommission
Declare Real totalPaid
Declare Real stockSoldFor
Declare Real
sellingCommission
Declare Real
totalReceived
Declare Real profitOrLoss
Set amountPaidForStock =
NUM_SHARES *
PURCHASE_PRICE
Set purchaseCommission
= COMMISSION_RATE *
amountPaidForStock
A
Set totalPaid =
amountPaidForStock +
purchaseCommission
A
Set stockSoldFor =
NUM_SHARES *
SELLING_PRICE
Set sellingCommission =
COMMISSION_RATE *
stockSoldFor
Set totalReceived =
stockSoldFor –
sellingCommission
Set profitOrLoss =
totalReceived – totalPaid
B
B
Display
“Amount paid
for stock: $”,
amountPaidFor
Stock
Display
“Commission
paid on the
purchase: $”,
purchaseComm
ission
Display
“Amount the
stock sold for:
$”,
stockSoldFor
Display
“Commission
paid on the
sale: $”,
sellingCommissi
on
Display “Profit
(or loss if
negative): $,
profitOrLoss
End