The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Is there a better way?
Discuss Is there a better way? in the C Programming forum on Dev Shed. Is there a better way? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 18th, 2003, 09:02 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
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:
#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...
|

February 18th, 2003, 09:16 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
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.
|

February 19th, 2003, 12:15 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
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:
#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;
} |
|

February 19th, 2003, 12:34 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
"How do I take into consideration the remainder?"
Look up how the modulus operator(%) works.
|

February 19th, 2003, 12:40 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
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).
|

February 19th, 2003, 03:06 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
"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.
|

February 19th, 2003, 05:44 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
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;
}
|

February 19th, 2003, 06:46 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
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.
|

February 19th, 2003, 07:01 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
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
|

February 19th, 2003, 08:48 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
lol...Sorry about that.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|