# Problem in counting

Page 1 of 1

## 13 Replies - 5066 Views - Last Post: 30 April 2013 - 07:50 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=319920&amp;s=8d383b76a5485a346ede704b9c6edb1b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 trupicc

Reputation: 0
• Posts: 26
• Joined: 22-April 13

# Problem in counting

Posted 30 April 2013 - 05:40 AM

I've encountered a problem in how should I count the middle integer in one of my excercises. And I'd like to have a proper explanation in here /> ,because my teacher could not manage to explain it to me ,or I just did not understand him.

This is the excercise

Write a method named hasMidpoint that accepts three integers as parameters and returns true if one of the integers
is the midpoint between the other two integers; that is, if one integer is exactly halfway between them. Your
method should return false if no such midpoint relationship exists. For example, the call hasMidpoint(7, 4,
10) should return true because 7 is halfway between 4 and 10. By contrast, the call hasMidpoint(9, 15, 8)
should return false because no integer is halfway between the other two. The integers could be passed in any order;
the midpoint could be the 1st, 2nd, or 3rd. You must check all cases. If your method is passed three of the same
value, return true.

Is This A Good Question/Topic? 0

## Replies To: Problem in counting

### #2 andrewsw

• Bouncy!

Reputation: 6562
• Posts: 26,615
• Joined: 12-December 12

## Re: Problem in counting

Posted 30 April 2013 - 05:48 AM

Have you made a start? Have you created a method hasMidpoint that accepts three integers? If so, post the code that you have so far.

That assignment is very clearly described, so you'll need to ask a more specific question. What is it that is confusing you?

### #3 trupicc

Reputation: 0
• Posts: 26
• Joined: 22-April 13

## Re: Problem in counting

Posted 30 April 2013 - 06:01 AM

Yeah ,I created it ,but it is not much of a help. I do not understand which loop should I use and how should I declare those Integers inside? I mean the order.

### #4 SwiftStriker00

• No idea why my code works

Reputation: 439
• Posts: 1,617
• Joined: 25-December 08

## Re: Problem in counting

Posted 30 April 2013 - 06:28 AM

A few generic things:

To declare a basic integer you can simply do this in your code: int a = 0; this will create an integer variable labeled 'a', with the initial value of 0.

While there are many different ways to complete this assignment. I don't think loops are necessary. Now you may need an if-statement. These will allow you to divert your code into two different sections depending on the outcome of your conditional statement. The syntax for that looks like this:

```int a = 0;
int b = 1;

if( a < b )
{
//Place code here for when a is less than b
}
else
{
//Place code here for when b greater than or equal to a
}
```

### #5 trupicc

Reputation: 0
• Posts: 26
• Joined: 22-April 13

## Re: Problem in counting

Posted 30 April 2013 - 06:44 AM

SwiftStriker00, on 30 April 2013 - 06:28 AM, said:

A few generic things:

To declare a basic integer you can simply do this in your code: int a = 0; this will create an integer variable labeled 'a', with the initial value of 0.

While there are many different ways to complete this assignment. I don't think loops are necessary. Now you may need an if-statement. These will allow you to divert your code into two different sections depending on the outcome of your conditional statement. The syntax for that looks like this:

```int a = 0;
int b = 1;

if( a < b )
{
//Place code here for when a is less than b
}
else
{
//Place code here for when b greater than or equal to a
}
```

```public class hasMidPoint{
public static void main(String[] args){

hasMidPoint ShowtheMiddle = new hasMidPoint();
}
public hasMidPoint(){

int small = 5;
int middle = 10;
int high = 15;
}
}

```

That what's bothering me. Teacher told me to do something like this : high-small/2 ,how can I use this in an If?

### #6 darek9576

• D.I.C Lover

Reputation: 203
• Posts: 1,731
• Joined: 13-March 10

## Re: Problem in counting

Posted 30 April 2013 - 06:47 AM

You were asked if you have created a hasMidPoint() method that takes 3 integers and you said yes. The code above is wrong. You need to go back to basics. Look up what methods are used for and how to correctly define one.

### #7 trupicc

Reputation: 0
• Posts: 26
• Joined: 22-April 13

## Re: Problem in counting

Posted 30 April 2013 - 06:50 AM

Ok. I'll change the code in a bit

### #8 SwiftStriker00

• No idea why my code works

Reputation: 439
• Posts: 1,617
• Joined: 25-December 08

## Re: Problem in counting

Posted 30 April 2013 - 06:54 AM

trupicc, on 30 April 2013 - 08:44 AM, said:

That what's bothering me. Teacher told me to do something like this : high-small/2 ,how can I use this in an If?

Your professor is trying to give you a mathematical formula to determine the midpoint between the high number and the low number. lets call this number FoundMid, and you know you have a GivenMid, correct? So I say to you:

