Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Tuesday, August 26, 2008

Fibonacci and the C++ conundrum (part deux)

A few days ago, I posted a C++ program that I wrote that was supposed to calculate the fibonacci sequence. It worked up to the 46th number, and then things went a little wonky. I started getting negative numbers for some odd reason. (See what happened: HERE)

'War&' left me a comment saying that I should change my 'int's (variables assigned as integers) to 'long's (variables assigned as long numbers, which are capable of storing more information),

Friday, August 15, 2008

Fibonacci and the C++ conundrum




DISCLAIMER: the following Blog post involves computer programming. If this sort of thing bores you or confuses you, you might as well not read any further and instead I advise you to check out something nonsensical like LOLCATS. I wouldn't want to ruin your interweb experience.
---------------------------------------------------------------------

I was wondering how difficult it would be to write a program that would calculate the fibonacci sequence. After giving it a bit of thought, I decided that it didn't seem too hard, so I wrote the program using C++ and compiled the code using Dev C++.




The code seems simple enough. It looks like this:



#include iostream
#include stdlib.h
using namespace std;

int main(){

int first, second, num1,num2,num3;
int index=1;

first=1;
second=1;

do{
num1=first;
num2=second;

num3=num1+num2;

cout<< num1 <<" ";
index++;

first=num2;
second=num3;

}while(index<=45);

cin.get();
return 0;
}



Unfortunately.... things didn't go EXACTLY as planned....