comp.lang.ada
 help / color / mirror / Atom feed
* Algorithms Homework Help?!?!
@ 2013-01-15 21:52 willmann817
  2013-01-15 22:50 ` Adam Beneschan
                   ` (5 more replies)
  0 siblings, 6 replies; 13+ messages in thread
From: willmann817 @ 2013-01-15 21:52 UTC (permalink / raw)


I am given an Array that contains the number of M&Ms of each color I have.  Each index in the array represents a different color.  I am also given an Integer of the number of children I am having at a birthday party. 

--THE CHILDREN MUST ALL HAVE THE SAME NUMBER OF M&MS
--EACH CHILD MUST ONLY HAVE 1 COLOR OF M&MS

Given an array containing the number of M&Ms I have in each available color, and the number of kids, determine the maximum number of M&Ms you can put in each goody bag. 
For example, with 11 red M&Ms, 9 blue M&Ms, and 5 M&Ms, you could give 5 children a maximum of 4 M&Ms each. You could not give each child 5 M&Ms without at least one child receiving M&Ms of different colors. 

  I was given a hint that said to write a helper function Groups(M : Integer; A : Int_Array) that takes a number of M&Ms and the array and returns how many groups I can make of M pieces of M&Ms. 

(For example, with 11 red bricks, 9 blue bricks, and 5 green bricks, Groups(3) would return 7.)

Then do my binary search based on this helper function(Posted below). 
--This problem MUST include Binary Search in some way.

I completed that but now I am stuck on the main function(also posted below) it is totally wrong cause I can't even pass the first test case:

	function Groups(Bricks : Integer; A: Int_Array) return Integer is
	counter : Integer := 0;
	begin
		for I in A'Range loop
			counter := counter + (A(I)/Bricks);
		end loop;
	return counter;

   function Candy(Kids : Integer; MsPerColor : Int_Array) return Integer is	
	lo : Integer := 1;
	hi : Integer := 50000;
	mid : Integer;
	begin
		for I in MsPerColor'Range loop
			if  MsPerColor(I) > hi then
				hi :=  MsPerColor(I);
			end if;
			if  MsPerColor(I) < lo then
				lo :=  MsPerColor(I);
			end if;
		end loop;

		mid := (lo+hi)/2;
		while Groups(mid, MsPerColor) /= Kids loop
			mid := (lo+hi)/2;
			if Groups(mid, MsPerColor) > Kids then
				hi := mid;
			else
				lo := mid;
			end if;
		end loop;
	return mid;
   end Candy;
	end Groups;



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2013-02-07  7:10 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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