Board index » delphi » what this program doing ?

what this program doing ?

program try1;
 type posint=1..maxint;
 var
 num:posint;

 function F2(num1,num2:real):real;
  begin
   f2:=sqrt(sqr(num1)+sqr(num2))
   end;

 function Fn(n:posint):real;
  var N1,N2:posint;
  begin
   if(n=2)
    then
     begin
      read(n1,n2);
      fn:=f2(n1,n2)
     end

     else
      begin
       read(n1);
       fn:=f2(n1,fn(n-1))
      end;
    end;

begin
 read(num);
 write(fn(num):8:6);
 readln;
 readln;

end.

 

Re:what this program doing ?


Quote
Haggai Reznik <sa...@ravdata.com> wrote:
>program try1;

It appears to be either getting stuck calling itself
recursively, or bombing out.

Jud McCranie

Re:what this program doing ?


Quote
"Mr. Ryan" <neo_neo_n...@hotmail.com> wrote:
>1) This program works well.
>2) First, you input an integer, say, N.
>3) Then you input N real numbers, say, {n1 n2 n3 n4 n5 ...}
>   each followed by <enter>

I put in a 1, then I put in a 2, and it didn't work for me.

Jud McCranie

Re:what this program doing ?


1) This program works well.
2) First, you input an integer, say, N.
3) Then you input N real numbers, say, {n1 n2 n3 n4 n5 ...}
   each followed by <enter>
4) The program returns ( sqrt (n1*n1 + n2*n2 + n3*n3 + ....) )
5) This result has some physical significance... that i don't know.

Haggai Reznik ???g??31 <386530DA.4F602...@ravdata.com>...

Quote
>program try1;
> type posint=1..maxint;
> var
> num:posint;

> function F2(num1,num2:real):real;
>  begin
>   f2:=sqrt(sqr(num1)+sqr(num2))
>   end;

> function Fn(n:posint):real;
>  var N1,N2:posint;
>  begin
>   if(n=2)
>    then
>     begin
>      read(n1,n2);
>      fn:=f2(n1,n2)
>     end

>     else
>      begin
>       read(n1);
>       fn:=f2(n1,fn(n-1))
>      end;
>    end;

>begin
> read(num);
> write(fn(num):8:6);
> readln;
> readln;

>end.

Re:what this program doing ?


It returns sqrt(n1*n1+n2*n2+...+nm*nm)?

It seems to be a stupid pythagoras, if you ask me:

a2 + b2 = c2
gives sqrt c = sqrt (a2 + b2)

simple as that

-Oliver Albers

Re:what this program doing ?


Quote
> >1) This program works well.
> >2) First, you input an integer, say, N.
> >3) Then you input N real numbers, say, {n1 n2 n3 n4 n5 ...}
> >   each followed by <enter>

Right. Afaik the length of a N-dimensional orthogonal (?) vector.

Quote
> I put in a 1, then I put in a 2, and it didn't work for me.

That is a bug, you are sharp today. The programmer creates a
special case for 2 (end of recursion), but doesn't consider values
lower than 2.

Other Threads