How to multiply N amount of floats using recursion?

Viewed 183

I'm practicing recursive problems for upcoming exams and have gotten stuck on this problem.

The program is supposed to take N amount of floats and multiply them together, all using recursion.

Example: The user inputs 2, then the program will take 2 floats from the user, say for example 2.0 and 3.0. Then the result is 6.0.

My attempted solution is the code below. The core problem that I have is that I don't know how I'm supposed to multiply the floats together in a way that works. Right now all my program does is that it takes N amount of floats and then just prints the last one without multiplying them all together. I do feel like the solution is simple but I really cannot find it. Would love some help on this one, thanks in advance.

    with Ada.Text_IO;         use Ada.Text_IO;
    with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
    with Ada.Float_Text_IO;   use Ada.Float_Text_IO;

    procedure Recprac3 is
       ---------------------------------- 
       procedure Calc(N : in Integer;
                    F : out Float) is
          Prod : Float;
       begin
          if N = 0 then
             Put("The product is: ");
             Put(Prod, 2, 2, 0);       -- 2, 2, 0 is formatting
          else                        
             Get(F);
             F := Prod;                -- <------- Problem area
             Prod := Prod * F;
             Calc((N-1), F);
          end if;
       end Calc;
       ----------------------------------    
    N : Integer;
    F : Float;

    begin
       Put("Get an integer: ");
       Get(N);
       Put("Get ");
       Put(N, Width => 0);
       Put(" floats: ");
       Calc(N, F);
    end Recprac3;
1 Answers

Ok, so let's break things down from the requirements, "The program is supposed to take N amount of floats and multiply them together, all using recursion.":

  1. Get an integer, N, from the user.
  2. Get N number of floats, from the user.
  3. Multiply the N floats, using recursion.
  4. [Implied] Display the result of the previous requirement.

Well, an obvious way to hold some number of items is the Array construct; now, Ada has two forms of this: the constrained and unconstrained, where the former has some definite length. Given that we are getting N from the user we cannot use the definite form.

So, we use something like: Type Vector is (Integer range <>) of Integer;.

Ada also gives some interesting attributes: Length, First, Last, Pred, Succ, and so on. There is also the notion of a "null range", which applied to an Array would indicate a length of zero; this gives us a good base case.

-- There IS a bug here, you have to find it.
Function Multiply( Input : Vector ) return Float is
   Subtype Internal is Integer 
      range Integer'Succ(Input'First)..Integer'Pred(Input'Last);
Begin
   Return (if Input'Length not in Positive then 1.0
           else Input(Input'First) * Input(Input'Last) * Multiply(Input(Internal))
          );
End;

Next, there's the problem of getting input, validating it, and using it; assuming there are functions of the form Function Input return Integer, and the same for Float, we could handle #1 and #2 in a single function:

Function Input return Float is
Begin
    Loop
        Ada.Text_IO.Put_Line( "Input a value:" );
        declare
            Text : String renames Ada.Text_IO.Get_Line;
        begin
            return Float'Value( Text );
        -- SOMETHING GOES HERE.
        end;
    end loop;
end Input;

Function Results return Vector is
Begin
   Return Result : Vector(1..Input):= (others => Input);
End;

That should be enough for you to complete your mission.

Note: Constructs herein will have to be adapted for your use.

Related