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

前端 未结 7 1642
广开言路
广开言路 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: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

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

提交回复
热议问题