< Browse > Home /

C++ Recursion – Palindrome

March 10th, 2010 | No Comments | Posted in C++, Functions by Diego | - [Full Entry]

Determining if a string is a Palindrome using recursion

Below is an example of how to determine if a word is a palindrome using recursion. A Palindrome is a string that could be read the same way in either direction such as racecar, mom, and even a. This tutorial expects you to understands the following.

C++ Recursion

Example of finding if a word is a palindrome

To determine if a word is a palindrome do the following.

  1. Compare the first and last character
  2. If these characters are the same, disregard these characters and repeat until one one character or no characters remain

Example:

// Check first and last character
[ r ] [ a ] [ c ] [ e ] [ c ] [ a ] [ r ]

// Second iteration
[ a ] [ c ] [ e ] [ c ] [ a ]

// Third iteration
[ c ] [ e ] [ c ]  

// Fourth iteration
[ e ]

// The word is a palindrome.
 

Now let’s try one that isn’t a palindrome.

// First iteration
[ t ] [ a ] [ b ] [ t ]

// In our second iteration, a and b don’t match
[ a ] [ b ]

Read more »

C++ Recursion – Reverse elements of a vector

March 8th, 2010 | No Comments | Posted in C++, Functions by Diego | - [Full Entry]

Reversing the elements of a vector using recursion

Below is an example of how to reverse the elements of a vector using recursion. This tutorial expects you to understand the following.

C++ Recursion

Example of reversing the elements of a vector

Below is an example of how one would reverse the elements of a vector by simply intuition.

// Our vector with size 5
[ X ] [ B ] [ A ] [ N ] [ Y ]
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

//Step 1: Swap entry 0 (first) with the entry n (last)
[ Y ] [ B ] [ A ] [ N ] [ X ]
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

//Step 2: Swap entry 1 with entry 3 (n -1)
[ Y ] [ N ] [ A ] [ B ] [ X ]
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

//Step 3: No swap needed for 3rd entry (or middle entry)
[ Y ] [ N ] [ A ] [ B ] [ X ]
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]
 

Notice how once we arrive at the entry in the middle of the vector we don’t continue. Why? The values beyond this entry have already been swapped. Keep this in mind.

Reversing the elements of a vector using iteration

Before we try making a recursive function to reverse the elements of a vector, let’s make one using iteration. By following the method described in the method above, it’s easy to see that we are simply swapping the ith and (nth – ith) variables (Example: the 0th and the 4th (4th – 0th) element).

Reversing the elements of a vector using iteration

The iterative solution for reversing the elements of a vector is below. Make sure you are trying all these functions if you haven’t before on your own.

//We pass the vector by reference
void reverseElements(vector <int> &vec) {

     //Temp variable for swapping
     int temp;

     int n = vec.size()-1;
 
     //Iterate vec.size()/2 times
     for ( int i = 0; i < vec.size()/2; i++ ) {
 
          // The swapping of elements
          temp = vec[i];
          vec[i] = vec[n-i];
          vec[n] = temp;
   
     }

     return;
}

Make sure you understand this completely before moving forward. Try working it out by hand.

Read more »

C++ Recursion – Greatest Common Divisor

March 7th, 2010 | No Comments | Posted in C++, Functions by Diego | - [Full Entry]

Greatest Common Divisor using the Euclidean Algorithm


Below is an example of how to create the recursive function of computing the greatest common divisor using the Euclidean Algorithm.

* This tutorial assumes you understand the following.

Euclid’s Algorithm

When studying computer science a famous algorithm taught to students is the Euclidean Algorithm which is used to compute the greatest common divisor of two integers. It’s definition is below.

gcd(x,y)

Computing various examples


So how does it work? Well, let’s follow the definition, but before doing that, do you remember how to compute the remainder of two integers in c++? If you don’t, well it’s the % (mod) operator.

// Computing the gcd of 45 and 9 //
gcd(45, 9)
= gcd(9, 45 % 9 ) = gcd(9, 0)
= 9

