#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    13

    Is there a better way?


    Is there a better way to achieve the same result (quicker, less memory, etc..)? In this situation, the user types in how many people he/she needs to transport to wherever. The options are buses and vans. 50 people to a bus, 10 to a van. It tells the user how many buses and vans he/she will need.

    code:
    #include <iostream.h>

    int main()
    {
    int people;
    int buses = 50;
    int vans = 10;
    int i = 0;
    int y= 0;
    char loop;

    do

    {

    cout << "Type in the amount of people\n";
    cin >> people;

    while (people > 49)
    {
    people = (people - 50);
    i++;
    }
    while (people < 50 && people > -1)
    {
    people = (people - 10);
    y++;
    }
    cout << "\n\n" << "you will need " << i << " bus/es and " << y << " vans" << "\n\n";
    cout << "\n\nwould you like to do it again? Type Y (yes) or N (no)\n";
    cin >> loop;
    cout << "\n";

    }

    while (loop == 'Y' || loop == 'y');

    return 0;

    }

    Thanks in advance.
    -andy
    hmmm...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Hi,

    Yes, there is. Integer arithmetic and the modulus operator (%) can get you the results without looping, so I presume it would be faster, especially if you had 40 million people:

    const int BUS_CAPACITY=50;
    cont int VAN_CAPACITY=10;

    int people=0;
    cin>>people;


    int buses=people/BUS_CAPACITY;
    int remaining_people=people % BUS_CAPACITY;

    int vans=remaining_people/VAN_CAPACITY;
    remaining_people=remaining_people % VAN_CAPACITY;

    You should also avoid using "magic" numbers. If for some reason the capacity of the vehicles changed, all I would need to change in my example is the initialization of the BUS_CAPACITY and VAN_CAPACITY variables. With your code, you would have to go through and make a lot of changes. Not a big deal with a small program, but with a large program it would be a daunting task.
    Last edited by 7stud; February 18th, 2003 at 10:37 PM.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    13
    I dont understand how your method works. Maybe I am just not thinking of this correctly. How do I take into consideration the remainder? here is what the code is now:

    #include <iostream.h>
    int main()
    {

    const int BUS_CAPACITY=50;
    const int VAN_CAPACITY=10;
    int people=0;

    cout << "How many people?\n";
    cin >> people;

    int buses = people/BUS_CAPACITY;
    int remaining_people = people % BUS_CAPACITY;
    int vans = remaining_people/VAN_CAPACITY;
    remaining_people = remaining_people % VAN_CAPACITY;

    cout << "You will need " << buses << " buses and " << vans << " vans\n";

    return 0;
    }
    hmmm...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    "How do I take into consideration the remainder?"

    Look up how the modulus operator(%) works.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    13
    I understand how it works..I just don't understand how I can use it in this situation with the whole number. I would probably need to add the remainder onto the whole number (remainder being #er of vans).
    hmmm...
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    "I just don't understand how I can use it in this situation with the whole number."

    If you answer these questions in order, I think you will understand:

    1)If you have 65 people, and a bus holds 50 people, what is stored in the integer variable buses when it is assigned 65/50?

    2)What is stored in the integer variable remaining_people when it is assigned 65 % 50?

    3)What is stored in the integer variable vans when it is assigned remaining_people/10?

    4)Finally what is stored in the variable remaining_people when it is assigned remaining_people % 10?

    Post your answers if you're still confused, and I'll check back later.
    Last edited by 7stud; February 19th, 2003 at 04:16 PM.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    13
    The only way I could get it to work was with an if statement..Here is the code:


    #include <iostream.h>

    int main()

    {

    const int BUS_CAPACITY=50;
    const int VAN_CAPACITY=10;
    int people = 0;

    cout << "people?\n";
    cin>>people;

    int buses=people/BUS_CAPACITY;
    int remaining_people=people % BUS_CAPACITY;
    int vans=remaining_people/VAN_CAPACITY;
    remaining_people=remaining_people % VAN_CAPACITY;

    if (remaining_people > 0)
    {
    vans++;
    }

    cout << "You will need " << buses << " buses and "<< vans << " vans.\n";
    return 0;

    }
    hmmm...
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Ahah! Now I see the problem you were having with the code I posted--the final remainder. I left that up to you to do what you wanted with it, and your solution takes care of it.
    Last edited by 7stud; February 19th, 2003 at 07:52 PM.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    13
    lol..i thought you gave me the whole solution. This was why I was spending all that time figuring out a way it would work with what you gave me.

    -andy
    hmmm...
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    lol...Sorry about that.

IMN logo majestic logo threadwatch logo seochat tools logo