#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?
Several issues with your code
should actually be
and
must read
And remove the line
since it does not have any effect after the
returnstatement. Additionally you can get rid of the instantiationin
main()since this is done in yourfibfunction also. As it stands, you leak the memory you just allocated.