Page 1 of 1

C --> how can I get bigger variables sizes?

Posted: Sun Oct 30, 2005 2:35 pm
by 0Scoder
I am currently coding a program that does some manipulating of the fibonacci series, but I have had problems with numbers being to large for the variable they are held in! I have attached the coe here. Is there a way someone can suggest that I can store the rest of the sequence without an overflow (I need to go up to 100).

Code: Select all

#include <stdio.h>

int main()
{
//create cariables
   unsigned long long sequence[100];
   int current_n;
   int i;
   char input[256];
   
//initialise variables
   sequence[0]=0;
   sequence[1]=1;

   for(i=2; i<25; i++)
    sequence[i]=sequence[i-1]+sequence[i-2];
   
   for(i=2; i<25; i++){
    printf("%d \n",sequence[i]);}

   scanf("%s", input);
 return;   
}
Thanks in advance,
OScoder

Re:C --> how can I get bigger variables sizes?

Posted: Sun Oct 30, 2005 4:43 pm
by QuiTeVexat
Make some structs to represent numbers with all the precision you need, and then write some functions to manipulate them. For this, add() and print() would probably suffice. Set your array of structs up, and blissfully add() and print() them to your heart's content. Not very elegant, but it'll work.

Oh, and *please* return 0; Your compiler should be complaining. Whatever anyone tells you, main() is does *not* have void return type.

Pax tecum,
Qui te vexat.

Re:C --> how can I get bigger variables sizes?

Posted: Mon Oct 31, 2005 2:42 am
by Solar
Check out whether any of the available bigint libs (e.g. http://www.eskimo.com/~eresrch/fast_onb/bigint.c) suits your tastes.