Question

In: Computer Science

IN C++ The price of stocks is sometimes given to the nearest eighth of a dollar;...

IN C++

The price of stocks is sometimes given to the nearest eighth of a dollar; for example, 29 7/8 or 89 1/2. Write a program that computes the value of the user’s holding of one stock. The program asks for the number of shares of stock owned, the whole dollar portion of the price and the fraction portion. The fraction portion is to be input as two int values, one for the numerator and one for the denominator. The program then outputs the value of the user’s holdings. Your program should allow the user to repeat this calculation as often as the user wishes. Your program will include a function definition that has three int arguments consisting of the whole dollar portion of the price and the two integers that make up the fraction part. The function returns the price of one share of stock as a single number of type double.

TEST RUN:

Enter stock price and number of shares, please.

Enter price as integers: dollars, numerator, denominator.

30

7

8

Enter number of shares held.

200

200 shares of stock with market price 30 7/8

have value $6175.00

Do you wish to do another? y

Enter stock price and number of shares, please.

Enter price as integers: dollars, numerator, denominator.

57

3

8

Enter number of shares held.

150

150 shares of stock with market price 57 3/8

have value $8606.25

Do you wish to do another? n

Solutions

Expert Solution

We just have to convert the mixed fraction into a proper fraction and multiply it by the number of stocks.

Here's the code.

#include <iostream>
using namespace std;

double calculatePrice(int dollars, int numerator, int denominator){
double res = 0.0;
res = static_cast<double>(dollars * denominator + numerator) / (denominator);
  
return res;
}

int main() {
  
   int dollars, numerator, denominator;
  
   double value = 0, costOfOneShare, numOfShares;
  
   char choice = 'y';
  
   while(choice == 'y' || choice == 'Y'){
   cout << "Enter price as integers: dollars, numerator, denominator" << endl;
   cin >> dollars;
   cin >> numerator;
   cin >> denominator;
  
   costOfOneShare = calculatePrice(dollars, numerator, denominator);
  
  
   cout << "Enter number of shares" << endl;
   cin >> numOfShares;
  
   cout << numOfShares << " shares of stock with market price " << dollars << " " << numerator << "/" << denominator;
   cout << " have value " << numOfShares * costOfOneShare << endl;
  
   cout << "Do you wish to do another?" << endl;
   cin >> choice;
   }
   return 0;
}

Here are the screenshots of the code and the output.

Let me know if you have any doubts in the comments. Please upvote if the answer helped you.


Related Solutions

Report bond prices to the nearest .01, dollar amounts to the nearest dollar, and returns or...
Report bond prices to the nearest .01, dollar amounts to the nearest dollar, and returns or yields to the nearest basis point. You do not need to express bond prices in 1/32nd format. You are considering purchasing a newly issued annual coupon bond with 3 years to maturity on the settlement date. The bond pays a coupon of 8% and will be priced with a yield-to-maturity of 5%, and you will purchase $10,000,000 par value.   What is the quoted price...
What is the minimum price that would be negotiated between these departments (rounded to nearest dollar if necessary)?
A firm has two departments: Seller and Buyer. Department Seller provides a critical component for Department Buyer’s final product. Department Buyer can purchase the part on the external market for $100.Department Seller has insufficientcapacity: Department Buyer needs 500 units and Department Seller only has excess capacity for 200 units. The following cost and price data apply to Department Seller.                  External Market price                                                                            $108                  Variable selling/distribution costs on external sales10                  Variable manufacturing cost                                                                   40                  Fixed manufacturing cost (per unit) 25What is the minimum price that would be...
For the answers to this question, round dollar figures to the nearest dollar and show all...
For the answers to this question, round dollar figures to the nearest dollar and show all percents to two decimal points. Set up a skeletal statement using the following figures: Net sales $286,800 Gross margin $145,121 Profit 2.80%
Complete the following balance sheet using the information given. Round account balances to the nearest dollar....
Complete the following balance sheet using the information given. Round account balances to the nearest dollar. Balance Sheet Income Statement Cash Sales (All Credit) $40,000 Accounts receivable Cost of goods sold 20,000 Inventory Operating expenses 12,000 Net fixed assets Interest expense 200 Total assets Taxes 2 730 Net income $5,070 Accounts payable Short-term notes payable $1,425 Ratios: Long-term debt Profit Margin = 12.675% Common stock $5,000 Return on Equity = 15% Retained earnings Quick Ratio = 1.2 Total Liabilities and...
When doing your calculations, round to the nearest whole dollar amount AND the nearest whole number...
When doing your calculations, round to the nearest whole dollar amount AND the nearest whole number of shares,  Enter your answers in whole dollar amounts, without '$' signs and without commas. This fact pattern spans three years. All eight requirements are based on this fact pattern. However, #1 only asks the balance in the Common Stock account at the end of Year 2.  ●Issuance of Shares: Company issued 6,000 common shares with a $10 per share par value for $95,000...
What is the price of a bond given the followinginformation?  What is the net dollar...
What is the price of a bond given the following information?  What is the net dollar amount you will pay for this bond on November 30, 2020?Face Value:  $1,000Coupon: 6%, paid twice a year on June 30 and December 31.Matures:  December 31, 2024Date Purchased:  You purchased the bond in the secondary market on November 30, 2020.  (Assume today is November 30, 2020; the day you purchase this bond)Current market rate (rate used to discount the future cash flows to present value) is  4.0%.
Express monetary answers to the nearest whole dollar. Express percentage answers to the nearest hundredth 22.Tiffanywould...
Express monetary answers to the nearest whole dollar. Express percentage answers to the nearest hundredth 22.Tiffanywould like to buy a delivery vehicle for hersmall business. Shecan afford to make a down payment of $7,500 and pay monthly (end-of-month) payments of $1,350. If the term is 48months and the loan rate is 5.24 percent(APR), then what is the maximum that Tiffanycan pay for the delivery vehicle? 23Geraldpurchases a house for $250,000 by getting a mortgage for $225,000 and paying a down...
How much interest (to the nearest dollar) would be saved on the following loan if the...
How much interest (to the nearest dollar) would be saved on the following loan if the condominium were financed for 15 rather than 30 years? A $251,000 condominium bought with a 30% down payment and the balance financed for 30 years at 3.05%
How much interest (to the nearest dollar) would be saved on the following loan if the...
How much interest (to the nearest dollar) would be saved on the following loan if the condominium were financed for 15 rather than 30 years? A $269,000 condominium bought with a 30% down payment and the balance financed for 30 years at 3.05%
How much interest (to the nearest dollar) would be saved on the following loan if the...
How much interest (to the nearest dollar) would be saved on the following loan if the home were financed for 15 rather than 30 years? A $518,000 home bought with a 20% down payment and the balance financed for 30 years at 5.4%
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT