We now have a program to compute the profit for a one stock portfolio. The remaining feature we must add to meet the spec is to handle multiple stocks in the portfolio.
Suppose we have a two stock portfolio. What would the algorithm look like?
Get the number of shares for the first stock
Get the selling price for the first stock
Get the purchase price for the first stock
Print the data values for the first stock
Compute the cost for the first stock
Compute the value for the first stock
Get the number of shares for the second stock
Get the selling price for the second stock
Get the purchase price for the second stock
Print the data values for the second stock
Compute the cost for the second stock
Compute the value for the second stock
Compute the profit
Print the result
Will this work?
How do we compute the profit? We need the total portfolio cost and value. Lets try a hand example:
We have the first stock (XYZ): 57 shares, we bought for 9 1/4, selling at 11 3/8.
We paid 57 * ( 9 1/4) = $527.25
We can get 57 * ( 11 3/8) = $648.375
We have a second stock (say IBM): 6 shares, we bought for 50 1/2, selling at 94 1/4.
We paid 6 * ( 50 1/2) = $303.00
We can get 6 * ( 94 1/4) = $565.50
We can compute the total we paid:
527.25 + 303.00 = 830.25
compute the total we can get:
648.375 + 565.50 = 1213.875
and then compute profit:
1213.875 - 830.25 = 383.625
We could compute the profit like that - compute the cost and value of each stock and then add them all up. But if we implemented the program like that, we would need separate variables to hold each stock cost and value untill we have them all, and then add all of these variables.
Is there a better way?
Why not accumulate the total cost and value as we process each stock?
We have the first stock (XYZ): 57 shares, we bought for 9 1/4, selling at 11 3/8.
We paid 57 * ( 9 1/4) = $527.25
So the total cost is 527.25
We can get 57 * ( 11 3/8) = $648.375
and the total value is 648.375
We have a second stock (say IBM): 6 shares, we bought for 50 1/2, selling at 94 1/4.
We paid 6 * ( 50 1/2) = $303.00
So the total cost is
527.25 + 303.00 = 830.25
We can get 6 * ( 94 1/4) = $565.50
and the total value is
648.375 + 565.50 = 1213.875
then compute profit:
1213.875 - 830.25 = 383.625
For each stock we add in the cost or value to the total so far.
So the algorithm becomes:
Initialize the portfolio cost and portfolio value
Get the number of shares for the first stock
Get the selling price for the first stock
Get the purchase price for the first stock
Print the data values for the first stock
Accumulate the portfolio cost for the first stock
Accumulate the portfolio value for the first stock
Get the number of shares for the second stock
Get the selling price for the second stock
Get the purchase price for the second stock
Print the data values for the second stock
Accumulate the portfolio cost for the second stock
Accumulate the portfolio value for the second stock
Compute the profit
Print the result
Suppose we have a third stock (say Apple), or a fourth? fifth?...
We are doing the same sequence of steps over and over for each stock. Instead of adding another iteration to the algorithm each time, we can loop the same steps while there are more stocks to process.
The algorithm becomes:
Initialize the portfolio cost and portfolio value
while there are more stocks to proccess
Get the number of shares for the next stock
Get the selling price for the stock
Get the purchase price for the stock
Print the data values for the stock
Accumulate the portfolio cost for the stock
Accumulate the portfolio value for the stock
Compute the profit
Print the result
How can we translate this looping construct to C?