6.3 Lab Lesson 3 (Part 3 of 3) Part of lab lesson 3 There are three parts to lab lesson 3. The entire lab will be worth 100 points. Bonus points for lab lesson 3 There are also 10 bonus points. To earn the bonus points you have to complete the Participation Activities and Challenge Activities for zyBooks/zyLabs unit 5 (Gaddis Chapter 3). These have to be completed by the due date for lab lesson 3. For example, if you complete 89% of the activities you will get 8 points (there is no rounding). As with units 1 and 2 the bonus points on zyBooks will be more than 10 points. These will adjust to 10 points total for the bonus points on the grade in eLearning. This will be true for the units that map to Gaddis Chapters 3 through 7. Lab lesson 3 part 3 is worth 40 points For part 3 you will have 30 points if you enter the program and successfully run the program tests. An additional 10 points will be based on the style and formatting of your C++ code. Style points The 10 points for coding style will be based on the following guidelines: Comments at the start of your programming with a brief description of the purpose of the program. Comments throughout your program Proper formatting of your code (follow the guidelines in the Gaddis text book, or those used by your CS 1336 professor) If you have any variables they must have meaningful names. Development in zyBooks For lab lesson 3 (all parts) you will be entering your programs directly into zyBooks/zyLabs. If you want to use an Integrated Development Environment (IDE) such as Visual Studio, Code::Blocks or Eclipse you can do so, but you will have to cut and paste the code into zyBooks/zyLabs. You will need to use Develop mode and Submit mode as you did in lab lessons 1 and 2. Staring with lab lesson 4 you will be doing your development in an IDE (use whatever your CS 1336 class is using) and you will be uploading the files to zyBooks/zyLabs. C++ requirements The commission percentage (see below) must be put into a const double variable that you will use in your calculations. The numeric variables in your program need to be of type double. You will have to use getline to read in the company name since it can include spaces. You must use the string class for the company name you read in from the user. Failure to follow the C++ requirements could reduce the points received from passing the tests. General overview Display a prompt for the user telling them to enter in the name of the company. The prompt is: Enter the name of the company Read in the name of the company into a string. You must use getline to read in the company name since the company name may have spaces in the name. Next prompt for the number of shares of stock with the prompt: Enter shares of stock Now read in the number of shares of stock. This is of type int. Prompt for the purchase price with the prompt: Enter purchase price Read the purchase price into a variable of type double. Now prompt for the sale price for the stock with the prompt: Enter sale price Finally read in the stock sale prices. This is also of type double. Output a blank line before you output any result from the following calculations. Now that you have read in the data you can calculate the total amount required to purchase the stock. You also need to calculate the commission that has to be paid to the stockbroker. The stockbroker commission will be 3.5% of the amount required to purchase the stock. Note: the commission percentage must be put into a const double variable that you will use in your calculations. The numeric variables in your program need to be of type double. Output the name of the company, the cost of a share of stock, the total amount required to pay for the stock (not including the commission), the commission, and the total cost of the stock (including the commission). Next calculate the amount of income you will get when you sell the stock for the stock sales price. You also need to calculate the cost of the commission to the stockbroker for the sale of the stock. Again this will be 3.5% of the money you get for selling the stock. Output the price of one share of stock, the amount of money you get from the sale (before the commission is paid), the amount of the commission, and the amount of money you get from the sale after you have paid the commission to the stockbroker. All of the money amounts should be output in a fixed format with two digits to the right of the decimal point. Final calculate the difference between what you paid for the stock (including the commission), and what you received when you sold the stock (less the commission). This number will be negative if you have lost money. Here is some sample input data: Acme Software, Inc. 1000 45.50 56.90 The output from the above input will be: Company: Acme Software, Inc. Shares: 1000 Purchase/share: $45.50 Cost of stock: $45500.00 Cost of commission: $1592.50 Total cost: $47092.50 Sale/share: $56.90 Income from stock: $56900.00 Cost of commission: $1991.50 Total income: $54908.50 Gain or loss: $7816.00 Failure to follow the requirements for lab lessons can result in deductions to your points, even if you pass the validation tests. Logic errors, where you are not actually implementing the correct behavior, can result in reductions even if the test cases happen to return valid answers. This will be true for this and all future lab lessons.
// C++ program to calculate the gain or loss obtained by purchase and sale of given number of stock
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main() {
string company;
const double commissionPercent = 3.5; // commission percentage constant
double purchasePrice, salePrice, commissionPurchase, commissionSale;
int num_shares;
double totalPurchasePrice, totalSalePrice, result;
// input of company name
cout<<"Enter the name of the company : ";
getline(cin,company);
company = company.substr(0,company.length()-1); // remove '\n' from the end
// input of number of shares
cout<<"Enter shares of stock : ";
cin>>num_shares;
// input of purchase price
cout<<"Enter purchase price : ";
cin>>purchasePrice;
// input of sale price
cout<<"Enter sale price : ";
cin>>salePrice;
cout<<endl;
// calculate total purchase price without commission
totalPurchasePrice = num_shares*purchasePrice;
// calculate commission on the purchase of shares
commissionPurchase = (totalPurchasePrice*commissionPercent)/100;
// calculate total sale price without commission
totalSalePrice = num_shares*salePrice;
// calculate commission on the sale price
commissionSale = (totalSalePrice*commissionPercent)/100;
// calculate the profit or loss by subtracting the total sale price by total purchase price
// total sale price = total sale before commission - sale commission
// total purchase price = total purchase before commission + sale commission
result = (totalSalePrice-commissionSale)-(totalPurchasePrice+commissionPurchase);
// output the result
cout<<fixed<<setprecision(2);
cout<<"Company : "<<company<<endl;
cout<<"Shares : "<<num_shares<<endl;
cout<<"Purchase/share: $"<<purchasePrice<<endl;
cout<<"Cost of stock: $"<<totalPurchasePrice<<endl;
cout<<"Cost of commission: $"<<commissionPurchase<<endl;
cout<<"Total cost: $"<<(totalPurchasePrice+commissionPurchase)<<endl;
cout<<"Sale/share: $"<<salePrice<<endl;
cout<<"Income from stock: $"<<totalSalePrice<<endl;
cout<<"Cost of commission: $"<<commissionSale<<endl;
cout<<"Total income: $"<<(totalSalePrice-commissionSale)<<endl;
cout<<"Gain or loss: $"<<result<<endl;
return 0;
}
//end of program
Output:
Get Answers For Free
Most questions answered within 1 hours.