0

What the title says, cout will not print anything! But main finished normally and returns a negative number, what's going on? Here is my code:

#include <iostream>
using namespace std;
unsigned long fibonacci(long unsigned int *);
int main(void)
{
    cout<<"IT WILL NOT PRINT!!!!!";
    unsigned long int fib[4000000];
    cout<<"SUM OF EVEN FIBONACCI NUMBERS: "<<fibonacci(fib)<<endl;
    return 0;
}
unsigned long fibonacci(unsigned long int *FIBO)
{
    unsigned long i;
    int sum=0,c=0, *EVEN = new int[2000000];
    FIBO[0]=1; FIBO[1]=2;
    for (i=2;i<3999999;i++){
        FIBO[i]=FIBO[i-1]+FIBO[i-2];
        if (FIBO[i]%2==0){
            EVEN[c]=FIBO[i];
            sum+=EVEN[c];
            c++;
        }
    }
    delete [] EVEN;
    return sum;
}
Santiago Benoit
  • 994
  • 1
  • 8
  • 22

3 Answers3

1

std::cout by default is buffered. Without an explicit flush, you won't see anything printed until the internal buffers need to be flushed. This is done for performance reasons.

You can add a specific flush, like so: std::cout<<"IT WILL NOT PRINT!!!!!" << std::endl;

That said, you're not seeing output because your program is crashing.

unsigned long int fib[4000000]; is going to require nearly 15MB of space (on a 32bit long int platform). There simply won't be enough stack space to allocate such a large block of memory within that storage duration.

For such large blocks you will need to dynamically allocate the block, or better yet:

std::vector<unsigned long int> fib(4000000);

Chad
  • 18,706
  • 4
  • 46
  • 63
1

Let's take a look at this problem: we want to produce a sum of even Fibonacci numbers within a predefined range.

Rearranged with minor edits for clarity:

#include <iostream>
using namespace std;
unsigned long fibonacci(unsigned long int *FIBO)
{
    unsigned long i;
    int sum=0,c=0, *EVEN = new int[2000000];
    FIBO[0]=1; FIBO[1]=2;
    for (i=2;i<3999999;i++){
        FIBO[i]=FIBO[i-1]+FIBO[i-2];
        if (FIBO[i]%2==0){
            EVEN[c]=FIBO[i];
            sum+=EVEN[c];
            c++;
        }
    }
    delete [] EVEN;
    return sum;
}
int main(void)
{
    unsigned long int fib[4000000];
    cout << "SUM OF EVEN FIBONACCI NUMBERS: " << fibonacci(fib) << endl;
    return 0;
}

First, let's remove unneeded code in the function fibonacci. We don't need to store even numbers, so we'll remove the EVEN array.

unsigned long fibonacci(unsigned long *FIBO)
{
    unsigned long i, sum=0;
    FIBO[0]=1; FIBO[1]=2;
    for (i=2;i<3999999;i++){
        FIBO[i]=FIBO[i-1]+FIBO[i-2];
        if (FIBO[i]%2==0){
            sum += FIBO[i];
        }
    }
    return sum;
}

We can get a bit more clever though- we can prove that every third Fibonacci number will be even. Starting from Fib(0)=0, this allows us to make a new function:

unsigned long sum_even_fibonacci(unsigned long n)
{
    unsigned long i, a=0, b=1, c=1, sum=0;
    for (i=0;i<n/3;i++){
        a = b + c; // Fib(3i)
        b = a + c; // Fib(3i+1)
        c = a + b; // Fib(3i+2)
        sum += a;  // Fib(3i) will always be even- add it to sum.
    }
    return sum;
}

This should produce the sum of the even Fibonacci numbers within the first n numbers of the series.

The new program, in full:

#include <iostream>
using namespace std;
unsigned long sum_even_fibonacci(unsigned long n)
{
    unsigned long i, a=0, b=1, c=1, sum=0;
    for (i=0;i<n/3;i++){
        a = b + c; // Fib(3i)
        b = a + c; // Fib(3i+1)
        c = a + b; // Fib(3i+2)
        sum += a;  // Fib(3i) will always be even- add it to sum.
    }
    return sum;
}
int main(void)
{
    cout << "SUM OF EVEN FIBONACCI NUMBERS: " << sum_even_fibonacci(4000000) << endl;
    return 0;
}
rwjb
  • 21
  • 3
1

But main finished normally and returns a negative number,

In your code main returns 0 and that is the only return path. So, the process exiting with a negative return code means that main() never finished.

Most likely, the array unsigned long int fib[4000000]; is too large for your system's default stack size, and the operating system handles this by making your process exit with that negative number.

To fix this, either make that array a lot smaller or remove it entirely; or use dynamic allocation.

Note that there is no need to even have this array (and no need for EVEN either). You're keeping the sum as you go, so you only need to keep the last 2 numbers ; not the entire history.

M.M
  • 138,810
  • 21
  • 208
  • 365