The Double Baggins

(This post originally appeared on my Digication site.)

As a (non-gradable) challenge for BHCC’s Data Structures course, Prof. Richmond challenged us to come up with a new sorting algorithm. This is the one I came up with.
Continue reading

Posted in Programming | Tagged , , | Leave a comment

Breaking cin for fun and profit

This post was originally written for students in Bunker Hill’s CIT 120 course.

Breaking the cin object for validation

As you should know, cin is an object. Specifically, it is an object of type istream (or, an instance of the istream class). For those who know anything about C (as opposed to C++), it is very roughly equivalent to stdin from the cstdio (stdio.h) library.

In C++, objects can overload operators. This means that whoever programmed the object, is able to determine the behavior of that operator when it works with that object. An example of this is the addition (+) operator and a string object; the operator is overloaded to concatenate strings (whereas by default, it simply adds numeric values).

In fact, this is how the stream extraction operator (>>) and stream insertion operator (<<) work. By default, in C++, they are not stream operators at all, but the “bitshift right” and “bitshift left” operators; they are overloaded when used with stream objects.

What does this have to do with “breaking” cin? Well, behind the scenes, the cin object has a series of flags. Flags are simple data types that can be either “1” or “0” – they are similar to the boolean type, but only take up one bit, rather than a whole byte. Flags usually represent the “status” of the object. The cin object has a bunch of flags; the ones that concern us here are badbit and failbit. If either of those bits are true, the cin object is considered “broken.” Any attempt to use the cin object’s methods (such as cin.ignore() or cin.getline()) or the stream extraction operator (>>) won’t work at all.

Continue reading

Posted in c++, Programming | Tagged , | Leave a comment

A beginner’s guide to function design in C/C++

This post was originally written for students in Bunker Hill’s CIT 120 course.

Quite a few beginning CS students have trouble with function design. They know the syntax of functions, how to write headers, etc.; but they’re having trouble actually deciding what functions should and should not do, how to organize them, and so forth.

This falls under the category of software design (as opposed to writing language-specific code). Software design is by no means a trivial subject; even experienced programmers find it hard to make design decisions. Additionally, most introductory CS courses do not deal with the design of objects and classes (the traditional subject of most tomes on software design), but on function design, which is only briefly touched upon in many books. So it is no surprise that many students feel completely lost when tackling it for the first time.

Continue reading

Posted in c++, Programming | Tagged , | Leave a comment

Obligatory welcome post

Welcome to my new WordPress blog. Come in and stay a while.

My apologies about the mess. I’m using a free WordPress blog, and that limits me to very few WP themes, all of which are only minimally customizable.

Those who know me will ask why I’m even creating a free site on WordPress, and not using my perfectly good Karlheinz site that I pay good money for. There are two reasons. First, this site is dedicated to my slim knowledge of computer science, which is not very interesting to anyone who is not a computer scientist. Second, I also need a site that is suitable for showing to prospective employers, as well as other students, and that Karlheinz guy is kind of a dick.

One or two of these posts are taken from my Digication page, which I set up while I was a peer mentor at BHCC. Unsurprisingly, they cover some pretty basic stuff (and I will tag them accordingly). As time goes by, I’ll be adding some other posts that will hopefully be a little more relevant to someone who isn’t a first-year CS student. (That will happen as soon as I learn something that is relevant to anyone who isn’t a first-year CS student.)

Many of those posts are too basic for this site, so they are going to remain on Digication.

Posted in Website | Leave a comment