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

前端 未结 7 1626
广开言路
广开言路 2021-01-24 08:12
#include 

int* fib(int);

int main()
{
    int count;
    std::cout<<\"enter number up to which fibonacci series is to be printed\"<

        
相关标签:
7条回答
  • 2021-01-24 08:51

    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 讨论(0)
  • 2021-01-24 08:53

    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.

    0 讨论(0)
  • 2021-01-24 08:53

    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.

    0 讨论(0)
  • 2021-01-24 08:53

    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;  
        }
    
    0 讨论(0)
  • 2021-01-24 08:56

    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;
    
    0 讨论(0)
  • 2021-01-24 09:03

    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.

    0 讨论(0)
提交回复
热议问题