comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Re: Algorithms Homework Help?!?!
Date: Tue, 15 Jan 2013 15:43:19 -0800 (PST)
Date: 2013-01-15T15:43:19-08:00	[thread overview]
Message-ID: <8a4f61cd-9d6e-4bc5-b28c-c14e1ac2e096@googlegroups.com> (raw)
In-Reply-To: <fd8f40f1-8d31-4a99-8828-16e155d045d4@googlegroups.com>

Um, there's nothing there suggestive of a binary-search, or ANY search.
But here's an example of how to do the groupings.

Pragma Ada_2012;

With
Ada.Text_IO,
Ada.Text_IO.Text_Streams;

Procedure Test is

    Type Color is ( Red, Blue, Green, Yellow, Brown );

    Type MM_Array is Array(Color Range <>) of Natural;
    
    --------------------------
    -- Forward Declarations --
    --------------------------
    
    Function Maximum( Candy : MM_Array ) Return Natural
      with Pre => Candy'Length in Positive;
    
    Function Group( Group_Size : Positive; Candy : MM_Array ) Return Natural
      with Pre => Candy'Length in Positive AND Maximum(Candy) >= Group_Size;
    

    -----------------------
    --  Function Bodies  --
    -----------------------
    
    Function Maximum( Candy : MM_Array ) Return Natural is
    begin
	Return Result : Natural := Natural'First do
	    For Value of Candy loop
		Result:= Natural'Max( Result, Value );
	    End loop;
	End return;
    end Maximum;
    
    Function Group( Group_Size : Positive; Candy : MM_Array ) Return Natural is
    begin
	Return Result : Natural := 0 do
	    For Value of Candy loop
		Result:= Result + Value / Group_Size;
	    End loop;
	End return;
    end Group;
    
    Candies : MM_Array := (Red => 11, Blue => 9, Green => 5);
Begin
    Ada.Text_IO.New_Line;
    Ada.Text_IO.Put_Line( "Max:  " & Maximum(Candies)'Img  );
    Ada.Text_IO.Put_Line( "Group:" & Group(8, Candies)'Img  );
End Test;



  parent reply	other threads:[~2013-01-15 23:43 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-15 21:52 Algorithms Homework Help?!?! willmann817
2013-01-15 22:50 ` Adam Beneschan
2013-01-15 23:50   ` willmann817
2013-01-15 23:43 ` Shark8 [this message]
2013-01-15 23:54 ` Jeffrey Carter
2013-01-16  0:01   ` willmann817
2013-01-16  3:54     ` willmann817
2013-01-16 19:47 ` Robert A Duff
2013-01-16 21:19   ` Adam Beneschan
2013-01-16 22:27     ` Robert A Duff
2013-01-16 23:43       ` Adam Beneschan
2013-02-07  7:06 ` ajim
2013-02-07  7:10 ` ajim
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox