Solved

# Write an algorithm in pseudocode

Posted on 2010-08-18
1,108 Views
Hi (again) :)
I have been asked to:
Write an algorithm in pseudocode that finds and displays the largest of a list of positive numbers entered by the user and the sum of positive numbers.  The user should indicate that s/he has finished entering numbers by entering a zero.

I was wondering if somebody could have a look over what I've done so far as I'm still not sure I'm getting this right....

Here it is:

DISPLAY “Please enter a number.  This can be negative or positive.  To indicate that you have finished entering numbers, please enter a zero.”
GET Number
WHILE Number is not equal to 0
END IF
STORE Number
IF Number is > 0 add to NumList
SET Highest = Number
IF Number > Highest
SET Highest = Number
WHILE Number is not equal to 0
ADD all numbers together from NumList and ADD to TOTAL
DISPLAY “The largest positive number entered is + Hightest as the sum of the positive numbers is” + TOTAL

Thank you in advance
0
Question by:peploe
• 5
• 4
• 3

LVL 7

Expert Comment

ID: 33463850
Maybe, but it does ask to check the list for numbers and then add them rather than check as you go along.
You also wrote it has to be positive and then said enter a negative or positive number.

START
noList = empty
highestNo = 0
total = 0
userValue = 0

While(userValue not 0)
WRITE & FETCH   userValue=  'Enter a number, positive only. Enter 0 when finished'
VALIDATE userValue is Possitive
LOOP

LOOP (Count noList) TIMES
total = total + noList.CurrentValue
if(noList.currentValue > highestNo) THEN (highestNo = noList.currentValue)
LOOP

Maybe???

0

LVL 1

Expert Comment

ID: 33463859
Hi,

I've some problems with understanding your Code, but try the followin:

VAR SUM = 0
VAR BIGGESTNUMBER = 0

WHILE (NUM = ASK(“Please enter a number.  This can be negative or positive.  To indicate that you have finished entering numbers, please enter a zero.”)
SUM += NUM
IF NUM > BIGGESTNUMBER
BIGGESTNUMBER = NUM
END WHILE

DISPLAY "The largest number is " + NUM
DISPLAY "The sum is " + SUM
0

Author Comment

ID: 33463892
Thank you to both of you for your advice :)
50cal - the user is allowed to enter either a negative or positive number but the program is only supposed to account for the positive numbers.
0

LVL 7

Expert Comment

ID: 33463972
Think this is a bit better, my prior one wouldn't of worked properly when 0 was entered.

userValue = 1
noList = [null]
totalValue = 0
highestNo = 0

WHILE(1=1) DO
PRINT "Enter a possitive number, Enter 0 to finish"
userValue = GET_NUMBER
IF(userValue = 0) THEN BREAK_LOOP
IF(userValue IsPossitive) THEN noList.ADD(userValue)
LOOP

i = 0

LOOP (COUNT noList) TIMES
totalValue = totalValue + noList.CurrentElementValue(i)
if(noList.CurrentElementValue(i) > highestNo) THEN (highestNo = noList.CurrentElementValue(i))
i + 1
LOOP
0

LVL 7

Expert Comment

ID: 33464016
Review of Yours as your questions asks.

DISPLAY “Please enter a number.  This can be negative or positive.  To indicate that you have finished entering numbers, please enter a zero.”   >> Seems Fine
GET Number   >>Seems Fine
WHILE Number is not equal to 0
DISPLAY “Please enter your next number.”   >> You dont appear to be collecting the new number
here - just displaying enter a number.
END IF
STORE Number    >> Not part of the loop - will only do this once?
IF Number is > 0 add to NumList  >> Again - not part of the loop, will only check the final answer.
SET Highest = Number    >> Highest is now the new number entered
IF Number > Highest            >> then your comapring the new number to highest which you just set allready
I.E,
Enter 0
Number = 0
Then you set highest to 0
then you compare number (0) to highest (0)
So it's not really checking.

SET Highest = Number
WHILE Number is not equal to 0  >> Just checking 1 number? Not list?
ADD all numbers together from NumList and ADD to TOTAL
DISPLAY “The largest positive number entered is + Hightest as the sum of the positive numbers is” + TOTAL
0

LVL 1

Expert Comment

ID: 33464046
Sorry, I missed the thing with counting only positive numbers...

VAR SUM = 0
VAR BIGGESTNUMBER = 0

WHILE (NUM = ASK(“Please enter a number.  This can be negative or positive.  To indicate that you have finished entering numbers, please enter a zero.”)
IF (NUM < 0) CONTINUE
SUM += NUM
IF NUM > BIGGESTNUMBER
BIGGESTNUMBER = NUM
END WHILE

DISPLAY "The largest number is " + NUM
DISPLAY "The sum is " + SUM
0

Author Comment

ID: 33464146
Thank you both very much for you help - it is invaluable to a noob like me :)

schlueter - could you please explain this part as I would of thought it would continue if NUM >0 and not the other way around:

IF (NUM < 0) CONTINUE
SUM += NUM

50cal - thank you for reviewing my work - I am making some changes now :)
0

LVL 1

Expert Comment

ID: 33464171
IF NUM < 0 CONTINUE

This means that for a number small than zero nothing will happen (loop continues and asks for the next number)

SUM += NUM

If the number is bigger than zero (loop will end if "num = 0", because it is false OR continue, if smaller than zero), NUM will be added to SUM
0

Author Comment

ID: 33464564
schlueter, I'm really sorry if I'm coming across as stupid but I'm totally new to this.  Which part of this code shows that if a zero is entered, it is terminated?
0

LVL 1

Expert Comment

ID: 33464763
WHILE (NUM = ASK(“Please enter a number.”));

If the number is 0 (this means: false) , the loop will cancel...
0

Author Comment

ID: 33467046
Thank you so much for your help - I think I am starting to get the hang of this a bit now :)
0

LVL 1

Accepted Solution

schlueter earned 500 total points
ID: 33467069
Okay, I hope your problem was solved.
0

## Join & Write a Comment Already a member? Login.

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…
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

#### 705 members asked questions and received personalized solutions in the past 7 days.

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

#### Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!