From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,PLING_QUERY autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,7d1a02b763945274 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit X-Received: by 10.66.86.138 with SMTP id p10mr12489585paz.14.1358290237934; Tue, 15 Jan 2013 14:50:37 -0800 (PST) X-Received: by 10.50.163.67 with SMTP id yg3mr1280877igb.12.1358290237874; Tue, 15 Jan 2013 14:50:37 -0800 (PST) Path: s9ni24pbb.0!nntp.google.com!ld4no133815pbb.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Tue, 15 Jan 2013 14:50:37 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ NNTP-Posting-Host: 66.126.103.122 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <6bf7934b-d736-4213-9786-45bd0a339cbf@googlegroups.com> Subject: Re: Algorithms Homework Help?!?! From: Adam Beneschan Injection-Date: Tue, 15 Jan 2013 22:50:37 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Date: 2013-01-15T14:50:37-08:00 List-Id: On Tuesday, January 15, 2013 1:52:44 PM UTC-8, willmann817 wrote: > 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 a= n Integer of the number of children I am having at a birthday party.=20 >=20 > --THE CHILDREN MUST ALL HAVE THE SAME NUMBER OF M&MS > --EACH CHILD MUST ONLY HAVE 1 COLOR OF M&MS >=20 > Given an array containing the number of M&Ms I have in each available col= or, and the number of kids, determine the maximum number of M&Ms you can pu= t in each goody bag.=20 >=20 > 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 wit= hout at least one child receiving M&Ms of different colors.=20 I think the first approach should be to put the M&M's away. If you give th= em to small children, they will get the chocolate all over their hands and = then get it all over your furniture, curtains, pets, etc. Don't buy the "m= elts in your mouth, not in your hand" garbage. Maybe you're not old enough= to remember that slogan. But it doesn't work with little kids. Trust me. > I was given a hint that said to write a helper function Groups(M : Inte= ger; A : Int_Array) that takes a number of M&Ms and the array and returns h= ow many groups I can make of M pieces of M&Ms.=20 >=20 > (For example, with 11 red bricks, 9 blue bricks, and 5 green bricks, Grou= ps(3) would return 7.) Ah, so you ate all the M&Ms yourself and decided to give them Legos instead= . Good, that's what I would have done. =20 > Then do my binary search based on this helper function(Posted below).=20 >=20 > --This problem MUST include Binary Search in some way. >=20 >=20 >=20 > I completed that but now I am stuck on the main function(also posted belo= w) it is totally wrong cause I can't even pass the first test case: >=20 >=20 >=20 > function Groups(Bricks : Integer; A: Int_Array) return Integer is > counter : Integer :=3D 0; > begin > for I in A'Range loop > counter :=3D counter + (A(I)/Bricks); > end loop; > return counter; end Groups; -- (obviously this line belonged up here) >=20 > function Candy(Kids : Integer; MsPerColor : Int_Array) return Integer = is=09 > lo : Integer :=3D 1; > hi : Integer :=3D 50000; > mid : Integer; > begin > for I in MsPerColor'Range loop > if MsPerColor(I) > hi then > hi :=3D MsPerColor(I); > end if; >=20 > if MsPerColor(I) < lo then > lo :=3D MsPerColor(I); > end if; > end loop; >=20 > mid :=3D (lo+hi)/2; > while Groups(mid, MsPerColor) /=3D Kids loop > mid :=3D (lo+hi)/2; > if Groups(mid, MsPerColor) > Kids then > hi :=3D mid; > else > lo :=3D mid; > end if; > end loop; > return mid; > end Candy; I won't give the whole answer, since this is a homework assignment. But it= will probably help to put some lines in the code to output some values. I= did that myself, since I was in too big a hurry to study your code careful= ly. I added these after the "while" line: text_io.put("lo=3D" & integer'image(lo)); text_io.put(" hi=3D" & integer'image(hi)); text_io.put(" mid=3D" & integer'image(mid)); text_io.put_line(" groups returns " & integer'image(Groups(mid, MsPerColor)= )); If you do this, I think you'll quickly figure out what the first mistake is= . As for the second mistake: the Put statements should help you see that too.= You need to think about: what kinds of quantities do your variables lo, h= i, and mid represent, and what kind of quantity does the first parameter to= Groups represent. They don't match, which is a problem. Good luck, and hope this helps, -- Adam