Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Write an algorithm in pseudocode

Posted on 2010-08-18
12
Medium Priority
?
1,115 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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 2000 total points
ID: 33467069
Okay, I hope your problem was solved.
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

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…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
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…

664 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