• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loops to find best fit  RSS feed

 
Bam Berth
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to be be able to find the best room to put a party of size n into given the capacity of rooms.

Basically I just need help writing a method that takes in the roomCapacity[] array and i and gives back the index at which the room capacity is most efficient with the size of the party.

Program should function somewhat like this (user input is bold and can be changed to anything positive).


How many rooms do you have?
3
What is the capacity of room 1?
30
What is the capacity of room 2?
20
What is the capacity of room 3?
10
How many parties are coming in?
3
How big is party 1?
11
The current best fit is room 2 with a capacity of 20
How big is party 2?
12
The current best fit is room 1 with a capacity of 30
How big is party 3?
3
The current best fit is room 3 with a capacity of 10




Any help or suggestions are appreciated, thanks!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

We don't give out solutions here. The help you can expect to receive here will be limited to tips and leading questions that are meant to get you to think about your problem and the solution. You're expected to do your own homework.

One of the things we advise most often is to stop typing, step away from the keyboard, get a pen/pencil and a piece of paper and work through the problem by hand.  As you work through the problem, try to describe the process of solving it in plain, simple English. Write that description down. A correct program solution usually reads just like how you describe it in plain English.
 
Liutauras Vilda
Sheriff
Posts: 4929
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. I'm interested, why have you decided to follow different indexing logic across those two loops? To confuse who? I think prefered is a bottom routine.

2. What should happen if party is as big as 31 person in it?

3. Have you covered objects? I find particularly wrong to model room's and parties capacities with arrays. That is something not intuitive and could mess up ones head.
 
Bam Berth
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:1. I'm interested, why have you decided to follow different indexing logic across those two loops? To confuse who? I think prefered is a bottom routine.

2. What should happen if party is as big as 31 person in it?

3. Have you covered objects? I find particularly wrong to model room's and parties capacities with arrays. That is something not intuitive and could mess up ones head.


1.  I'm not exactly sure why I chose to do it that way...  I was getting errors doing it other ways and then did it that way and it worked.

2.  If 31 is entered then the program will say that there are no rooms that can hold a party of that size (not shown in the code). 

3.  I haven't covered objects yet so arrays are the only thing I can work with.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bam Berth wrote:. . . 1.  I'm not exactly sure why I chose to do it that way...  I was getting errors doing it other ways and then did it that way and it worked.
What sort of errors were you getting? Was it printing 01 11 12 instead of 1 2 3? Read the Java® Language Specification for more details about that error. Before you do the apparent addition to get Fiddlers 3, you have created the String "Fiddlers 1" and you can't do arithmetic with the 1 any more.
Try System.out.print(...) instead of println in lines 9, 12 and 17.
. . . I haven't covered objects yet so arrays are the only thing I can work with.
What do they teach at schools nowadays?

And welcome again
 
Liutauras Vilda
Sheriff
Posts: 4929
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bam Berth wrote:1.  I'm not exactly sure why I chose to do it that way...
Oh, I see. Alright, so you had problems with rooms numbers, and in addition of that probably there were problem Campbell pointed out.

Always start with a below form of for loop:
Now, we know there are no 0 rooms, so, what you need to do is to add 1 (mathematical addition) to your current index within the print statement.
See how I enclosed i + 1 to parentheses? That is the problem Campbell mentioned to you. If you omit those, what you get is String concatenation. And finally you get printed out "Something 01 something else". That happens because i gets concatenated to Something first, then 1 gets concatenated to previous result of concatenation of Something and i and then last bit (something else). There wouldn't be a problem if there were only i, but since you want to do arithmetic first, you need to... Play around with various scenarios and you'll see.

Now about the method bestFit you need to implement. What is your thinking at the moment, how you'd solve? Please tell us in simple plain English, how you'd solve that? Try to omit any technical terminology.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!