# Help With Beginner Level Problem

• (2 Pages)
• 1
• 2

## 20 Replies - 1648 Views - Last Post: 11 September 2009 - 08:43 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=125285&amp;s=355929c26338263b27767b38a097018c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 messianic

Reputation: 0
• Posts: 37
• Joined: 03-September 09

# Help With Beginner Level Problem

Posted 11 September 2009 - 06:19 PM

Tipping Waiters

Heres problem statement:

* The sum I pay must be round, i.e., divisible by 5.
* The tip must be between 5% and 10% of the final sum I pay, inclusive.

Clearly, sometimes there may be multiple "correct" ways of settling the bill.

Inputs:

* an int bill -- the amount I have to pay for the dinner
* an int cash -- the amount of money I have in my pocket

Write a function that computes how many different final sums satisfy the conditions above.

Heres the code I have so far:

```
public class TippingWaiters {
public int possiblePayments(int bill, int cash) {

int start=(int)(bill+.05*bill);
int end=(int)(bill+.1*bill);

if(start>cash)
start=cash;
if(end>cash)
end=cash;

int count=0;

for(int i=start;i<=end;++i)
if(i%5==0)
++count;
return count;
}
}

```

But I'm getting errors when compiling. For instance, with the inputs : 240 (bill) 279 (cash), the answer is 3 but the code gives me 2.

Can someone help

This post has been edited by messianic: 11 September 2009 - 06:46 PM

Is This A Good Question/Topic? 0

## Replies To: Help With Beginner Level Problem

### #2 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Help With Beginner Level Problem

Posted 11 September 2009 - 06:45 PM

### #3 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• Joined: 20-March 08

## Re: Help With Beginner Level Problem

Posted 11 September 2009 - 06:46 PM

messianic, on 11 Sep, 2009 - 07:19 PM, said:

But I'm getting errors when compiling.

Which are...?

And, as mostyfriedman said, .

This post has been edited by Locke: 11 September 2009 - 06:47 PM

### #4 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Help With Beginner Level Problem

Posted 11 September 2009 - 06:54 PM

btw there's no need for type casting here

edit: dunno what's wrong with me tonight

This post has been edited by mostyfriedman: 11 September 2009 - 07:32 PM

### #5 messianic

Reputation: 0
• Posts: 37
• Joined: 03-September 09

## Re: Help With Beginner Level Problem

Posted 11 September 2009 - 06:59 PM

what does type casting mean?

Can you help me out on my code

### #6 messianic

Reputation: 0
• Posts: 37
• Joined: 03-September 09

## Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:06 PM

I honestly do not know what is wrong with my code. When I do it manually, i find that the number i get working with my code is the right answer, but for some reason when I run it it gives back a wrong answer

### #7 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:12 PM

```int start=(int)(bill+.05*bill);

```

the (int) is called type casting..

Quote

But I'm getting errors when compiling. For instance, with the inputs : 240 (bill) 279 (cash), the answer is 3 but the code gives me 2.

i dont really see any compilation errors.. what exactly are you trying to do?

```int end=(int)(bill+.1*bill);

```

it should be 0.01 instead of 0.1

### #8 messianic

Reputation: 0
• Posts: 37
• Joined: 03-September 09

## Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:14 PM

well 10% should be .1 correct?

And are you saying I should delete int? Should I replace it with something

And I run my code in a program that feeds in input and I am getting wrong answers for some of them.

### #9 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• Joined: 20-March 08

## Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:15 PM

mostyfriedman, on 11 Sep, 2009 - 08:12 PM, said:

it should be 0.01 instead of 0.1

Not true...10% is .1.

### #10 messianic

Reputation: 0
• Posts: 37
• Joined: 03-September 09

## Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:17 PM

Basically, when i input the numbers manually and figure it out, the code should give the right answers, but somehow when I run it through the program the code is giving wrong answers. Im kinda in a dead end

### #11 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:20 PM

Locke, on 11 Sep, 2009 - 06:15 PM, said:

mostyfriedman, on 11 Sep, 2009 - 08:12 PM, said:

it should be 0.01 instead of 0.1

Not true...10% is .1.

ah yea right..crap, brain overflow again

### #12 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:36 PM

i also ran it by hand and start will be 252, end will be 264..only 255 and 260 are divisible by 5 so the output is 2

### #13 messianic

Reputation: 0
• Posts: 37
• Joined: 03-September 09

## Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:37 PM

is this supposed to be a help forum

### #14 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• Joined: 20-March 08

## Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:46 PM

Yes...but sadly, you haven't given much information...

But from what I can tell, I don't see why it's going wrong.

### #15 messianic

Reputation: 0
• Posts: 37
• Joined: 03-September 09

## Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:56 PM

here are other constraints that might help:

Assume that both bill and cash are in dollars
All the money I have is in one-dollar banknotes.

Here are some examples:

1234567 (bill), 12345678 (cash); answer = 14440