funçao para fatorial...
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;
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
Curtidas 0
Respostas
Fred
02/07/2003
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)
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)
GOSTEI 0
Jairroberto
02/07/2003
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:
Você poderia alterar o código para:
Um abraço,
Jair
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
GOSTEI 0
Marconi
02/07/2003
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}
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}
GOSTEI 0
Marconi
02/07/2003
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
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
GOSTEI 0