Using max to find two numbers

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

37 Replies - 995 Views - Last Post: 06 May 2013 - 05:08 PM Rate Topic: -----

#1 hhman1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7802
  • View blog
  • Posts: 13,197
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 hhman1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
}
}


Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7802
  • View blog
  • Posts: 13,197
  • Joined: 19-March 11

Re: Using max to find two numbers

Posted 04 May 2013 - 07:09 PM

View Posthhman1, 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?
Was This Post Helpful? 0
  • +
  • -

#5 hhman1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
}
}


Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7802
  • View blog
  • Posts: 13,197
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 hhman1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7802
  • View blog
  • Posts: 13,197
  • 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?
Was This Post Helpful? 0
  • +
  • -

#9 hhman1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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)
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7802
  • View blog
  • Posts: 13,197
  • 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.
Was This Post Helpful? 0
  • +
  • -

#11 hhman1  Icon User is offline

  • New D.I.C Head

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

Re: Using max to find two numbers

Posted 04 May 2013 - 08:48 PM

View Postjon.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.
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7802
  • View blog
  • Posts: 13,197
  • 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?
Was This Post Helpful? 0
  • +
  • -

#13 hhman1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7802
  • View blog
  • Posts: 13,197
  • 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!
Was This Post Helpful? 0
  • +
  • -

#15 hhman1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3