Wednesday, June 29, 2011

Testing

#include <iostream>
#include <string>

using namespace std;

int main()
{
 cout << "Please enter your first name: ";

 string name;
 cin >> name;

 const string greeting = "Hello, " + name + "!";

 cout << "Enter the vertical and horizontal padding: ";
 int padTopBottom;
 int padLeftRight;
 cin >> padTopBottom >> padLeftRight;

 const int rows = padTopBottom * 2 + 3;
 const string::size_type cols = greeting.size() + padLeftRight * 2 + 2;

 cout << endl;

 for (int r = 0; r != rows; ++r)
 {
  string::size_type c = 0;

  while (c != cols)
  {
   if (r == padTopBottom + 1 && c == padLeftRight + 1)
   {
    cout << greeting;
    c += greeting.size();
   }
   else
   {
    if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1)
    {
     cout << "*";
     ++c;
    }
    else
    {

     // The line where the greeting goes.  Should only be here twice
     if ( r == padTopBottom + 1 )
     {
      string space(padLeftRight, ' ');
      cout << space;
      c += padLeftRight;
     }
     else
     {
      string space(cols - 2, ' ');
      cout << space;
      c += cols - 2;
     }
    }
   }
  }

  cout << endl;
 }

 return 0;
}

Accelerated C++

Now that I'm done with school I finally have some time to sit and re-learn C++. The last time I did any substantial programming with the language was during an internship back in 2007. To help me in this task, I've decided to read a book, which I feel is the most conformable way of learning things for me.

Since I already know a large portion of the syntax, and semantics I wanted to pick a book that didn't delve into beginning territory, and instead focused on people who already know how to program but want to learn the C++ language quickly. Most of the time, the type of books that cater to programmers are titled "Pro" for example "Professional C#," etc. The reason I'm avoiding books such as C++ Primer Plus, Beginning C++, C++ in 21 days and others like them is not because they are bad, but because too much time is spent explaining the fundamentals of programming, not to mention that some of these tomes are 1000+ pages long.

There is one very popular and well reviewed book for C++ that has stood the test of time due to its unique approach of teaching the language: Accelerated C++. It covers just as much material as other beginners books and doesn't spend hundreds of pages teaching how to use a for loop. My goal is to read and do all the exercises in the book by August 15 of this year.

In the last two weeks I've read the first 4 chapters and I'm still working my way through the exercises in Section 3. What I'm finding is that searching for the solutions online has shown that only a few people have posted them, but not for the entire book. So I thought it would be a cool idea to post all the solutions here in my blog as I work my way through each chapter. Perhaps it will give me a little more motivation to read the book in its entirety, and some people will find it useful to see how I approached each exercise.