|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
||||
|
||||
|
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: Quote:
Thanks in advance. -andy
__________________
hmmm... |
|
#2
|
|||
|
|||
|
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 09:37 PM. |
|
#3
|
||||
|
||||
|
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:
Quote:
|
|
#4
|
|||
|
|||
|
"How do I take into consideration the remainder?"
Look up how the modulus operator(%) works. |
|
#5
|
||||
|
||||
|
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).
|
|
#6
|
|||
|
|||
|
"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 03:16 PM. |
|
#7
|
||||
|
||||
|
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; } |
|
#8
|
|||
|
|||
|
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 06:52 PM. |
|
#9
|
||||
|
||||
|
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 |
|
#10
|
|||
|
|||
|
lol...Sorry about that.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Is there a better way? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|