Fibonacci Cycles

-5

the first one is this:

/ Determine how much is the sum of the elements of the fibonacci series between 0 and 100 /

#include<iostream>
using namespace std;

int main()
{
    int i,numero,x,y,z;

    numero = 11;
    x = 1;
    y = 1;
    z = 0;

    for(i=0;i<=numero;i++)
    {
        z = x + y;
        x = y;
        y = z;


    }
    cout << "La suma es: " << z;

}

this gives me a wrong value if I set it to i = 0 to 377, if I start i = 1, it gives me 233 and the final value should give 232.

the others are these 2 that I have no idea how to make them are these:

/ Determine how much the whole average of the elements of the fibonaci series equals between 0 and 1000. /

#include<iostream>

using namespace std;

int main()
{
    int x,y,z,numero,acumulado,i,promedio;

    x = 0;
    y = 1;
    z = 0
    numero = 16;
    acumulado = 0;
    promedio = 0;

    for(i=2;i<=numero;i++)
    {
        z = x + y;
        x = y;
        y = z;

        acumulado = acumulado + z;
    }
    cout << acumulado << endl;
    promedio = acumulado/numero;
    cout << "El promedio es: " << promedio;

    return 0;
}

/ Determine how many elements of the fibonnaci series are between 1000 and 2000 /

#include<iostream>
using namespace std;

int main()
{
    int contador,x,y,z;

    x = 0;
    y = 1;
    z = 0;

    while(z<=2000)
    {
        if(z>=1000)
        {
            contador++;
        }
        z = x + y;
        x = y;
        y = z;      
    }

    cout << "Hay " << contador << " elementos entre 1000 y 2000 de la serie fibonacci";


    return 0;
}

if you can explain them without the answer to be able to make them a guide or something (I'm just starting in this, I've been practicing since November of last year practicing, reading and looking for exercises, now I'm with the cycles, within 1 month I enter the university and I'm practicing).

    
asked by Mr.Miau 02.01.2019 в 18:18
source

1 answer

-1

The Fibonacci sequence is calculated according to the following formula:

There are two basic ways to deal with a problem of this style: Imperatively or recursively.

The iterative strategy is based on executing a fragment multiple times in a loop and modifying values in each iteration. This is the strategy you follow in your code snippet.

#include <iostream>
int main()
{
int x = 0;
int y = 1;
int t;
for(int i = 1; i < n;i++)
{
    t = x + y;
    x = y;
    y = t;
}
std::cout << y << std::endl;
return 0;
}

In this case, in the first iteration of the loop, x = 0 e y = 1 . t will be 1.

In the second iteration, x = 1 , y = 1 , t = 2 As you can see, the loop uses an auxiliary variable, t , to calculate the intermediate value. you must define n as the number of iterations or "turns" you want to give to the loop, in the case of your example, n = 100 .

The second option is to use recursion. This is simply calling a function within itself.

#include <iostream>
unsigned long long fib_rec(int n)
{
if (n<2)
    return n;
else
    return fib_rec(n-1) + fib_rec(n-2);
}
int main()
{
    int n = 100;
    std::cout << fib_rec(n) << std::endl;
    return 0;
}

As you see, we define a function fib_rec, which calls itself, but with the values n-1 and n-2. As you can see, this looks a lot like the mathematical definition of the function and is quite clear. In the case that n is 1, the code will enter the if and return 1, and if it is 0, it will return 0. Therefore, the execution of fib_rec (3), for example, would be:

fib_rec(3) = fib_rec(2) + fib_rec(1)
fib_rec(3) = fib_rec(1) + fib_rec(0) + fib_rec(1)
fib_rec(3) = 1 + 0 + 1
fib_rec(3) = 2
    
answered by 02.01.2019 / 20:14
source