< Browse > Home

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 »

Updates for coming weeks

February 21st, 2010 | No Comments | Posted in Blog by Diego | - [Full Entry]


What’s going on at Talk Binary

You might be wondering, what’s up with all these posts on recursion? I’m trying to make a small set of examples a newcomer might find helpful in order to learn this subject. Then, once I finish with a couple more examples, I plan on visiting pointers and my personal favorite, segmentation faults.

I also plan on writing more tutorials on anything programming related. So if you have anything you’d like to see, feel free to post it in the comments section or shoot me an e-mail. I’d really love to know what you have to say.

Are you new to Talk Binary?


If you are new, you might wonder what Talk Binary is all about. The main goal is to teach you how to talk in binary. Ok, maybe not necessarily in 1’s and 0’s but the language necessary for you to program and learn the ins and outs of computer science. So if you are interested in learning new topics in programming, C++, or anything programming related make sure to Subscribe to my feed, Subscribe via e-mail, or simply keep coming back!

So what now?


If you also haven’t noticed, I’ve been trying to find a structure for creating tutorials so maybe later on I could grab them and make an e-book. Now wouldn’t that be great?
Read more »

C++ Recursion – Printing Elements of a Vector

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

Printing elements of a Vector using Recursion


Below is an example of how to print the elements of a vector using recursion. Later, we’ll print the elements in reverse. Why is this useful? You can apply this principle later to more complex data structures such as Binary Search Trees, or Linked Lists.

This tutorial assumes you understand the following:

Iterative version for printing elements of a vector


To understand how to print elements of a vector using recursion, let’s write a simple algorithm that will do it for us using iteration.

void printVector(vector <int> vec) {
   
     for ( int i = 0; i < vec.size(); i++ ) {

          //Printing ith element  followed by a space
          cout << vec[i] << " ";

     }

     //Printing end line
     cout << endl;
}

Read more »

C++ Recursion – Power Continued

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

Creating a power function using Recursion


So, previously we tried computing the Power function using recursion but we didn’t take into account when the exponent was negative. This continues the previous function, so if you haven’t yet, go back. Make sure you understand the following as well:

  1. C++ Recursion
  2. C++ Recursion – Summation
  3. C++ Recursion – Factorial

Power Function using Recursion


Below is our previous power function which takes into account only positive exponents. So how would we go on by taking into account negative exponents?

int power(int base, int exp) {
     
     //Return 1 when n is 0
     if ( n == 0 )
          return 1;
     else if ( n == 1 )
           return base;
     else
          return base * power(base, exp-1);
     int result = base;

}

Let’s start by coming up with a formula again.

power(2,-1) = (1/2);

power(2,-2) = (1/2) * power(2, -1) = (1/2) * (1/2) = (1/4);

power(2,-3) = (1/2) * power(2,-2) = (1/2) * (1/4) = (1/8);

power(2,-4) = (1/2) * power(2,-3) = (1/2) * (1/8) = (1/16);

//Until exp == -1
power(base, -exp) = (1/base) * power(base, exp+1);

Taking into account negative exponents


So now that we have a formula, what would be our base case? We surely don’t want our previous base case to return 1 when (n==0) because that would be wrong.
Read more »

Using Workspaces in Ubuntu

February 10th, 2010 | No Comments | Posted in Linux, Tips & Tricks by Diego | - [Full Entry]

Workspaces allows you to organize your GNOME Desktop in Ubuntu by creating virtual desktops from which you can place your applications. In my case, I like having two files I’m currently working on my first workspace, my browser in the second, and so on. That way, I only have to switch through the spaces instead of having to bring up the windows individually.

This in turn increases productivity since Workspaces allows you to focus on things that you need to get done, instead of sifting through your applications on simply one screen.

Setting up Workspaces in Ubuntu

To set up a Workspace you can simply right click on a panel and choose Add to panel and select Workspace Switcher. This by default should add four Workspaces to wherever you clicked on your panel. To change this, simply right click your Workspace Switcher, and choose Preferences

So now that you have them you can switch to them by clicking on them, or preferably by pressing CTRL + ALT + RIGHT, or instead of RIGHT: LEFT, UP, or DOWN would be fine depending on how you set up your rows and columns and where you currently are.
Read more »

How to set the Color Scheme in vim

February 9th, 2010 | No Comments | Posted in Linux, Tips & Tricks by Diego | - [Full Entry]

I’ve been using vim (or better yet, gvim) for the better part of my undergraduate programming career and for quite some time I struggled on figuring out how to set the default color scheme for gvim under Ubuntu. Well, now that I figured it out, I wanted to share it with you.

Setting the default Color Scheme in vim (gvim)


To set your colorscheme in gvim to a theme of your choice, simply edit the file named .gvimrc. It can be opened using the following in your terminal. If it doesn’t exist, simply create it.

gvim ~/.gvimrc

Then after opening the file, add the following lines of code to the file if they don’t exist yet.

 
syntax on
colorscheme slate
 

Slate (left) and Evening(right)

But why Slate you must ask? Its the one mentioned on all the other tutorials. Yes, I know, but it simply rocks. Just check it out and you’ll understand. If not you can choose from any of these other themes.
Read more »