1
#include <iostream>

int* fib(int);

int main()
{
    int count;
    std::cout<<"enter number up to which fibonacci series is to be printed"<<std::endl;
    std::cin>>count;
    int *p=new int[count];
    p=fib(count);
    int i;
    for(i<0;i<=count;i++)
        std::cout<<p[i]<<std::endl;
    return 0;
}

int* fib(int d)
{
    int *ar=new int[d];
    int p=-1,q=1,r;
    int j;
    for(j=0;j<=d;j++)
    {
        r=p+q;
        ar[j]=r;
        p=q;
        q=r;
    }
    return ar;
    delete ar;
}

Why am I not able to print the whole array of Fibonacci series in this way?

6
  • 1
    What problems are you seeing? Compiler errors? If so, which ones? Unexpected results? If so, what results were you expecting and what do you get instead? Commented May 8, 2011 at 11:04
  • well, where to start... first, what does it mean "not able" - what happens? Also, there are some problems in your code: 1) you don't need to allocate the array in main - it causes memory leak as you immediately override the pointer; 2) the delete ar statement in fib() is never executed as it is after return, but if it where you'd lost the data you've just computed Commented May 8, 2011 at 11:08
  • no error is coming.but i don't get the series.can you please check it on your compiler?i am using gcc. Commented May 8, 2011 at 11:17
  • 1
    If you are "done with this question", please accept an answer and ask a new question. Commented May 8, 2011 at 11:37
  • done with this question.how can i extend this program to print fibonacci series between two numbers?please give some idea Commented May 8, 2011 at 11:37

7 Answers 7

4

Several issues with your code

for(i<0;i<=count;i++)

should actually be

for(i=0;i<count;i++)

and

for(j=0;j<=d;j++)

must read

for(j=0;j<d;j++)

And remove the line

delete ar;

since it does not have any effect after the return statement. Additionally you can get rid of the instantiation

int *p=new int[count];

in main() since this is done in your fib function also. As it stands, you leak the memory you just allocated.

Sign up to request clarification or add additional context in comments.

5 Comments

@David well this can be discussed. I can not read this out of the problem spec ;-) It gives the first n numbers starting with 0, 1. Thus it all depends on the definition of fib(n).
the loops are fine so long as n+1 elements are allocated
i have done all that you have said but couldn't get the series.can you please it on your compiler?
done with this question.how can i extend this program to print fibonacci series between two numbers?please give some idea
@Gautam: you are supposed to learn how to think. You should be able to work out how to do it (it arguably only takes one call to your existing fib() function - you just alter the way you do the printing).
2

Your i is not initialized. Instead of making it i = 0, you do i < 0. And in the j loop, the maximum number should be d. So j < d. Not j <= d.

1 Comment

done with this question.how can i extend this program to print fibonacci series between two numbers?please give some idea
1

The problem is exactly here:

int i;
for(i<0;i<=count;i++)
    std::cout<<p[i]<<std::endl;

You are not assigning i any start value. Change it to:

for (int i = 0; i < count; i++)
    std::cout << p[i] << std::endl;

1 Comment

how can i extend this program to print fibonacci series between two numbers?please give some idea
1

You are allocating one element too few. Your code to delete ar never runs because it follows the return. You also leak p because you overwrite the pointer with that returned by fib().

If I were you I would probably pass p to fib() and get fib() to fill out the array.

void fib(int n, int p[])
{
    p[0] = 1;
    p[1] = 1;
    for (int i=2; i<=n; i++)
        p[i] = p[i-2]+p[i-1];
}

Obviously this code requires n>=2 but I will leave error checking as an exercise to the reader!

To call it use code like this:

int p[] = new int[count];
fib(count, p);

If you want to print out values between i1 and i2, say, do it like this:

for (int i=i1, i<=i2, i++)
    std::cout<<p[i]<<std::endl;

Since you are using C++, all this code would be simpler with the C++ vector class.

Comments

1

Here is an example of fibonacci series, and I started mine off with a[0] = 1 and a[1] = 1. Fibonacci series normaly start with 0 and 1 but mine will start 1 and 1.

#include "stdafx.h" #include <iostream>

using namespace std;

int main()
{
    int a[25];
    int i,j;
    int fib[25];
    int fibs;
    char z;

    a[0] = 1;
    a[1] = 1;


        fib[1] = a[0];
        fib[2] = a[1];
        fibs = 0;

    for ( i=2; i<25; i++ )
    {
        fibs = fib[1] + fib[2];
        fib[1] = fib[2];
        fib[2] = fibs;
        a[i] = fibs;
    }

    for(i=0; i<25; i++)
    {
        cout << "a[" << i << "]=" << a[i] << endl;  
    }

Comments

0

One problem is that you allocate the array one element too short. The parameter in new[] is the number of elements, not the highest index.

Another problem is that you allocate the array in two places, but that only creates a memory leak and doesn't affect the result.

1 Comment

done with this question.how can i extend this program to print fibonacci series between two numbers?please give some idea
0

First, you don't have to allocate int *p=new int[count]; inside main, because you will recieve from the fib function a pointer to an already alocated memory.

Secondly, everything that is after a return statement is unreachable code, so you can either remove it, or move it before return.

Furthermore, if you delete the array inside fib function, you will return a null pointer.

And the main problem is at:

for(i<0;i<=count;i++)

whom correct for is:

for(i = 0; i <= count; i++)

2 Comments

done with this question.how can i extend this program to print fibonacci series between two numbers?please give some idea –
You must compute the series until you reach the first number, and then continue to compute and this time also store the values in the array until you reach the second number. Being a series, you cannot begin with a random number (or you will need 2 beginning numbers)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.