Thursday, July 29, 2010

C++: Words on previous posts and a set of experiments...

By now you would be wondering why I discussed member functions for strings
and vectors. You see, they are not built-in data types that comes with C++ -
int, char, float, bool and pointers. The strings and vectors are what we
(the programmers and engineering students) call custom or programmer-defined
data types, more commonly known as "classes". A string is essentially an
array of characters; a vector is a specially enhanced version of a typical
array because there are more than one data that it holds - the data item
themselves and a special assistant that keeps track of data. Since they are
not built-in data types, we need special ways of dealing with it, popularly
known as member functions. We'll examine if it is even possible for us
(programmers) to create custom data types and what components are required,
if any in future posts.

So, I guess that sums up our discussion on member functions. Now it's time
for some hands-on experiments - get your editor and compiler ready...

1. Create a string variable named month and store the current month
according to system date. Then create another string variable named "s"
which will store the first three characters from the month string. Then
output both variables to the console. Provide:
A. An English version of how you would accomplish it.
B. Try translating your English words into C++ (hint: see a detailed post on
strings; you need a small assistant...). Compile your code and see what
happens.

2. Your friend asks you if you can write a small financial recorder program
to store how much money he has spent for a given month. You ask him how it
should do it, and your friend tells you to keep asking for monthly
expendatures until he types -1, at which it should output all data to the
screen.
A. What would be the most useful data type and monetary variable to use if
you want to work with whole numbers?
B. Since we are dealing with unknown number of months, what would be the
variable declaration and a useful function to add more items on this data
type you chose? (hint: look at the first example of the declaration on one
of the previous posts...).
C. Provide an English version of how you would accomplish the task outlined
above.
D. Translate your comments into C++, compile it and see what happens.

3. Suppose if we want to create a vector of ten names for a class roll call
sheet.
A. What would be the data type for this vector?
B. How would you declare a vector of your chosen data type to hold ten
items?
C. What would be more useful to add names to our vector with "fixed" element
size at the beginning: a push_back or a for loop to ask for name and store
it in the elements? (hint: one of them requires another string variable to
be placed at the beginning of our program).
D. Provide an English version of your code, then translate this into C++,
compile it and see what happens.

4. Using our roll call sheet above:
A. Suppose now you want to add more names to it. The program would ask for
names until you type "done" at the prompt, at which point it should print
out current names in the entire vector. What would you do (both in English
and in actual C++ code) to accomplish this task?
B. Suppose you find that you typed the last two names wrong. You want to
delete these two names and type the new names. What would you do (both in
English and in C++ code) to solve this problem?
In both cases, after writing C++ code, compile it and run it.

My opinions and a working program will be shown at the end.
// JL

No comments:

Post a Comment