Write a program to analyze a stock portfolio consisting of some number of different stocks we own. For each stock we have information on how many shares we own, how much we paid for each share, and how much each share is worth now. Stock prices are expressed as a whole dollar amount and a fraction (e.g. 1/2, 1/4, 1/8, etc.). We would like to compute how our portfolio is worth and how much profit we have made if we liquidated it today.
For this example, we will take the "bottom up" approach; start with a simplified version of the task, get it working, and then add features until we have solved the entire problem.
So, let's assume we have a stock portfolio consisting of just one stock (say in XYZ Corp.). Furthermore, we will only compute how much this little portfolio is worth at first.
So we begin with our five step design process.
From the task description, and our initial simplifying assumptions, we can see that the program is supposed to compute the value of one stock.
We are given the number of shares of stock owned, and the stock value, expressed as 3 numbers (whole dollars, and the numerator and denominator of the fraction).
We should produce the value of the portfolio in dollars.
The value of the stock is simply the number of shares times the stock price. The stock price is the whole dollars plus the numerator/denominator of the fractional part.
Let's say we have 57 shares of XYZ Corp. which is selling for 11 3/8 dollars now.
Our portfolio is worth:
57 * 11 3/8
= 57 * 11.375
= 648.375
The algorithm for the simplified task is straightforward:
1. We must first set the data values for the information we have about the one stock - the number of shares, and the three stock price numbers.
2. It would be a good idea to display the data values to verify they are correct
3. Compute the stock value.
4. Display the result to the screen.
We know that a C program is a collection of functions. For such a simple algorithm, we only need a single function. Since every C program must have a function main(), we can implement the algorithm by defining main(). Start with template.c
Note: before even writing any code, we have included documentation in our program file in the form of comments
We use the algorithm to translate to stock0.c
Before we even complete coding the entire algorithm into C, we can test the program to see if it compiles.
When we compile, we get errors. These are called compile time errors or syntax errors.
stock0.c: In function āmainā:
stock0.c:20: error: syntax error before āsell_wholeā
stock0.c:28: error: āportfolio_valueā undeclared (first use in this function)
stock0.c:28: error: (Each undeclared identifier is reported only once
stock0.c:28: error: for each function it appears in.)
What Went Wrong!?
We can fix these errors and try to compile again.
Since the program compiles this time around.
We can now go back to step 4 and finish coding the algorithm in C.
line 20 -
The compiler doesn't understand what sell_whole is doing at this point in the statement. This is because we forgot the ';' at the end of line 19. The compiler did not see the end of that assignment statement, so it thought this was just more of the expression. But there was no operator between the constant and the variable name.
Tip - The error was flagged on line 20, but the problem was really on line 19. Sometimes you need to look "near by" to find the cause of the syntax error.
line 19 -
This error was caused by the same mistake - the compiler did not understand the Lvalue for this incomplete expression.
Tip - One mistake can cause many error messages to be generated. If you get many error messages, it is best to try to "fix" the first few, and recompile to see how many errors remain.
line 28 -
The compiler does not know about the variable portfolio_value - we forgot to declare it. The value of the portfolio is a dollars and cents value, so it does have a fractional part. We should declare portfolio_value as a float type.
float portfolio_value;
We can fix these errors and try to compile again.
Since the program compiles this time around.
We can now go back to step 4 and finish coding the algorithm in C.