#include /******************************** * When you start a career at a * company, part of your compensation * may include stock options. A stock * option gives you the right to buy * a specified number of shares at a * fixed price, called the exercise * price. * * After the options have vested, you * may exercise them by purchasing the * shares at the exercise price, even * if the current market price is much * higher. * * One way of exercising the options * is a cashless exercise. Enough of * the resulting shares are sold at * the current market price to pay the * cost of exercising all the options. * You then keep the remaining shares. * * For example, suppose you have * options on 1000 shares at an * exercise price of $10 per share. * Exercising all the options costs * * 1000*$10 = $10000. * * If the shares are now worth $40 * each, selling 250 shares produces * * 250*$40 = $10000. * * Thus, 250 shares can be sold to * cover the exercise cost, leaving * you with 750 shares. * * Query the user for: * * - the number of shares covered * by the options, * - the exercise price per share, * - the current price per share. * * For simplicity, assume that all * prices are positive whole-dollar * amounts. * * If the number of shares is not * positive, or either price is not * positive, print an appropriate * message and return 0. * * If the current price is not greater * than the exercise price, print an * appropriate message and return 0. * * Otherwise, calculate the smallest * whole number of shares that must be * sold at the current price to cover * the entire exercise cost. * * You may use either a for loop or a * formula. If you use a formula, make * sure that it always rounds up when * necessary. ********************************/ // Function declarations int main(); // Function definitions int main() { int count{}; std::cout << "How many shares do you have options on? "; std::cin >> count; int price0{}; std::cout << "Enter the exercise price per share: "; std::cin >> price0; int price1{}; std::cout << "Enter the current price per share: "; std::cin >> price1; std::cout << "You must sell 0 shares." << std::endl; return 0; } // Hint: // // First calculate the total cost of exercising // all the options: // // count*price0 // // You need to find the smallest whole number of // shares that can be sold at 'price1' per share // to produce at least this much money. // // If you use a for loop, start at zero and keep // increasing the number of shares until their // value is sufficient. // // If you use integer division to derive a formula, // remember that ordinary integer division rounds // down. In this problem, you may need to round up. // Here is one solution using a for loop: /* int main() { int count{}; std::cout << "How many shares do you have options on? "; std::cin >> count; if ( count <= 0 ) { std::cout << "The number of shares must be positive." << std::endl; return 0; } int price0{}; std::cout << "Enter the exercise price per share: "; std::cin >> price0; if ( price0 <= 0 ) { std::cout << "The exercise price must be positive." << std::endl; return 0; } int price1{}; std::cout << "Enter the current price per share: "; std::cin >> price1; if ( price1 <= 0 ) { std::cout << "The current price must be positive." << std::endl; return 0; } if ( price1 <= price0 ) { std::cout << "The current price must be greater than " << "the exercise price." << std::endl; return 0; } int exercise_cost{ count*price0 }; int shares_to_sell{}; for ( shares_to_sell = 0; shares_to_sell*price1 < exercise_cost; ++shares_to_sell ) { // The loop body is intentionally empty. // Continue increasing 'shares_to_sell' // until selling that many shares covers // the exercise cost. } std::cout << "You must sell " << shares_to_sell << " shares to cover the exercise cost." << std::endl; std::cout << "You will keep " << count - shares_to_sell << " shares." << std::endl; return 0; } */ // Here is one formula you could use: /* int exercise_cost{ count*price0 }; int shares_to_sell{ exercise_cost/price1 }; if ( exercise_cost%price1 != 0 ) { ++shares_to_sell; } */ // Here is a more compact formula: // // For positive integers 'a' and 'b', // // (a - 1)/b + 1 // // calculates a/b rounded up to the next integer // whenever necessary. Therefore: /* int exercise_cost{ count*price0 }; int shares_to_sell{ (exercise_cost - 1)/price1 + 1 }; */