programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Comparing 10 Integers

John Stuart
Greenhorn
Posts: 12
• 1

(Find the Largest Number) The process of finding the largest value is used frequently in computer applications. For example, a program that determines the winner of a sales contest would input
the number of units sold by each salesperson. The salesperson who sells the most units wins the contest. Write a pseudocode program, then a Java application that inputs a series of 10 integers
and determines and prints the largest integer. Your program should use at least the following three variables:
a) counter: A counter to count to 10 (i.e., to keep track of how many numbers have been
input and to determine when all 10 numbers have been processed).
b) number: The integer most recently input by the user.
c) largest: The largest number found so far.

You can only use Scanner class for input, loop (while, for, do..while), increment operator, and condition. You may not use "or" (||) or "and" (&&) in the condition but you can use relational operator (>, <, <=, >=). Why? Because this exercise appears at the end of a chapter where these are the only things that are taught.

Is this possible? Or am I misinterpreting the test?

Campbell Ritchie
Marshal
Posts: 56599
172
That sounds really easy.

What you should do is write down on paper how you would find the largest of 10 numbers. If you can do that you can easily translate that into code.

John Stuart
Greenhorn
Posts: 12
Well, I got it. My program works well. I just take things too far before.

Liutauras Vilda
Sheriff
Posts: 4928
334
• 1
Hi John,

After you do the steps Campbell Ritchie suggested to you, and you should take that seriously, otherwise time solving the problem likely would become very complicated.

What I'd like to add from myself:
1. Don't put all your code in a "main" method, but instead, create an another method/-s to do the task you have been asked (keep in mind, it could be, i'd say should be more than one method you need to create)
2. One of the methods could be to take users inputs and put them in a data structure
3. One method to determine most recent input by the user. So likely it should take as an argument above method's returned value/object.
4. Another method to determine largest value in your data structure. So again probably it would take returned value/object of your method defined in 2. step.

As you see, there is something you need to think about, before you start coding. So, Campbell's given suggestion starts making the sense, isn't it?
And high probability that all such a tasks suppose to start with a pen and piece of paper.

Moreover, once you have all on a piece of paper, basically you get "first" part of your assignment solved, which is sounds "Write a pseudocode program..".

Best luck

Campbell Ritchie
Marshal
Posts: 56599
172
John Stuart wrote:Well, I got it. My program works well. I just take things too far before.
In which case let's see how good it is

Campbell Ritchie
Marshal
Posts: 56599
172
Do you need a data structure, Liutauras? The original assignment simply said to find the largest number.

Liutauras Vilda
Sheriff
Posts: 4928
334
Campbell Ritchie wrote:Do you need a data structure, Liutauras? The original assignment simply said to find the largest number.

Write a pseudocode program, then a Java application that 1. (inputs a series of 10 integers) and 2. (determines and prints the largest integer)

edit: when I said data structure, I took array's definition as: "an array data structure or simply an array is a data structure..."

Liutauras Vilda
Sheriff
Posts: 4928
334
And I'm reading details again, and a bit not sure, what needs to be done.

First interpretation: inputs a series of 10 integers and only then determines and prints the largest integer.
Another interpretation: takes inputs of 10 integers, and determines the largest number, where "largest: The largest number found so far". So it sounds like checking on the fly.

Well, for me is not that clear which interpretation to follow.

Liutauras Vilda
Sheriff
Posts: 4928
334
You can only use Scanner class for input, loop (while, for, do..while), increment operator, and condition.
And likely Campbell Ritchie was right - I over complicated, because there is a list of things OP can use.

Well, but then in description says: "For example, a program that determines the winner of a sales contest would input the number of units sold by each salesperson. The salesperson who sells the most units wins the contest."
What happens if two or more salespersons sold the same amount of units? Previous records would be lost, so you wouldn't be able to identify all of them, which salespersons and how many of them sold mostly (the same amount).

I know, digging probably too deep, need to defend my position now

John Stuart
Greenhorn
Posts: 12

Paweł Baczyński
Bartender
Posts: 2087
44
• 1
I might be a little nitpicky but what if only negative numbers are entered? Your program will print The largest number is: 0 which won't be true.

An example:
Insert number 1: -10
Insert number 2: -5
Insert number 3: -7
Insert number 4: -2
Insert number 5: -4
Insert number 6: -111
Insert number 7: -156
Insert number 8: -1561
Insert number 9: -5
Insert number 10: -21
The largest number is: 0

I would initialize largest to Integer.MIN_VALUE.

Also, don't use \n. Use %n.

Liutauras Vilda
Sheriff
Posts: 4928
334
And probably would need to restrict user from entering negative values, if you'd be asked to write a program as a actual given example, because sale units couldn't be negatives at all.

Campbell Ritchie
Marshal
Posts: 56599
172
I would prefer to see several methods rather than having everything in the main method. I also don't like multiple declarations on the same line. Apart from that, the program is good.

John Stuart
Greenhorn
Posts: 12
Thanks for your time everyone, but apparently this is not the end.. Further down, the book says:

(Find the Two Largest Numbers) Using an approach similar to that for Exercise 4.21 (refers to my first question), find the two largest values of the 10 values entered. [Note: You may input each number only once.]

I'm ripping my hair here, I've been thinking about this since last night. Since the book doesn't provide any sample output , I guess this is what they want:

//This output is manually typed.

Insert number 1: 1
Insert number 2: 2
Insert number 3: 3
Insert number 4: 4
Insert number 5: 5
Insert number 6: 6
Insert number 7: 7
Insert number 8: 8
Insert number 9: 9
Insert number 10: 10
The largest number is: 10
The second largest number is: 9

Campbell Ritchie
Marshal
Posts: 56599
172
Pencil and paper. Write down how to find the largest, and at the same time recording what the next largest number was. Remember you are not creating a new method but enhancing the previous technique.
The simplest method will not work well if you have two equal values for the largest, or you enter the largest number first.