SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Procedural Abstraction And Functions That Return A Value
Exercise:
Programming Projects
Question:3 | ISBN:9780321531346 | Edition: 7

Question

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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program Code:


#include<iostream>
using namespace std;
double stockCost(int x, int y, int z, int stock);

void main()
{
	int x, y, z, stock;
	double stockVal=0;
	char choice;	 
	do
	{

		cout<<"\n Enter whole part of stock:";
		cin >>x;
		cout<<"\n Enter Numerator:";
		cin>>y;
		cout<<"\n Enter Denominator:";
		cin>>z;
		cout<<"\n Enter number of stocks:";
          cin>>stock;
		stockVal = stockCost(x,y,z,stock);	
		cout<<"\n Cost of single stock: "<>choice;
	}   while(choice == 'y'||choice == 'Y');
}  


double stockCost(int x, int y, int z, int stock)
{
  double price;
  price=static_cast(x * z + y)/(z * stock);
  return price;
}

 

Output:

 

 Enter whole part of stock:100

 Enter Numerator:10

 Enter Denominator:5

 Enter number of stocks:11

 Cost of single stock: 9.27273
 Tochoice then enter 'y' or 'Y' : n
0 0

Discussions

Post the discussion to improve the above solution.