// Computing the gcd of 54 and 12 //
gcd(54, 12)
= gcd(12, 54 % 12) = gcc(12, 6)
= gcd(6, 12 % 6) = gcd(6, 0)
= 6

Read more »

Beej’s Guide to Network Programming

March 2nd, 2010 | No Comments | Posted in Network, Programming by Diego | - [Full Entry]

Learn Network Programming


Learn network programming with Beej’s Guide to Network Programming. For those wanting to learn this guide is a great choice. The guide is concise and makes learning networking programming a lot easier. It is not by any means a complete guide, but it’ll help you enough so you could start on your own.


But why learn network programming? At the time of this writing, I’ve been taking a course in Networks and we’ve already programmed quite a simple terminal based instant messaging client in C. You can join and login to a network, create buddies, and have conversations with them. If I was given more time, I would’ve implemented a gui but since we were given a one week period to work on it I didn’t have as much freedom as I had hoped for. I did use the following guide to understand network programming and I don’t think I could have found a faster and easier way to learn than this.

Beej’s Guide to Network Programming

Lighthouse – Short Movie

February 25th, 2010 | No Comments | Posted in Blog, Videos by Diego | - [Full Entry]

Lighthouse, a short movie


I wanted to share something with all of you today. It might not make a whole lot of sense today why I’m showing you this, but if you stick around you’ll have the opportunity to find out the importance of this video. Trust me, it’s one of those videos that make you feel good inside by the time it’s over and at 2:00 minutes long, you really can’t say no. Can you? Well, let’s get on with it.


Read more »

C++ Recursion – Fibonacci

February 23rd, 2010 | No Comments | Posted in C++ by Diego | - [Full Entry]

The Fibonacci Sequence


Below is an example of how to create the recursive version of the Fibonacci sequence. But first of all, what is it?

* This tutorial assumes you understand the following:

Fibonacci is a well known number sequence that models the growth of a rabbit population amongst other things found in nature.

Below is the Fibonacci numbers computed up to the 11th term.

0 1 2 3 4 5 6 7 8 9 10 11
0 1 1 2 3 5 8 13 21 34 55 89

Iterative version of Fibonacci


Before we create the recursive version of Fibonacci, let’s look at how to create the iterative version by looking at the formula.

Fibonacci Formula

Fibonacci(0) = 0;
Fibonacci(1) = 1;
Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2);

So how would we do an iterative version? I’ll walk you through the code.

// The value of n is the Fibonacci number we want to compute //
int fibonacci(int n) {

    //Our two initial values
    int fib1 = 0;
    int fib2 = 1;

    //Our running value
    int fib = fib1;

    // Computing the nth value of Fibonacci //

   return fib;
 

So how would we compute the value of the nth value of the Fibonacci sequence? We can do it with a for loop, to add UP to the value we are looking for. In other words, if we were doing it by hand, we’d do it as following:
Read more »

How to install Ubuntu using VirtualBox

February 22nd, 2010 | No Comments | Posted in Linux by Diego | - [Full Entry]


How to install Ubuntu using VirtualBox

Today, we are going to teach you how to use VirtualBox to install Ubuntu using Windows. (This could easily be done on Linux, or a Mac too.) This is a great option for those of you who want to get a programming environment and those who want to avoid dual-booting your computer since it involves a great risk if done wrong. Virtualizing an OS is an easy task and it only takes a few steps. It should take a new user about five minutes to get going. So let’s get started.

What you need to virtualize Ubuntu on VirtualBox


First of all you will need VirtualBox and an OS, for our tutorial we will be going with Ubuntu. For Ubuntu, you can simply download the Ubuntu CD image on their download page. They are both free of course. You can choose any other top linux distribution if you want, only one step would change in this process. Most of the instructions are relatively straight forward, so I’ll guide you through them so you don’t get lost.

By the time you finish this quick and easy process, you should be getting this type of environment (version may be different of course).

Step 1: Create a new Virtual Machine


Open up VirtualBox and click on the blue button that says new so you can be greeted with the following.


Read more »

Page 1 of 1712345...Last »