Solved

# Psuedocode:  How do I Display the largest list of Positive Numbers?

Posted on 2009-02-19
1,035 Views
I am so stuck with this problem im new to this and very very confused.
Heres the Question      Find and Display the largest of a list of Postive Numbers entered by the user. The user should indicate that he/she has finished entering numbers by entering 0. How would i solve this problem?
0
Question by:Jasminne

LVL 31

Expert Comment

There's a big difference between the "largest list of positive numbers" and the "largest OF a list of positive numbers". Which is it?
0

Author Comment

Largest of a list....?
0

LVL 84

Expert Comment

start with the largest so far being less than any positive number.

read each number in turn and if it is larger than the largest so far, adjust the largest so far to be equal to the new number
as long as the number is not 0, go back and read another number
0

Author Comment

I dont know if this is right is there any way to shorten this solution
Declare Number, Highest as Float
Write Highest Number
Write This program will find the highest number in a list of positive numbers entered by the
Write user.
Write Please enter a positive number, enter 0 when you are finished:
Input Number
While Number < 0
Write You have entered a negative number.  Please enter a positive number, enter
Write 0 when you are finished:
Input Number
End While
Set Highest = Number
While Number != 0
Write Please enter another positive number, enter 0 when you are finished:
Input Number
While Number < 0
Write You have entered a negative number.  Please enter a positive number,
Write enter 0 when you are finished:
Input Number
End While
If Number > Highest
Set Highest = Number
End If
End While
Write The highest number that you entered is: , Highest
End Program
0

LVL 84

Assisted Solution

> is there any way to shorten this solution
given the similarities  between the code for reading the first number and reading subsequent numbers, you may be able to collect it into a subroutine that you only need to write once,
Or, you may be able to arrange your initial conditions such that only the code for reading the rest of the list is needed because you have arranged fir it to do the right thing when it reads hr first number.

Why is  shortening the solution important?
0

LVL 31

Accepted Solution

Shortening the solution is important when, academically, your teacher considers a "shorter" solution a "better" solution ;)

Simple pseudocode:

Begin Program
LastMaxValue = 0
Begin Loop
If Number > LastMaxValue
LastMaxValue = Number
End If
If Number = 0
Break out of loop
End If
If Number < 0
Fail with an error / throw exception
End If
End Loop

Show LastMaxValue to User
End Program

The aesthetic aspects of the program are unrelated, and up to you to implement. You can make it as fancy or as simple as you like in that respect.
0

LVL 20

Expert Comment

As to shortness: I don't see in the specification of the problem that the program should be fail-safe enough to rejedt negative numbers. Hence that test might be thrown away (though input validation *is* a valid issue)
0

LVL 1

Assisted Solution

The following would simply "Find and Display the largest of a list of Postive Numbers entered by the user", without doing anything fancy.

Set L = 0
Do
Input Number
If Number>L
Set L = Number
End If
Until Number =0
If L > 0
Write "The Largest is", L
Else
Write "No positive number has been entered before entering 0."
End If
0

LVL 1

Expert Comment

step 1: logic to get the integers, once a 0 character is entered move on to step 2
step 2: get all the integers from the use storedt in an arraylist
step 3: sort the array list from largest to smallest
step 4: print the list out.

To shorten the solution, use the highest level language you can find, technically it should still pass as pseudo code still.

Use java or C# and you should be able to get it down to maybe 15 lines, a few print statements, a few try clauses and a call to sort.
0

LVL 4

Expert Comment

To find the largest and smallest number in an array, take the first value of array as largest and smallest number initially, and test against all the values of array to find the appropriate values.

Declare Number[100] As Integer
Declare X, Largest, Smallest, Count As Integer
Set Count = 0

Write "Enter a number:"
Input X

While Number != 0
Set Number[Count] = X

// Take the first value of array as smallest and largest initially
IF Count equals 0 Then
Set Largest = Number[Count]
Set Smallest = Number[Count]
End IF

// Find the largest value in the array
IF Largest < Number[Count] Then
Set Largest = Number[Count]
End IF

// Find the smallest value in the array
IF Smallest > Number[Count] Then
Set Smallest = Number[Count]
End IF

Set Count = Count + 1
Write "Enter a number:"
Input X
End While

For (K = Count - 1; K >= 0; K++)
Write Number[K]
End For
0

LVL 100

Expert Comment

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

## Featured Post

Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…