Question
Stock Profit The profit from the sale of a stock can be calculated as follows: Profit (( NS SP ) SC ) (( NS PP
Stock Profit
The profit from the sale of a stock can be calculated as follows: Profit (( NS SP ) SC ) (( NS PP ) PC ) where NS is the number of shares, SP is the sale price per share, SC is the sale commission paid, PP is the purchase price per share, and PC is the purchase commission paid. If the calculation yields a positive value, then the sale of the stock resulted in a profit. If the calculation yields a negative number, then the sale resulted in a loss. Write a function that accepts as arguments the number of shares, the purchase price per share, the purchase commission paid, the sale price per share, and the sale commission paid. The function should return the profit (or loss) from the sale of stock. Demonstrate the function in a program that asks the user to enter the necessary data and displays the amount of the profit or loss.
#include
#include
using namespace std;
// Function prototype
int main()
{
int ns; // Number of shares
double sp; // Sale price per share
double sc; // Sale commission
double pp; // Purchase price per share
double pc; // Purchase commission
double prof; // Profit from a sale
// Get the number of shares.
cout << "How many shares did you buy and then sell? ";
cin >> ns;
// Get the purchase price per share.
cout << "What price did you pay for the stock "
<< "per share? ";
cin >> pp;
// Get the purchase commission.
cout << "What was the purchase commission? ";
cin >> pc;
// Get the sale price per share.
cout << "What was the sale price per share? ";
cin >> sp;
// Get the sales commission.
cout << "What was the sales commission? ";
cin >> sc;
// Call the profit function to get the profit or loss.
// Display the result.
cout << "The profit from this sale of stock is $"
<< setprecision(2) << fixed << showpoint
<< prof << endl;
return 0;
}
// ********************************************************
// The profit function accepts as arguments the number of *
// shares, the purchase price per share, the purchase *
// commission paid, the sale price per share, and the *
// sale commission paid. The function returns the profit *
// (or loss) from the sale of stock as a double. *
// ********************************************************
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started