comp.lang.ada
 help / color / mirror / Atom feed
From: willmann817 <willmann817@gmail.com>
Subject: Algorithms Homework Help?!?!
Date: Tue, 15 Jan 2013 13:52:44 -0800 (PST)
Date: 2013-01-15T13:52:44-08:00	[thread overview]
Message-ID: <fd8f40f1-8d31-4a99-8828-16e155d045d4@googlegroups.com> (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;



             reply	other threads:[~2013-01-15 21:52 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-15 21:52 willmann817 [this message]
2013-01-15 22:50 ` Algorithms Homework Help?!?! 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
replies disabled

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