Solved

Posted on 2009-02-19

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?

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?

11 Comments

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

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

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?

Simple pseudocode:

Begin Program

LastMaxValue = 0

Begin Loop

Read In Number

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.

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

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.

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

By clicking you are agreeing to Experts Exchange's Terms of Use.

This video discusses moving either the default database or any database to a new volume.

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**17** Experts available now in Live!