C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old February 18th, 2003, 09:02 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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...

Reply With Quote
  #2  
Old February 18th, 2003, 09:16 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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.

Reply With Quote
  #3  
Old February 19th, 2003, 12:15 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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;
}

Reply With Quote
  #4  
Old February 19th, 2003, 12:34 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
"How do I take into consideration the remainder?"

Look up how the modulus operator(%) works.

Reply With Quote
  #5  
Old February 19th, 2003, 12:40 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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).

Reply With Quote
  #6  
Old February 19th, 2003, 03:06 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
"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.

Reply With Quote
  #7  
Old February 19th, 2003, 05:44 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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;

}

Reply With Quote
  #8  
Old February 19th, 2003, 06:46 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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.

Reply With Quote
  #9  
Old February 19th, 2003, 07:01 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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

Reply With Quote
  #10  
Old February 19th, 2003, 08:48 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
lol...Sorry about that.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Is there a better way?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway