Solved

Write an algorithm in pseudocode

Posted on 2010-08-18
12
1,112 Views
Last Modified: 2012-05-10
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
Comment
Question by:peploe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 7

Expert Comment

by:50cal
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
noList.ADD userValue
LOOP

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

Maybe???


0
 
LVL 1

Expert Comment

by:schlueter
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

by:peploe
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
LVL 7

Expert Comment

by:50cal
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

by:50cal
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

by:schlueter
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

by:peploe
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

by:schlueter
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

by:peploe
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

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

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

Author Comment

by:peploe
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

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

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

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

Join & Ask a Question