Results 1 to 12 of 12
Like Tree3Likes
  • 2 Post By Fubarable
  • 1 Post By pbrockway2

Thread: Help with simple algorithm problem

  1. #1
    Eaballard513 is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Help with simple algorithm problem

    here is the problem my teacher assigned :
    For an upcoming field trip, buses are needed to take the students on a trip. Buses hold 40 students. Your
    boss has given you the task of writing the code that will compute and display the number of buses needed for a field trip
    with 473 students. The output should be formatted as follows, with the blank replaced with the computed answer.
    Your program should do all the computations necessary.

    473 students will require ___ buses
    .
    If you do your computation in an organized way, it should be easy to copy and paste your code, replace all the instances
    of 473 with some other number of students, and have your code still work correctly. So, once youíve written the code
    for 473 students, copy and paste that code five more times, and thoroughly test your program with 5 other numbers of
    students. For each test, do not change the formula you use, except that you should replace the number 473 with the
    number of students you are testing. You should assume that the number of students will always be an integer that is at
    least 1 and at most 1000. Donít bother checking test cases outside this range. Test thoroughly, even if you believe with
    all your heart that youíve done the correct computation.

    **** CANT USE IF STATEMENTS ******


    i have the basic algorithm to calculate the number of buses. i need a code that says if the remainder > 0 then extraBus = 1
    but i dont understand how to do this with out and if statement....

    this is my code so far:

    Java Code:
    public class Assign3Practice
    {
       public static void main( String [] args )
       {
          
    
    
    
    {
             int busCompacity = 40;
             
             int numStudents = 473; 
             
             int fullBuses = numStudents / busCompacity;
             
             int remainder = numStudents % busCompacity;
      
    
       
     // i need a code here to do what i asked  
       
              
            
             int totalBuses = remainder + fullBuses;
             
             System.out.println (   + numStudents + " Students will require " + totalBuses + " buses" );
          }
    Last edited by Fubarable; 02-05-2012 at 10:05 PM. Reason: moderation

  2. #2
    Vampiricx3 is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Help with simple algorithm problem!!!!!????

    Java Code:
    if(studentsRemaining > 0) {
          extraBus++;
    }
    ?
    If I have helped you, + rep

  3. #3
    Eaballard513 is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help with simple algorithm problem!!!!!????

    i cant use any if statements i need an algorithm that does the same thing as:

    if (studentsRemaining > 0);{
    extraBuses++;
    }
    with out using if else or while

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with simple algorithm problem

    Moderator edit: code tags added to initial post. Distracting punctuation removed from thread title.
    DarrylBurke and Vampiricx3 like this.

  5. #5
    Eaballard513 is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help with simple algorithm problem

    Please some one try and figure out an algorithm for me this program is due in a couple of hours.

    i cant use if statements

    the algorithm has t work with any instance of student 1 to 1000

  6. #6
    Vampiricx3 is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Help with simple algorithm problem

    Why not ask a class mate of yours to help? We aren't here to do your homework mate.
    If I have helped you, + rep

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Help with simple algorithm problem

    Notice that the remainder doesn't play any part in the actual question.

    By focussing on remainder you have led yourself into a problem: you want to calculate whether there is a remainder, but are unwilling to use the obvious and sane method of using an if statement.

    Forget the remainder, I say. Use teddybears instead! More precisely have almost a bus full of them (39) on hand, and put them onto the buses as well as the students. Put the teddybears onto the buses after the students because the teddybears are not actually going anywhere (that would be silly). The remainder can now be safely ignored because it consists of a bus with some number of teddybears (but no students) in it.

  8. #8
    Eaballard513 is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help with simple algorithm problem

    Quote Originally Posted by Vampiricx3 View Post
    Why not ask a class mate of yours to help? We aren't here to do your homework mate.
    this isn't my whole home work assignment only a small portion and i'm only looking for help because i've spent 3 days trying to figure it out and its getting frustrating. i don't see aany of my class mates till tomorrow, after the assignments due.

  9. #9
    Eaballard513 is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help with simple algorithm problem

    Quote Originally Posted by pbrockway2 View Post
    Notice that the remainder doesn't play any part in the actual question.

    By focussing on remainder you have led yourself into a problem: you want to calculate whether there is a remainder, but are unwilling to use the obvious and sane method of using an if statement.

    Forget the remainder, I say. Use teddybears instead! More precisely have almost a bus full of them (39) on hand, and put them onto the buses as well as the students. Put the teddybears onto the buses after the students because the teddybears are not actually going anywhere (that would be silly). The remainder can now be safely ignored because it consists of a bus with some number of teddybears (but no students) in it.
    i don't think i get what you trying to say. Could you give me an example?

    and my teacher wont allow us to use if statements on this problem i wish i could i would have finished this assignment along time ago.
    Last edited by Eaballard513; 02-06-2012 at 03:49 AM.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Help with simple algorithm problem

    i don't think i get what you trying to say. Could you give me an example?
    I don't know if I can make the algorithm (effective procedure) any clearer, but here's an example:

    (The example is much easier to see if you arm yourself with small tokens or pieces of paper - I'm assuming you don't have 39 teddybears or 1000 obliging students to experiment with - and shuffle them about on your desk)

    0. Let's suppose we have 90 students wanting to travel on the buses. It is required to construct enough buses for them to travel, 40 per bus.

    Here are the students:
    Java Code:
    XXXXXXXXXX
    XXXXXXXXXX
    XXXXXXXXXX
    XXXXXXXXXX
    XXXXXXXXXX
    XXXXXXXXXX
    XXXXXXXXXX
    XXXXXXXXXX
    XXXXXXXXXX
    I. More precisely have almost a bus full of them [teddybears] (39) on hand

    Here are my teddybears:
    Java Code:
    ..........
    ..........
    ..........
    .........
    II Put them onto the buses as well as the students. Put the teddybears onto the buses after the students because the teddybears are not actually going anywhere (that would be silly).

    By busload they look like this:

    Java Code:
    Bus 1:         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    Bus 2:         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    Bus 3:         XXXXXXXXXX..............................
    The remainder: .........
    III The remainder can now be safely ignored because it consists of a bus with some number of teddybears (but no students) in it.

    We have constructed the buses required: 3 of them. (QEF, but remember to remove the teddybears before departure so they can be used for the next calculation.)

    [edit] corrected the number of bears in the remainder bus. Teh codez, btw, come from step II: a single line of code which replaces 2/3 of your main method.
    Last edited by pbrockway2; 02-06-2012 at 05:03 AM.
    Fubarable likes this.

  11. #11
    Eaballard513 is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help with simple algorithm problem

    Thank you soo much i got it now... you have no idea how long this has been bothering me.

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Help with simple algorithm problem

    You're welcome. I'm glad you've got it figured out.

Similar Threads

  1. An algorithm problem
    By bMorgan in forum New To Java
    Replies: 1
    Last Post: 01-27-2011, 10:53 AM
  2. Algorithm Problem
    By Aggy in forum New To Java
    Replies: 4
    Last Post: 01-22-2010, 12:00 AM
  3. Problem with equation in my algorithm
    By romina in forum New To Java
    Replies: 2
    Last Post: 07-20-2007, 08:53 AM
  4. Problem with algorithm
    By Albert in forum AWT / Swing
    Replies: 1
    Last Post: 07-13-2007, 04:31 PM
  5. Algorithm problem
    By Marcus in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 02:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •