50 likes | 71 Views
Develop a program to analyze a stock portfolio including shares owned, purchase price, and current value. Calculate the total portfolio worth and profit upon liquidation. Learn the importance of functions for modularity and task repetition in program design.
E N D
The Five Steps • Understanding the problem • Do a small example by hand • Write an algorithm for solving the problem • Translate the algorithm into a programming language (like C) • Test the program
Ways to solve problems • Top down approach • Break problem up into smaller problems • Bottom up approach • Solve smaller problem and then add features • Examples: payroll problem (book), stock market portfolio (started last lecture) • Combined approach
Where are we now? • Understand what a function is and main() • Understand declaration of variables • Able to write simple programs that can write to output (printf) • Able to write simple programs that perform mathematical operations (know about expressions) • Able to write simple programs that can read from inputs (scanf) • Able to comment programs • Know a systematic way of testing and debugging a program
Stock Portfolio Task 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.
Why use functions? • Modularity: helpful for top down design and organization of programs • Repeating tasks: many engineering problems require that certain task be repeated • Testing and debugging programs