Fórum funçao para fatorial... #167365

02/07/2003

0

USANDO VETOR, PRECISO UMA FUNÇAO QUE RETORNE O FATORIAL DE TODOS OS NUMEROS DO VETOR, ALGUEM PODERIA ME DIZER ONDE ESTOU ERRANDO?


OBRIGADO


function fatorial(x:integer): integer;
var
f, i:integer;
begin
f:=1;
for I:=1 to X do
f:=f *i;
fatorial :=f;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
A: array[1..5] of Integer;
n,I: Integer;
begin
A[1] := StrToInt(Edit1.Text);
A[2] := StrToInt(Edit2.Text);
A[3] := StrToInt(Edit3.Text);
A[4] := StrToInt(Edit4.Text);
A[5] := StrToInt(Edit5.Text);
n := A[I];
Label2.Caption := IntToStr(fatorial(n));
Edit1.SetFocus;
end;


Harry

Harry

Responder

Posts

02/07/2003

Fred

function fatorial(var a:array[1..5] of integer;
var i, r : integer
begin
r := 0;
for i := 1 to 5 do
begin
r := r*a[i];
end;
result := r;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
A: array[1..5] of Integer;
begin
A[1] := StrToInt(Edit1.Text);
A[2] := StrToInt(Edit2.Text);
A[3] := StrToInt(Edit3.Text);
A[4] := StrToInt(Edit4.Text);
A[5] := StrToInt(Edit5.Text);
Label2.Caption := IntToStr(fatorial(A));
Edit1.SetFocus;
end;
tente isto. 8)


Responder

Gostei + 0

02/07/2003

Jairroberto

Olá, Harry!

Não entendi muito bem o que você quer, pois há apenas um label mostrando o resultado e são 5 elementos no vetor para calcular o fatorial. Porém, foi possível identificar um erro no seguinte código:

procedure TForm1.Button1Click(Sender: TObject); 
var 
  A: array[1..5] of Integer; 
  n,I: Integer; 
begin 
  A[1] := StrToInt(Edit1.Text); 
  A[2] := StrToInt(Edit2.Text); 
  A[3] := StrToInt(Edit3.Text); 
  A[4] := StrToInt(Edit4.Text); 
  A[5] := StrToInt(Edit5.Text); 
  n := A[I]; // neste ponto I vale zero, por isso não há resultado
  Label2.Caption := IntToStr(fatorial(n)); 
  Edit1.SetFocus; 
end;


Você poderia alterar o código para:
procedure TForm1.Button1Click(Sender: TObject); 
var 
  A: array[1..5] of Integer; 
  n,I: Integer; 
begin 
  A[1] := StrToInt(Edit1.Text); 
  A[2] := StrToInt(Edit2.Text); 
  A[3] := StrToInt(Edit3.Text); 
  A[4] := StrToInt(Edit4.Text); 
  A[5] := StrToInt(Edit5.Text); 

  for I := Low(A) to High(A) do
  begin
     n := A[I]; // agora I varia de 1 a 5
     // a seguir, cada Label (Label1, Label2, ...) mostra o fatorial de cada elemento do vetor
     TLabel(FindComponent(Format(´Label¬d´, [I]))).Caption := IntToStr(fatorial(n)); 
  end;
  Edit1.SetFocus; 
end;



Um abraço,
Jair


Responder

Gostei + 0

02/07/2003

Marconi

function fatorial(X :integer); // Aqui voce não precisa da Matriz
var i, r : integer
begin
if X<3 then result:=X; //
else begin
r := 2;
for i := 3 to X do begin
r := r*i;
end;
end;
result := r;
end;

{ se voce deixar o r inicial = 0, por mais que multiplique vai dar sempre zero}


Responder

Gostei + 0

02/07/2003

Marconi

Continuando

Quando voce fez a chamada seguinte

n := A[I];
Label2.Caption := IntToStr(fatorial(n));

Voce não tinha dado nenhum valor para I ainda. Ele ainda esta com o valor NULL, e vai dar erro na certa.

Marconi


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar