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

自古美人都是妖i 提交于 2019-12-02 05:07:54
Howard

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++)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!