Where have we been?
After several iterations, we now have a running, user friendly, well
documented program that solves our
task
of processing a stock portfolio.
To get there we used a
bottom-up
approach and the
five step process:
-
We started with a simplified instance of the task, to process a single
stock with selling information only.
After getting rid of the
syntax and
link
errors
(
stock0.c)
and a
run time
error
(
stock1.c),
using
debug lines
to help out
(
stock1.debug.c),
we had a running program
(
stock1.fixed.c).
-
We next added the ability to read the stock information at run time using
scanf()
(
stock2.c)
and then added another feature from the
spec,
the buying price, in
stock3.c
using a
function
to implement the common steps of calculating
the extended stock price.
-
We then completed the task by using a
looping construct
to process a multi-stock portfolio.
We implemented the loop three different ways:
-
Asking the user how many stocks there were, and then counting them
(
stock4.count.c).
-
Using a special data value for the entered number of shares to
indicate the end of the data
(
stock4.spv.c).
-
Using the End Of File marker as the defacto end of data marker
(
stock4.eof.c).
- You might have noticed that most of the programs we have done so far
(at least all the looping ones)
in lecture, lab and homework are pretty similar in their overall
algorithm; the Filter Algorithm.
This is a very common algorithm that can be used to solve lots of problems
and we will see variations on it over and over.
-
Next we began improving the program, making it more
robust
(tolerant of user errors) by checking the
data values entered and readable using
macros
(
stock5.more.c).
-
Finally, we made the program more managable for the programmer
(and maintainer) by splitting the code into
separate files
(
stock6.c and
stock_utils.c)
with associated
header files
(
stock_utils.h and
tfdef.h).
We also added
conditional compilation
directives to make it easier to
turn on and off debugging, and interactive prompts from the program
(
stock7.c).
Along the way...
we have seen some of the features of the C language, such as:
- A C program is just a collection of functions, including one
special function, main().
-
A function is a list of
statements
which we give some information,
it performs some task, and returns a value.
We can define a function by writing its header and body.
- In the header we define what type of value the
function returns, and declare the parameters by listing
the variables to receive the information passed to the function.
- We saw we must also declare functions to the compiler using a
prototype statement.
- The body consists of
variable declarations,
and then the list of
statements
to perform the task.
-
Variable declarations
tell the compiler to allocate
memory to store a specific type of data, and associate a name with it.
So far, we have seen two types of data, int and float.
- We have seen several types of statements:
- We have also seen two built-in functions provided in the C
library for output and input:
-
printf()
which is given
a format string containing characters and
conversion specifiers to be printed to the output and
expressions for each conversion specifier to compute the
values to be substituted in the output.
-
scanf()
which is given
a format string generally containing only
conversion specifiers for data to be read from the input
and addresses of the variables where the data is to be stored.
It returns an integer indicating the status of the read.
The status tells how many data conversions were done, or the
special value EOF if the end of the file was detected.
- We have also seen ways to document and organize our code using
comments
and compiler directives such as:
It is time to put the stock program away...
for now... and begin a
new task.
to Overview.