Thread: Confused about this C problem

  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    London, UK
    Posts
    18

    Smile Confused about this C problem

    The last C problem I need to do, and for me it's quite difficult and I'd like to learn how to do it. Thanks so much
    Question 3: Juggl(ug)ing
    A cookery set contains several jugs of different sizes, each of which has a known capacity in oz (fluid ounces).
    There are no graduations on the jugs, so the only way to measure the liquid is by filling up a jug
    completely, pouring as much liquid as possible from one jug into another or by emptying a jug. A step
    consists of performing one of these three operations.
    Initially all the jugs are empty. To measure n oz it is necessary to perform a sequence of steps and finish
    with exactly n oz in one of the jugs.
    For example, suppose we have two jugs: jug A holds 3 oz and jug B holds 5 oz.
    • If we fill up jug B and then pour as much as possible from jug B into jug A, we would have (after 2
    steps) 3 oz in jug A and 2 oz in jug B. This is one way to measure 2 oz.
    • If we now empty jug A, pour the 2 oz from jug B into jug A, fill jug B and finally pour as much as
    possible from jug B into jug A, we would finish (after 6 steps) with 3 oz in jug A and 4 oz in jug B.
    We have now measured 4 oz.
    3(a) [ 23 marks ]
    Write a program that, given the capacities of several jugs, determines the shortest
    number of steps necessary to measure n oz.
    The input will consist of two lines. The first line will contain two integers j (1 " j " 3)
    then n (1 " n " 250), indicating the number of jugs and the required amount to
    measure respectively. The second line will contain j integers, each between 1 and
    250 inclusive, indicating the capacity of the jugs.
    You should output a single integer giving the minimum number of steps necessary to
    measure n oz.
    Your program will only be asked to measure amounts that are possible.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    How about you do your own homework instead of coming here and expecting us to do it for you? If you have something you've already done, then post it in code tags.

    C Board - Announcements in Forum : General Programming Boards

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    London, UK
    Posts
    18
    Quote Originally Posted by rags_to_riches View Post
    How about you do your own homework instead of coming here and expecting us to do it for you? If you have something you've already done, then post it in code tags.

    C Board - Announcements in Forum : General Programming Boards
    I'm sorry but I am not asking ANYONE TO DO MY HOMEWORK, merely to help me and possibly point me in the right direction, SORRY if I am not as good as you but I am sure you were at the low level I am now at some point and would have been very grateful if someone helped you and explained something you do not understand to you. That is what I have understood this forum is for, and am seeking engaged and productive talk with experienced programmers who I am sure would enjoy helping an inexperienced and begginer programmer like myself who is willing to learn and get better. Thank you for your productive input

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    We're willing to help you with what YOU have put some effort into. We're NOT willing to do all the work for you. You've yet to show an effort except to cut and paste your assignment into as post with "I don't understand".

    Ah, so this is the British Informatics Olympiad (warning: PDF) exam, which just came out today.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    London, UK
    Posts
    18
    Your link is last year's paper and not an assignement I am doing it on my own and it is not compulsory since no one is asking me to do it, I am doing the 2011 one (current one nobody knows whats in it) in 2 weeks so am attempting to have a stab at past questions to somewhat prepare myself (although there is not much hope) and try to do at least 1 question in the actual thing... I am not trying to cheat merely preparing myself for the real thing whos content is unknown to me and my friend who is also preparing for it. I am working on it and shall post whatever non functioning code I have to show my attempts.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by alexbcg View Post
    Your link is last year's paper and not an assignement I am doing it on my own and it is not compulsory since no one is asking me to do it, I am doing the 2011 one (current one nobody knows whats in it) in 2 weeks so am attempting to have a stab at past questions to somewhat prepare myself (although there is not much hope) and try to do at least 1 question in the actual thing... I am not trying to cheat merely preparing myself for the real thing whos content is unknown to me and my friend who is also preparing for it. I am working on it and shall post whatever non functioning code I have to show my attempts.
    We are looking forward to it. No really, I am not being ironic, that is the right attitude to have. And, if you happen to participate in the 2012 one, you may want to start preparing for it even earlier. I remember I used to go to the Olympiads in my high school years... ahhh the good times.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    An interesting project. I don't know of any algorithm that would solve it to find the minimum sequence of moves. Therefore you're likely going to end up exploring all possible permutations of bucket[n] fills and empty to bucket[other than n]... and keeping track of how many oz in each bucket. If one of the buckets happens to contain the desired amount, you stop.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I would probably use a dynamic programming approach to this: Dynamic programming - Wikipedia, the free encyclopedia. It wont have to be exhaustive, but will nevertheless be computationally complex.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM

Tags for this Thread