Saturday, July 31, 2010
I'm so proud of my school: Miss Korea 2010 attends UCR!
(most likely poli sci major, as she wants to become a diplomat). I'm not
sure if I know here or not - I wish her good luck at being a highlander (I
hope to meet her some day)...
Here's the link:
http://hanopolis.com/?articleNo=29799&story/UC-Riverside-student-crowned-201
0-Miss-Korea
// JL
Thursday, July 29, 2010
C++: Intermission 4
with designing a custom object. Before then, I just memorized what a given
function does, not knowing that I needed to learn the true workings of these
functions for later use.
As for vectors, at first I just memorized the function names and what it
does. As my experience with vectors grew, I began to appreciate what each
function does exactly under the hood - especially after writing an integer
vector class as part of a lab assignment.
You'll see what I mean by the words "class" and "object" starting from the
next post, as creating our own custom data types requires significant
investigation and hours of notetaking. The very concept of working with our
own data types would be covered in detail for the remainder of our
discussion of C++, with occasional notes on other useful things you need to
know such as memory operations, file operations and so forth.
Again, if you have any comments about these notes, please let me know...
// JL (UCR)
C++: Misc notes 4
* You need to first include the desired library before you can even declare
that variable type and use its functions.
* There is a handy function to combine two or more strings, ironically it is
the plus (+) sign. For instance, if I want to have three string variables
named A, b, and C, assign some text string on A and B and combine these
strings into C, I would write:
string A, B, C; // Which is legal.
A = "Hello, "; B = "World!";
C = A + B;
cout << C << endl;
If we run this code, it'll print out "Hello, World!" to the screen.
I think that's it for now... If you have any comments, please let me know.
// JL
C++: Words on previous posts and a set of experiments...
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
C++: A detailed tour of vector operations
function is and how it is used. In this post, let's revisit the concept of a
member function using vectors.
Just like strings, in order to use member functions for vectors, we need to
include vector library:
#include <vector>
Just like strings, we can manipulate vectors, such as find out how many
items are in a given vector, access elements, add or remove something and so
forth. Let's examine it in more detail.
To declare a vector, you would use the syntax like:
vector<type> var;
Where "type" is the data that the vector would hold, and "var" is the name
of this vector. For instance, we can declare a string vector like:
vector<string> s;
Note the arrows (less-than and greater-than signs) surrounding our data
type. As a matter of fact, there are two other popular variants of our above
code - one is used to tell the computer how many items will be stored on
that vector, and another one goes further and assigns each element to a
unique value. For instance, if we want to store five integers in a vector
called "item", we could use:
vector<int> item (5);
Furthermore, if we want to assign the value "10" to each of the elements, we
could say:
vector<int> item (5, 10);
In our second declaration, what happens if we output our vector using for
loop?
for (int i = 0; I < 5; i++)
{
cout << Item[i] << " ";
}
On the screen, the result would be five "10's" being displayed on the
monitor, just like our output routine from our example earlier on another
post.
But we don't really want to use a fixed number for this kind of output code.
What if the number of items change? Thankfully, there is a dedicated
assistant that helps us here: the "size" member function, which gives us how
many elements does a vector have (you may have seen it before): v.size();
So, if we use this function, we'll get the output code similar to something
you may have seen before:
for (int I = 0; I < item.size(); i++)
{
cout << item[i] << " ";
}
And, as I've said before, we use subscript operator ([]) to access elements.
As for adding and removing things, in addition to the push_back function
we've seen, there is a dedicated function to take out the last element of a
vector: the pop_back member function. The job of this function is to delete
the last element in a vector and decrease the size of our vector by one. We
call this function similar to how we call size:
v.pop_back();
Don't worry about arguments here - it does not need it. So, for instance, if
I invoke the pop_back on my item vector, it would delete the fifth "10" and
decrease the size by 1 (so the total would be four numbers):
Item.pop_back();
Now, if we invoke the for loop to print our vector, it should print four
"10's" instead of five. I guess that's enough on vectors for now - enough
basic functions and tips to get you started on a small program to work with
vectors.
By now you may be wondering why I am giving you tips on so-called member
functions. This was done so that I can gradually introduce you to the
concept of objects and classes, or custom (or programmer defined) data
types. I'll provide more details on my decision and flow of our discussion
on the next post. Then you can have a hands-on experiments with working with
member functions.
// JL
Tuesday, July 27, 2010
C++: Examining string of characters
it, arguments and concept of return values. In this part, we'll examine more
details about functions by looking at what we can do with string variables.
There are a number of reasons why I chose strings for this series. First, as
you might have guessed, a string is essentially an array of characters.
Second, there are a host of functions we can use to manipulate strings,
including finding an exact character within a string, taking a part of a
string and assigning it into a new variable and so forth. Third, using these
functions, I can gradually introduce you to the concept of classes, objects
and member functions, as you'll see after this chapter.
So, as you have seen, we declare a string just like any variable:
string var; // Where var is the name of our string variable.
However, as you've seen, in order for us to work with strings, we need to:
#include <string>
Since "string" is not a built-in data type (although it is an array of chars
though). The string library, in addition to allowing us to declare string
variables, hosts a number of "helpers," or member functions. Let us meet our
helpers, shall we?
Sometimes, you may need to take part of a string and assign it to something
else - perhaps for comparing two strings or printing just a portion of that
string. To do this, we use a "substring" function (substr) to take part of a
string and use it for other purposes. The syntax is:
string_var.substr(start_of_selection, length_of_string);
Where "string-var" is our string variable, "start_of_selection" is where we
want to start the substring routine e.g. where a portion of our string
begins, and "length' is the number of characters that the substring should
"capture" e.g. set aside for further action. Note that the first character
in a string starts at zero (0.
For example, say if I have a string variable with the following string:
string text = "Welcome to Blogger.";
The total length of our string is nineteen characters, so the length range
would be from zero to eighteen. Suppose if we want to set the word "welcome"
aside for another string variable named "intro." We can do the following (in
English):
We want to take a portion of our string, so we need substr function. The
start of our portion is at zero (at the letter "W") and will run for seven
characters (welcome). Using what we've just obtained, assign this word to
another string variable.
In C++, it goes something like:
string intro = text.substr(0, 7);
In other words, we've just taken the first seven characters on our first
string (under text variable) and assigned it int "intro" one. In terms of
arguments, the start of substring could be anywhere - at the start of our
text (zero) or even halfway across. As for length, it could be any length,
provided that it does not exceed the total length of our source text (we'll
see how we do this below). For example, if we want to select the word
"Blogger" from our text above, we could say (assigning the substring to blog
variable):
string blog = text.substr(11);
Notice that I only put one argument, and that is perfectly legal. What we
have just performed was select from the letter "B" to the end of our string.
So, if we output our blog variable:
cout << blog << endl;
It'll print, "Blogger."
But how do we know the length of our string? The next friend we'll meet is
the length function. This just tells (returns) the length of our string. Let
us return to our message under "text" variable. If we use the code:
size_t length = text.length();
Few things here: don't worry about size_t, it's just an enhanced version of
integer variable used to store size of a string or similar things. The
length function did not take any arguments (and that is legal); what it will
do is just tell us how many characters (technically elements) does our array
of characters (string) has - in our case, 19.
Another quick visit to another friend and I'll let you go, and that is the
question of how we find a specific character in our string. Logically
enough, this friend's name is find function, which it does what it is
supposed to do: find a character for us. All we need to put in this member
function is just one argument: the actual character we are looking for. What
will it give us in return? The location where this item is located, if it
can find it.
For instance, using our text variable, suppose you want to find the first
instance of the letter "g". We would write:
Text.find("g");
Or something similar. Be sure to put quotes around the character. Luckily
for us, there is a "g" - it's in location 14.
As a side note, we can use this function to find anything - even a word.
What it will return is the location where the start of our search string is
located. (a teaser: this fact becomes very important much later...)
I think that's enough for now. Hope you understood the concept of a member
function and examples of its use in string library. In the next post, we'll
meet more member function friends, this time the ones assisting vectors.
P.S. Hope this was simple for you. I apologize for any errors or
misinformation on this post...
// JL
Monday, July 26, 2010
C++: Intermission 3
at first. But after reading many C++ resources (including cplusplus.com) and
experiments, I was able to understand and work with vectors as though they
were regular data types.
As for functions, I understood the concept for the most part. The only thing
that I didn't get on the first try was a precaution of too many lines of
code in a given function. I thought I should just write the whole story
within a block of code. However, after listening to lectures from my
instructor and visiting his office hours, coupled with my own findings that
I couldn't really follow my code, I decided to split a long function into
smaller parts - and it worked. A function is supposed to do one thing at a
time; if it tries to do multiple things at once (unless if a function is
optimized for multiple processors), it is best to split this multipart one
into smaller ones.
I think that's all that I have to say regarding functions. Starting from
next installment, we'll go into things such as member functions (we'll
examine string and vectors more), as well as creating our own data types
(classes or objects) to help us explain where member functions came from.
// JL P.S. if you have any comments, please let me know...