3. The price of stocks is sometimes given to the nearest eighth of a dollar; for
ID: 3733220 • Letter: 3
Question
3. The price of stocks is sometimes given to the nearest eighth of a dollar; for example, 297/8 or 891/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 and 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 doubleExplanation / Answer
// File Name: StockCalculation.cpp
#include<iostream>
#include<string>
using namespace std;
/* Function to return a number between low and high
* Parameters:
* prompt: message to display
* low: lower bound of the number
* high: higher bound of the number
*/
double GetIntInRange(string prompt, int low = 1, int high = 8)
{
// To store the number entered by the user
double no;
// Loops till valid number
do
{
// If low is one and high is 8 then set the number to 8 for denominator
if(low == 1 && high == 8)
no = 8;
// Otherwise
else
{
// Display the message with lower and higher bound for the number
cout<<prompt<<low<<" and "<<high<<" ";
// Accept the number
cin>>no;
// Checks if the number is greater than or equals to zero
// Number is between lower and higher bound
if(no >= 0 && no >= low && no <= high)
// Come out of the loop
break;
// Otherwise invalid number error message and asks to re enter the number
else
cout<<"Number not in range. Please re - enter. ";
}// End of else
}while(1); // End of do - while loop
// Returns the number
return no;
}// End of function
/* Function to calculate and return the amount
* Parameters:
* low: lower bound of the number
* high: higher bound of the number
*/
double Fraction2Double(int low, int high)
{
// Calls the function to accept share
double share = GetIntInRange("Enter number of stocks between: ", low, high);
// Calls the function to accept numerator of price
double numeratorPrice = GetIntInRange("Enter price numerator between: ", low, high);
// Calls the function to accept denominator of price
double denominatorPrice = GetIntInRange("Enter price denominator between: ");
// Calculates the amount
double amount = share * (numeratorPrice / (double)denominatorPrice);
// Returns the amount
return amount;
}// End of function
// main function definition
int main()
{
// Calls the function to calculate amount and displays it
cout<<" Dollar amount: "<<Fraction2Double(0, 7);
}// End of function
Sample Output 1:
Enter number of stocks between: 0 and 7 -2
Number not in range.
Please re - enter.
Enter number of stocks between: 0 and 7 4
Enter price numerator between: 0 and 7 2
Dollar amount: 1
Sample Output 2:
Enter number of stocks between: 0 and 7 -3
Number not in range.
Please re - enter.
Enter number of stocks between: 0 and 7 6
Enter price numerator between: 0 and 7 23
Number not in range.
Please re - enter.
Enter price numerator between: 0 and 7 3
Dollar amount: 2.25
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.