Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1116
  • Last Modified:

Write an algorithm in pseudocode

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
      DISPLAY “Please enter your next number.”
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
peploe
Asked:
peploe
  • 5
  • 4
  • 3
1 Solution
 
50calCommented:
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
noList.ADD userValue
LOOP

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

Maybe???


0
 
schlueterCommented:
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
 
peploeAuthor Commented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
50calCommented:
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
 
50calCommented:
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
 
schlueterCommented:
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
 
peploeAuthor Commented:
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
 
schlueterCommented:
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
 
peploeAuthor Commented:
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
 
schlueterCommented:
WHILE (NUM = ASK(“Please enter a number.”));

If the number is 0 (this means: false) , the loop will cancel...
0
 
peploeAuthor Commented:
Thank you so much for your help - I think I am starting to get the hang of this a bit now :)
0
 
schlueterCommented:
Okay, I hope your problem was solved.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now