开发者

How to return an array from a function and loop through it?

#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;
开发者_Go百科        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

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.


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.


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;


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.


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;  
    }


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.


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++)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