A apresentação está carregando. Por favor, espere

A apresentação está carregando. Por favor, espere

Estrutura de Dados Unidade 16 Simulação da função Fatorial utilizando recursão – 16.1 Prof. Dr. Roberto Ferrari Jr.

Apresentações semelhantes


Apresentação em tema: "Estrutura de Dados Unidade 16 Simulação da função Fatorial utilizando recursão – 16.1 Prof. Dr. Roberto Ferrari Jr."— Transcrição da apresentação:

1 Estrutura de Dados Unidade 16 Simulação da função Fatorial utilizando recursão – 16.1 Prof. Dr. Roberto Ferrari Jr.

2 16.1 : Fatorial por Recursão Nesta simulação, um programa principal fará a chamada de uma função fatorial(Fat), que foi construída utilizando recursão, conforme exemplo abaixo. function fat( n ); begin if n = 0 then fat := 1 else fat := n * fat (n-1); end; // fechamento da função Chamada recursiva: isto é, a função chama ela mesma, com novo valor N-1 Saída da recursão, pois retorna um valor, não chamando a mais a própria função

3 16.1 : Fatorial por Recursão Vamos rodar um exemplo...

4 program calcfat; var n, fatorial : integer; begin writeln(Entre com N : '); readln(n); if (a<=0) then writeln(Valor ilegal para N !); else begin fatorial := fat(N); writeln(Fatorial de N = ', fatorial); end; end. 16.1 : Fatorial por Recursão function fat( 2 ); begin if n = 0 then fat := 1 else fat := 2 * fat (2-1); 1end; function fat( 1 ); begin if n = 0 then fat := 1 else fat := 1 * fat (0); end; function fat( 0 ); begin if n = 0 then fat := 1 else fat := 2 * fat (0-1); end; >calcfat Entre com N : Fatorial de N = 6 > fat(3); 3 1 1 * 1 function fat( 3 ); begin if n = 0 then fat := 1 else fat := 3 * fat (3-1); end; 2 * 1 * 1 3 * 2 * 1 * 1


Carregar ppt "Estrutura de Dados Unidade 16 Simulação da função Fatorial utilizando recursão – 16.1 Prof. Dr. Roberto Ferrari Jr."

Apresentações semelhantes


Anúncios Google