If FoundMid is not equal to GivenMid.
Then we should return saying there is no mid value given
Else
We should return the mid value that was given.

### #9 Gungnir

Reputation: 152
• Posts: 527
• Joined: 21-May 11

## Re: Problem in counting

Posted 30 April 2013 - 06:54 AM

You do NOT need to check all cases, like it says. You can find the minimum and maximum,
then check whether or not (maximum + minimum) / 2) == otherNumber. I don't know why your teacher has asked you to do otherwise.

A vague example might be something along the lines of:
```boolean checkMid(int a, int b, int c)
{
if(a == b == c)
return true;
else
{
int[] temp = {a, b, c};
int min, max, other;

for(int i = 0; i < temp.length; i++)
//find min, max, and other

return (((min + max) / 2) == other)
}
}

```

This post has been edited by Gungnir: 30 April 2013 - 07:20 AM

### #10 trupicc

Reputation: 0
• Posts: 26
• Joined: 22-April 13

## Re: Problem in counting

Posted 30 April 2013 - 06:59 AM

trupicc, on 30 April 2013 - 06:50 AM, said:

Ok. I'll change the code in a bit

```public class hasMidPoint{
public static void main(String[] args){

}
public hasMidPoint(int small,int middle ,int high){

}
}

```

### #11 SwiftStriker00

• No idea why my code works

Reputation: 439
• Posts: 1,617
• Joined: 25-December 08

## Re: Problem in counting

Posted 30 April 2013 - 07:04 AM

Gungnir, on 30 April 2013 - 08:54 AM, said:

You do NOT need to check all cases, like it says. You can find the minimum and maximum,
then check whether or not (maximum + minimum / 2) = otherNumber. I don't know why your teacher has asked you to do otherwise.

Also to avoid confusion (maximum + minimum / 2) = otherNumber is not your midpoint.
```int halfOfDifference = (max - min) / 2;
int mid = min + halfOfDifference ;
```

Parentheses are important!

Edit: changed name for clarity

or
```(maximum + minimum ) / 2 = otherNumber;

```

This post has been edited by SwiftStriker00: 30 April 2013 - 07:01 AM

### #12 Gungnir

Reputation: 152
• Posts: 527
• Joined: 21-May 11

## Re: Problem in counting

Posted 30 April 2013 - 07:24 AM

Quote

(maximum + minimum / 2) = otherNumber is not your midpoint.

The average between two numbers is the midpoint.
But I see that you quoted me a few edits ago, so I suppose it's fair. I've edited my response since then.

Quote

```public hasMidPoint(int small, int middle ,int high)

```

Remember that the parameters could be in any order. These parameter names are going to become cumbersome and confusing in the event that small is a larger number than high. Which, as the original question points out, is a very real possibility.

### #13 trupicc

Reputation: 0
• Posts: 26
• Joined: 22-April 13

## Re: Problem in counting

Posted 30 April 2013 - 07:39 AM

Gungnir, on 30 April 2013 - 07:24 AM, said:

Quote

(maximum + minimum / 2) = otherNumber is not your midpoint.

The average between two numbers is the midpoint.
But I see that you quoted me a few edits ago, so I suppose it's fair. I've edited my response since then.

Quote

```public hasMidPoint(int small, int middle ,int high)

```

Remember that the parameters could be in any order. These parameter names are going to become cumbersome and confusing in the event that small is a larger number than high. Which, as the original question points out, is a very real possibility.

Oh. Thanks. I didnt think about that...

### #14 jon.kiparsky

• Beginner

Reputation: 11093
• Posts: 18,982
• Joined: 19-March 11

## Re: Problem in counting

Posted 30 April 2013 - 07:50 AM

I suggest you stop thinking about code and start thinking about logic. Once you get the logic, you can think about the code.

Now, I give you three numbers, in arbitrary order, and ask you to determine whether one of the three is precisely at the midpoint between the other two.

Leaving the code aside, and just working on a chalkboard, how do you solve this? Can you develop a series of steps which I could apply, without much or any thought on my part, that would reliably give the right answer?

So I'll give you and example of what I mean for a somewhat different problem. The problem is, given an array of integers, to determine whether one of these integers is the average of the set.
The obvious way to do it:

1. Sum the integers in the array
2. Divide by the number of integers in the array, call the result the average.
3. If the sum does not divide evenly return false, since no integer can be the average.
4. If the sum does divide evenly, examine each integer in the array. If any one of those is equal to the average, return true.
5. If we have not returned true from the previous step, return false.

Try to come up with steps like this for your problem, and test them against different cases. Then, when you're sure your algorithm works, you can implement it.

Test cases make an interesting problem as well - you might also think about what makes a good test case for this. This will help you think through the problem.