Hello!
I have to write some functions using different structures to determine the result of the Fibonacci sequence from a given number and all below. For example, if I want the result of the sixth therm of Fibonacci sequence, I have to print on screen the following text:
0 - F(0) = 0
1 - F(1) = 1
2 - F(2) = 1
3 - F(3) = 2
4 - F(4) = 3
5 - F(5) = 5
6 - F(6) = 8
I have already done using a recursive function, but I don'y know how to do using the do-while loop. Thank you.
----------------------------------------------------------------------------------------------------------
Code:
/* aula0301.h */
#ifndef _AULA_0301_
#define _AULA_0301_ "@(#)aula0301.h $Revision: 1.1 $"
typedef unsigned long long ull;
typedef unsigned short byte;
ull
CalcularTermoSerieFibonacci(byte);
#endif
----------------------------------------------------------------------------------------------------------
Code:
/* aula0301a.c */
#include "aula0301.h"
ull
CalcularTermoSerieFibonacci(byte numero)
{
if (numero == 0)
return 0;
if (numero == 1 || numero == 2)
return 1;
return (CalcularTermoSerieFibonacci(numero - 1) + CalcularTermoSerieFibonacci(numero - 2));
}
----------------------------------------------------------------------------------------------------------
Code:
/*aula0301b.c */
#include "aula0301.h"
ull
CalcularTermoSerieFibonacci(byte numero)
{
ull contador = 2;
do
{
contador = (contador - 1) + (contador - 2);
}
while (contador != numero);
return contador;
}
---------------------------------------------------------------------------------------------------------
Code:
/* aula0302.c */
#include <stdio.h>
#include <stdlib.h>
#include "aula0301.h"
#define NUMERO_ARGUMENTOS 2
#define OK 0
#define NUMERO_ARGUMENTOS_INVALIDO 1
#define ARGUMENTO_INVALIDO 2
#define EOS '\0'
int main(int argc, char *argv[])
{
unsigned short indice;
if (argc != NUMERO_ARGUMENTOS)
{
printf("Uso: %s <numero-inteiro-nao-negativo>\n", argv[0]);
exit(NUMERO_ARGUMENTOS_INVALIDO);
}
printf("\n");
for (indice = 0; indice <= atoi(argv[1]); indice++)
{
printf("F(%hu) = %llu\n", indice,
CalcularTermoSerieFibonacci((byte) indice));
}
printf("\n");
return OK;
}