You have four things that you're tracking from one item to the next: you're tracking the largest value and the second-largest values that you've seen so far, and you're keeping track of how many times you've seen each of them. The latter are the frequency counts. When you haven't seen any numbers, how many of the max value have you seen? How many of the second-max? (yes, that's as simple a question as it sounds)

## 37 Replies - 1952 Views - Last Post: 06 May 2013 - 05:08 PM

### #17

## Re: Using max to find two numbers

Posted 04 May 2013 - 09:29 PM

When you haven't seen any numbers it you've seen zero. Is this what you meant earlier?

### #18

## Re: Using max to find two numbers

Posted 04 May 2013 - 09:36 PM

That's correct. What I was talking about earlier was a little more subtle: 1 is a possible value for the max or the second max - it's an actually-occurring value. That means if you get 1 for second-max, you can't be sure whether you've actually seen a 1 or not. Since 0 can't occur in your data set, it's a better value to use to start with (or -1, as I think you did to begin with, would also work) But as I say, that's not a big deal.

So you've set initial values now, you're ready to accept a number. What happens when you read the first item in the list, which is 1?

So you've set initial values now, you're ready to accept a number. What happens when you read the first item in the list, which is 1?

### #19

## Re: Using max to find two numbers

Posted 04 May 2013 - 09:42 PM

One becomes the maximum number and its' occurrence sets to one.

### #20

## Re: Using max to find two numbers

Posted 04 May 2013 - 09:55 PM

Yes, that works. It's possible you'll revise it in a few minutes, but it's right for now.

Next number is 2 - you get the idea, I think, so work through the rest of the list. Use a pencil and paper to keep track of those four variables we initialized earlier, and think about the rules that you're following when you change those values.

Next number is 2 - you get the idea, I think, so work through the rest of the list. Use a pencil and paper to keep track of those four variables we initialized earlier, and think about the rules that you're following when you change those values.

### #21

## Re: Using max to find two numbers

Posted 04 May 2013 - 10:08 PM

One is the largest number with an occurrence of one.

Two is now largest with occurrence of one one is now second with occurrence of one.

Three is now largest with occurrence of one and two is now second with occurrence of one.

Four is now largest with occurrence of one and three is now second with occurrence of one.

Four is now largest with occurrence of one and three is now second with occurrence of two.

Four is now largest with occurrence of two and three is now second with occurrence of two.

Five is now largest with occurrence of one and four is now second with occurrence of two.

Five is now largest with occurrence of one and four is now second with occurrence of three.

{1,2,3,4, 3, 4, 5,4, 1}

Two is now largest with occurrence of one one is now second with occurrence of one.

Three is now largest with occurrence of one and two is now second with occurrence of one.

Four is now largest with occurrence of one and three is now second with occurrence of one.

Four is now largest with occurrence of one and three is now second with occurrence of two.

Four is now largest with occurrence of two and three is now second with occurrence of two.

Five is now largest with occurrence of one and four is now second with occurrence of two.

Five is now largest with occurrence of one and four is now second with occurrence of three.

{1,2,3,4, 3, 4, 5,4, 1}

### #22

## Re: Using max to find two numbers

Posted 04 May 2013 - 10:23 PM

Looks good to me - you left out the last iteration, but that's okay. I think you've now dealt with all of the special cases that are going to come up here. There are five cases to deal with: the number you read can be greater than the max, equal to the max, greater than the second max and less than the max, or equal to the second max, or else it doesn't matter. If you deal with each of those, in that order, and make sure you handle each of those four variables correctly, you should have this working without much more difficulty. Since you've just now handled each those cases in a real example, it's just a matter of translating what you've just done into code.

### #23

## Re: Using max to find two numbers

Posted 06 May 2013 - 11:06 AM

I now have a perfect understanding of what to do. The only thing left is what methods to use?

### #24

## Re: Using max to find two numbers

Posted 06 May 2013 - 02:16 PM

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

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.

The naming conventions of variables is very important to programmers, it allows other users to understand what is going on. That being said, you want to record the frequency (how many times) the highest number is shown, so lets call that firstHighestNumberFrequency, remember there is no such thing as too obvious, and the second highest number would be secondHighestNumberFrequency. So let's initialize those:

int firstHighestNumberFrequency =0; //I like to start at zero so you don't get an off by one bug (the end result being one number off) int secondHighestNumberFrequency =0; //Again starting at zero so we can count up //If the first number happens again then we do count up firstHighestNumberFrequency++ //If the second highest number happens again we once again count up secondHighestNumberFrequency++

This is a great example of good well documented code, however I left out some things for you to do, such as how will we know when to count up and such.

EDIT: Oops, didn't see there was a second page, still a lesson in naming conventions is always good

This post has been edited by **cbennett**: 06 May 2013 - 02:17 PM

### #25

## Re: Using max to find two numbers

Posted 06 May 2013 - 02:58 PM

### #26

## Re: Using max to find two numbers

Posted 06 May 2013 - 03:21 PM

The only part I don't know how to do is the coding to make the current maximum the next largest maximum once a new number comes along.

### #27

## Re: Using max to find two numbers

Posted 06 May 2013 - 03:23 PM

hhman1, on 06 May 2013 - 03:21 PM, said:

The only part I don't know how to do is the coding to make the current maximum the next largest maximum once a new number comes along.

Write it out in plain english, then code it.

I want the current maximum to equal the next largest number.

so

currentMaximum = ????

### #28

## Re: Using max to find two numbers

Posted 06 May 2013 - 03:25 PM

and also

But not necessarily in that order.

And of course

nextMaximum = ????

But not necessarily in that order.

And of course

maxOccurrences = ???? nextMaxOccurrences = ????

### #29

## Re: Using max to find two numbers

Posted 06 May 2013 - 03:29 PM

hhman1, on 06 May 2013 - 03:21 PM, said:

The only part I don't know how to do is the coding to make the current maximum the next largest maximum once a new number comes along.

It is up to you, the artist, to decide how it looks, works, and behaves. Less lines is efficient and neat but can get complex and confusing, you have to decide when it becomes too simple/complex. A good look at the API is always good too, remember someone wrote all that documentation for a reason.

### #30

## Re: Using max to find two numbers

Posted 06 May 2013 - 03:55 PM

So looking at my current code what is wrong?

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 = 0; int maxcount = 0; int nextmax; int nextmaxcount; int number; while((number = in.nextInt()) != 0){ if(number > max){ maxcount = 1; } else if (number == max){ maxcount++; }else if (number > max){ number = nextmax; nextmaxcount = 1; }else if (number == nextmax){ nextmaxcount++; } } System.out.println("The largest number is " + max); System.out.println("The occurence count of the largest number is " + maxcount); System.out.println("The second largest number is " + nextmax); System.out.println("The occurence count of the second largest number is " + nextmaxcount); } }