#include #include #include #include // need for rand() #include // need for rand() using namespace std; static double cash = 1000.00, stockPrice = 10.0, expectedPrice; static int stockShares; void main() { srand(time(NULL)); cout << fixed << showpoint << setprecision(2); bool keepTrading = true; while (keepTrading) { stockPrice = stockPrice + static_cast(rand()-RAND_MAX/2)/RAND_MAX; if (stockPrice <= 0) stockPrice = 0.02; expectedPrice = stockPrice + static_cast(rand()-RAND_MAX/2)/RAND_MAX; if (cash > stockPrice && expectedPrice > stockPrice*1.01) { cash = cash - stockPrice; stockShares = stockShares + 1; } if (stockShares > 0 && expectedPrice*1.02 < stockPrice) { cash = cash + stockPrice; stockShares = stockShares - 1; } if (cash+stockShares*stockPrice == 500) keepTrading = false; if (cash+stockShares*stockPrice == 2000) keepTrading = false; } // end while cout << "You now have $" << (cash+stockShares*stockPrice) << endl; }