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,FREEMAIL_FROM, PLING_QUERY autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,7d1a02b763945274 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit X-Received: by 10.224.175.65 with SMTP id w1mr7962768qaz.7.1358293854237; Tue, 15 Jan 2013 15:50:54 -0800 (PST) Received: by 10.49.75.195 with SMTP id e3mr16827528qew.24.1358293854216; Tue, 15 Jan 2013 15:50:54 -0800 (PST) Path: k2ni11qap.0!nntp.google.com!p13no416783qai.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Tue, 15 Jan 2013 15:50:54 -0800 (PST) In-Reply-To: <6bf7934b-d736-4213-9786-45bd0a339cbf@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=134.240.94.90; posting-account=CZZpzgoAAAAoaHoNNp9zhY9EzQgEmxhU NNTP-Posting-Host: 134.240.94.90 References: <6bf7934b-d736-4213-9786-45bd0a339cbf@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Algorithms Homework Help?!?! From: willmann817 Injection-Date: Tue, 15 Jan 2013 23:50:54 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Date: 2013-01-15T15:50:54-08:00 List-Id: On Tuesday, January 15, 2013 5:50:37 PM UTC-5, Adam Beneschan wrote: > On Tuesday, January 15, 2013 1:52:44 PM UTC-8, willmann817 wrote: >=20 > > I am given an Array that contains the number of M&Ms of each color I ha= ve. 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.=20 >=20 > >=20 >=20 > > --THE CHILDREN MUST ALL HAVE THE SAME NUMBER OF M&MS >=20 > > --EACH CHILD MUST ONLY HAVE 1 COLOR OF M&MS >=20 > >=20 >=20 > > Given an array containing the number of M&Ms I have in each available c= olor, and the number of kids, determine the maximum number of M&Ms you can = put in each goody bag.=20 >=20 > >=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 w= ithout at least one child receiving M&Ms of different colors.=20 >=20 >=20 >=20 >=20 >=20 > I think the first approach should be to put the M&M's away. If you give = them to small children, they will get the chocolate all over their hands an= d then get it all over your furniture, curtains, pets, etc. Don't buy the = "melts in your mouth, not in your hand" garbage. Maybe you're not old enou= gh to remember that slogan. But it doesn't work with little kids. Trust m= e. >=20 >=20 >=20 >=20 >=20 > > I was given a hint that said to write a helper function Groups(M : In= teger; 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.=20 >=20 > >=20 >=20 > > (For example, with 11 red bricks, 9 blue bricks, and 5 green bricks, Gr= oups(3) would return 7.) >=20 >=20 >=20 >=20 >=20 > Ah, so you ate all the M&Ms yourself and decided to give them Legos inste= ad. Good, that's what I would have done. >=20 >=20 >=20 > =20 >=20 > > Then do my binary search based on this helper function(Posted below).= =20 >=20 > >=20 >=20 > > --This problem MUST include Binary Search in some way. >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > I completed that but now I am stuck on the main function(also posted be= low) it is totally wrong cause I can't even pass the first test case: >=20 > >=20 >=20 > >=20 >=20 > >=20 >=20 > > function Groups(Bricks : Integer; A: Int_Array) return Integer is >=20 > > counter : Integer :=3D 0; >=20 > > begin >=20 > > for I in A'Range loop >=20 > > counter :=3D counter + (A(I)/Bricks); >=20 > > end loop; >=20 > > return counter; >=20 > end Groups; -- (obviously this line belonged up here) >=20 > >=20 >=20 > > function Candy(Kids : Integer; MsPerColor : Int_Array) return Intege= r is=09 >=20 > > lo : Integer :=3D 1; >=20 > > hi : Integer :=3D 50000; >=20 > > mid : Integer; >=20 > > begin >=20 > > for I in MsPerColor'Range loop >=20 > > if MsPerColor(I) > hi then >=20 > > hi :=3D MsPerColor(I); >=20 > > end if; >=20 > >=20 >=20 > > if MsPerColor(I) < lo then >=20 > > lo :=3D MsPerColor(I); >=20 > > end if; >=20 > > end loop; >=20 > >=20 >=20 > > mid :=3D (lo+hi)/2; >=20 > > while Groups(mid, MsPerColor) /=3D Kids loop >=20 > > mid :=3D (lo+hi)/2; >=20 > > if Groups(mid, MsPerColor) > Kids then >=20 > > hi :=3D mid; >=20 > > else >=20 > > lo :=3D mid; >=20 > > end if; >=20 > > end loop; >=20 > > return mid; >=20 > > end Candy; >=20 >=20 >=20 > 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 caref= ully. I added these after the "while" line: >=20 >=20 >=20 > text_io.put("lo=3D" & integer'image(lo)); >=20 > text_io.put(" hi=3D" & integer'image(hi)); >=20 > text_io.put(" mid=3D" & integer'image(mid)); >=20 > text_io.put_line(" groups returns " & integer'image(Groups(mid, MsPerColo= r))); >=20 >=20 >=20 > If you do this, I think you'll quickly figure out what the first mistake = is. >=20 >=20 >=20 > As for the second mistake: the Put statements should help you see that to= o. You need to think about: what kinds of quantities do your variables lo,= hi, and mid represent, and what kind of quantity does the first parameter = to Groups represent. They don't match, which is a problem. >=20 >=20 >=20 > Good luck, and hope this helps, >=20 >=20 >=20 > -- Adam Adam, Here is what I am seeing: There are many more of this before what I have posted below lo=3D 45 hi=3D 46 mid=3D 45 groups returns 1 lo=3D 45 hi=3D 46 mid=3D 45 groups returns 1 lo=3D 45 hi=3D 46 mid=3D 45 groups returns 1 lo=3D 45 hi=3D 46 mid=3D 45 groups returns 1 lo=3D 45 hi=3D 46 mid=3D 45 groups returns 1 lo=3D 45 hi=3D 46 mid=3D 45 groups returns 1 lo=3D 45 hi=3D 46 mid=3D 45 groups returns 1 lo=3D 45 hi=3D 46 mid=3D 45 groups returns 1 lo=3D 45 hi=3D 46 mid=3D 45 groups returns 1 lo=3D 45 hi=3D 46 mid=3D 45 groups returns 1 lo=3D 45 hi=3D 46 mid=3D 45 groups returns 1 lo=3D 45 hi=3D 46 mid=3D 45 groups returns 1 lo=3D 45 hi=3D 46 mid=3D 45 groups returns 1 lo=3D 45 hi=3D 46*** Failed Test #2 *** Kids =3D 10 BricksPerColor =3D (6,2,3,46,2) Time limit exceeded. Your code may be stuck in an infinite loop, or may just be too slow. Failure limit reached. Aborting remaining tests. Can you maybe give me a guiding hint on what needs to be done to fix this. = I do not want code or an answer but. I think the binary search is searchi= ng for the value of mid that makes Groups(mid,BricksPerColor) =3D Kids. If= that is incorrect then that is the first place I need help. Thanks.