# Using max to find two numbers

• (3 Pages)
• 1
• 2
• 3

## 37 Replies - 1234 Views - Last Post: 06 May 2013 - 05:08 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=320212&amp;s=819723a82389f58590fd7b45d3ab560e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hhman1

Reputation: 0
• Posts: 23
• Joined: 02-May 13

# Using max to find two numbers

Posted 03 May 2013 - 11:11 AM

I'm working on some java homework and I'm stuck on another problem.
I'm suppose to write a program that reads integers, finds the two largest of them, and counts their occurrences. I've got the largest number down but I'm struggling to come up with the second one. Here is what I have so far:
```import java.util.Scanner;

public class Unit4PT1 {
public static void main (String [] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter numbers: ");

int max = -1;
int count = 0;
int largest;
int nextLargest;
while((number = in.nextInt()) != 0){
if(number > max){
max = largest;
count = 1;
} else if (number == max){
count++;
}else if (number > nextmax){
nextmax = nextLargest;
count = 1;
}else if (number == nextmax){
count++;
}
}

System.out.println("The largest number is " + max);
System.out.println("The occurence count of the largest number is " + count);
System.out.println("The second largest number is " + nextLargest);
System.out.println("The occurence count of the second largest number is " + count);
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Using max to find two numbers

### #2 jon.kiparsky

• Pancakes!

Reputation: 8565
• Posts: 14,815
• Joined: 19-March 11

## Re: Using max to find two numbers

Posted 03 May 2013 - 11:34 AM

```if(number > max){
max = largest;

```

This looks wrong to me. Shouldn't it be max=number?

You do the same thing for nextmax as well.

This looks like it's worth fixing.

Also, you're counting max and nextmax with the same counter variable, which is wrong.

### #3 hhman1

Reputation: 0
• Posts: 23
• Joined: 02-May 13

## Re: Using max to find two numbers

Posted 04 May 2013 - 07:00 PM

Okay I tried to fix it but I'm still seeing some errors. The symbols number, nextmax, and counter cannot be found. Any suggestions?
```import java.util.Scanner;

public class Unit4PT1 {
public static void main (String [] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter numbers: ");

int max = -1;
int count = 0;
int number;
while((number = in.nextInt()) != 0){
if(number > max){
max = number;
count = 1;
} else if (number == max){
count++;
}else if (number > nextmax){
nextmax = number;
counter = 1;
}else if (number == nextmax){
counter++;
}
}

System.out.println("The largest number is " + max);
System.out.println("The occurence count of the largest number is " + count);
System.out.println("The second largest number is " + nextmax);
System.out.println("The occurence count of the second largest number is " + counter);
}
}

```

### #4 jon.kiparsky

• Pancakes!

Reputation: 8565
• Posts: 14,815
• Joined: 19-March 11

## Re: Using max to find two numbers

Posted 04 May 2013 - 07:09 PM

hhman1, on 04 May 2013 - 09:00 PM, said:

Okay I tried to fix it but I'm still seeing some errors. The symbols number, nextmax, and counter cannot be found. Any suggestions?

You could try declaring them...

okay, I see number is declared, so you shouldn't be getting that error. Can you post your error output?

### #5 hhman1

Reputation: 0
• Posts: 23
• Joined: 02-May 13

## Re: Using max to find two numbers

Posted 04 May 2013 - 07:54 PM

Changed my code again and now I'm getting the following four errors:
error: Variable counter might not have been initialized. counter++
error: Variable nextmax might not have been initialized. }else if (number > nextmax){
error: Variable nextmax might not have been initialized.System.out.println("The second largest number is " + nextmax);
error: Variable counter might not have been initialized. System.out.println("The occurence count of the second largest number is " + counter);
```import java.util.Scanner;
public class Unit4PT1 {
public static void main (String [] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter numbers: ");

int max = -1;
int count = 0;
int number;
int counter;
int nextmax;
while((number = in.nextInt()) != 0){
if(number > max){
max = number;
count = 1;
} else if (number == max){
count++;
}else if (number > nextmax){
nextmax = number;
counter = 1;
}else if (number == nextmax){
counter++;
}
}

System.out.println("The largest number is " + max);
System.out.println("The occurence count of the largest number is " + count);
System.out.println("The second largest number is " + nextmax);
System.out.println("The occurence count of the second largest number is " + counter);
}
}

```

### #6 jon.kiparsky

• Pancakes!

Reputation: 8565
• Posts: 14,815
• Joined: 19-March 11

## Re: Using max to find two numbers

Posted 04 May 2013 - 07:58 PM

That means that there are possible branches of the code in which those variables don't have values when you read them. For example, if you get to this
```counter++;

```

before you get to this
```counter = 1;
```

then you're incrementing the value of a variable that hasn't been set to anything.

Also, you seem to have a variable called "count" and one called "counter" - this is going to confuse you. Better pick a different name for at least one of them.

### #7 hhman1

Reputation: 0
• Posts: 23
• Joined: 02-May 13

## Re: Using max to find two numbers

Posted 04 May 2013 - 08:11 PM

So would moving
```counter = 1;
```
up next to
``` count = 1
```
going to solve the problem?

### #8 jon.kiparsky

• Pancakes!

Reputation: 8565
• Posts: 14,815
• Joined: 19-March 11

## Re: Using max to find two numbers

Posted 04 May 2013 - 08:15 PM

Depends. What does counter count? What does count count? Should they both get the value 1 at the same time?

### #9 hhman1

Reputation: 0
• Posts: 23
• Joined: 02-May 13

## Re: Using max to find two numbers

Posted 04 May 2013 - 08:21 PM

Count is suppose to count the highest number (max). Counter is suppose to count the second highest number (nextmax)

### #10 jon.kiparsky

• Pancakes!

Reputation: 8565
• Posts: 14,815
• Joined: 19-March 11

## Re: Using max to find two numbers

Posted 04 May 2013 - 08:28 PM

Hm. So probably no, setting them both to 1 at the same time isn't likely to be good.

I suspect you haven't spent enough time trying to work the logic - no use writing code until you understand what it's supposed to do.

Suppose you have a list {1,2,3,4, 3, 4, 5,4, 1}

What are you going to want to report back to the user?
More important: how are you going to get to that answer? What are you going to be keeping track of, what values will you have before you start reading anything at all, and what will you have to do each time you read a number? Don't try to do it in code now, just think it through. Once you understand it, you can turn it into code.

### #11 hhman1

Reputation: 0
• Posts: 23
• Joined: 02-May 13

## Re: Using max to find two numbers

Posted 04 May 2013 - 08:48 PM

jon.kiparsky, on 04 May 2013 - 08:28 PM, said:

Hm. So probably no, setting them both to 1 at the same time isn't likely to be good.

I suspect you haven't spent enough time trying to work the logic - no use writing code until you understand what it's supposed to do.

Suppose you have a list {1,2,3,4, 3, 4, 5,4, 1}

What are you going to want to report back to the user?
More important: how are you going to get to that answer? What are you going to be keeping track of, what values will you have before you start reading anything at all, and what will you have to do each time you read a number? Don't try to do it in code now, just think it through. Once you understand it, you can turn it into code.

From your example I would like the program to say the largest number is five and it occurs once. The next largest number is four and it occurs three times. I don't know how I'm going to get that answer. I tried to do what I thought would work and when that failed I came here for help. I'm keeping track of how often the two largest numbers occur in a sequence of integers. The count++ and counter++ should add to the total every time there is an occurrence but I'm struggling to formulate it correctly.

### #12 jon.kiparsky

• Pancakes!

Reputation: 8565
• Posts: 14,815
• Joined: 19-March 11

## Re: Using max to find two numbers

Posted 04 May 2013 - 08:56 PM

Okay, that's progress: we agree that you're stumped, and we have a pretty good idea where you're stumped.

So, let's go through that example step by step.

Step zero: Before you look at any numbers, you have to set up your initial values for all the variables you're going to use. What values are you going to want to keep track of, and what will they start out at?

### #13 hhman1

Reputation: 0
• Posts: 23
• Joined: 02-May 13

## Re: Using max to find two numbers

Posted 04 May 2013 - 09:04 PM

I want to keep track of the largest number and second largest number, both of which will have an initial value of one. I want to keep track of those values as well and when there is an occurrence, add to the final count.

### #14 jon.kiparsky

• Pancakes!

Reputation: 8565
• Posts: 14,815
• Joined: 19-March 11

## Re: Using max to find two numbers

Posted 04 May 2013 - 09:10 PM

Initial value of 1? Well, okay. I'd use zero, but it's not that big a deal. What about the frequency counts? Do you want to initialize those? And what do you want to call these? Let's not call them "count" and "counter" again - that's confusing!

### #15 hhman1

Reputation: 0
• Posts: 23
• Joined: 02-May 13

## Re: Using max to find two numbers

Posted 04 May 2013 - 09:21 PM

I don't understand the implications of using one versus zero but that is off topic I think. What do you mean by frequency counts? It doesn't say anything in my book about those and I don't have a teacher. What they are called doesn't matter to me. I do get that count and counter could be confusing to some people though.

• (3 Pages)
• 1
• 2
• 3

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }