0

I have written a program in c++ to output the number in the fibonacci sequence you tell it to. It works up until about the 47th number, after that it prints out completely different numbers, even negatives, and none of them have more than 9 or 10 individual integers. Here is the code

#include <iostream>

int a = 0;
int b = 1;
int c;
int var = 0;
int num;

void fibonacci()
{
     using namespace std;
     c = a;          
     a = a + b;           
     b = c;
     var += 1;
}

void loop()
{    
    using namespace std;
    for (int iii=0; iii<num; iii++)
    fibonacci();
}

int main()
{
    using namespace std;
    cout << "What fibonacci number do you want to know? ";
    cin >> num;
    cin.get();
    loop();
    cout << "" << endl;
    cout << c << endl;
    cin.get();

    a = 0;
    b = 1;
    var = 0;

    return main();
}

Is there some sort of limit on how many characters c++ can print at once?

WillumMaguire
  • 537
  • 2
  • 10
  • 21
  • How many characters do you need ? You could just use `long` or `long long`. Types have limits in C++ (and most if not all other languages as well). – ApplePie Sep 16 '12 at 14:55
  • What data type could get me the maximum amount of characters? – WillumMaguire Sep 16 '12 at 15:00
  • 3
    I'm trying to figure out why you have `using namespace std;` in functions that don't even use the namespace. – chris Sep 16 '12 at 15:03
  • `std::cout << std::numerical_limits::max() << "\n";` – Martin York Sep 16 '12 at 15:03
  • @WillumMaguire, Something like [GMP](http://gmplib.org/) can be good for going as high as you want. Other than that, a `uint64_t` should be an unsigned 64-bit type, and I think Visual Studio even has a compiler-specific 128-bit one. – chris Sep 16 '12 at 15:05
  • I'm very new to c++, I don't know specifically where namespace std is needed – WillumMaguire Sep 16 '12 at 15:09
  • After you `#include ` and before you use things in the `std` namespace like `cout`. Ideally, after all of the `#include`s and not inside a function. – Jonathan Seng Sep 16 '12 at 16:09

5 Answers5

6

Fib(47) = 2971215073 which is greater than 2147483647.

std::numeric_limits<int>::max() == 2^31 - 1 == 2147483647 in your case.

Your result integer therefore overflows at num == 47.

@jogojapan added:

sizeof(int) is 4 bytes (on many platforms, that is), which is 32 bits. 2^31-1 [numeric_limits::max()] is the largest number that can be presented using an int (on those platforms).

nullpotent
  • 9,162
  • 1
  • 31
  • 42
  • The answer is informative, although wrong as `int` is signed, so the max will be `2^31-1` – David Rodríguez - dribeas Sep 16 '12 at 15:01
  • @DavidRodríguez-dribeas Thank you very much. I rushed a bit. – nullpotent Sep 16 '12 at 15:03
  • `sizeof(int)` is 4 bytes (on many platforms, that is), which is 32 bits. `2^31-1` is the largest number that can be presented using an `int` (on those platforms). – jogojapan Sep 16 '12 at 15:09
  • @iccthedral Sure, that's fine; I made the comment mainly because you have `sizeof(int)==2^31-1`, which seems to confuse `sizeof(int)` with MAX_INT. Of course we all know what you mean, but still. – jogojapan Sep 16 '12 at 15:14
  • Nice use of `std::numeric_limits<>` which can be used at runtime to detect how close you are to limits. – Jonathan Seng Sep 16 '12 at 23:26
1

Increase your data type size/capacity from an int to an unsigned long or unsigned long long. Each of these has a size which, when exceeded, creates a bit pattern which will create an almost certainly incorrect value. Also, since your number can never be negative, allowing part of the number's size to be used for negative values (which is a bit) wastes the maximum number you can represent.

Also, you should add a check to detect this case. If a value ever goes down in this algorithm, you have clearly exceeded the limits of your data type. At this point, print a polite error that you cannot calculate the number because it is too large.

Jonathan Seng
  • 1,219
  • 7
  • 20
0

Your integer is overflowing. Google "integer overflow" and a signed int's capacity. Try changing it to an unsigned int and you will notice it will overflow at a higher number.

BSull
  • 329
  • 2
  • 10
0

Here is my code to output Fabonica series in C:

    #include<stdio.h>
    int main(){
    int k,r;
    long int i=0l,j=1,f;

    //Taking maximum numbers form user
    printf("Enter the number range:");
    scanf("%d",&r);

    printf("FIBONACCI SERIES: ");
    printf("%ld %ld",i,j); //printing firts two values.

    for(k=2;k<r;k++){
         f=i+j;
         i=j;
         j=f;
         printf(" %ld",j);
    }

    return 0;
}

Sample output:

Enter the number range: 15

FIBONACCI SERIES: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

Fibonacci series using array in c

 #include<stdio.h>
int main(){
int i,range;
long int arr[40];

printf("Enter the number range: ");
scanf("%d",&range);

arr[0]=0;
arr[1]=1;

for(i=2;i<range;i++){
     arr[i] = arr[i-1] + arr[i-2];
}

printf("Fibonacci series is: ");
for(i=0;i<range;i++)
     printf("%ld ",arr[i]);


    return 0;
}
Arjun Sunil Kumar
  • 1,781
  • 3
  • 28
  • 46
-1

This is not because of a limit on the number of digits cout can handle. It is because your numbers are to big to fit in int. Try using long or long long for a, b and c. Here is a good reference on different data types.

Sapan Diwakar
  • 10,480
  • 6
  • 33
  • 43