# Write an algorithm in pseudocode

Hi :)
I am really having problems with pseudocode as I have never had to use it before.  I have been asked to write an algorithm in pseudocode that allows the user to input the selling prices of all homes in a town sold during 2009 and determine the median selling price.  The median of a list of N numbers is as follows:
- the middle number of the sorted list, if N is odd
- the average of the two middle number in the sorted list, if N is even
The user will terminate the input by entering a zero or a negative number.

I would really appreciate any help and would be so grateful if somebody could explain how I go about this.

Thank you.
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
pseudo code is very close to writing down the algorithm in concise phrases that resemble code (from far).

For example :

get number from user
increment number by 5
show number

Try giving it a go, and see what you can come up with. You can then post it here, and we'll have a look at it for you.
0
Commented:
Note that the example I gave is very high level pseudo code (ie. it's very close to human speech). If you prefer to get closer to code, the same might look something like this :

number = get_from_user()
number = number + 5
show(number)

or anywhere between these two extremes - as you prefer.
0
Author Commented:
Thank you - I will give it a go and post on here
0
Commented:
The best way to learn something is to try to imitate others. Try to get hold of a problem and its pseudocode and see how it was done. Here are some links to help you become acquainted with pseudocode:

http://faculty.ccri.edu/mkelly/COMI1150/PseudocodeBasics.pdf
http://en.wikipedia.org/wiki/Pseudocode
http://polya.computing.dcu.ie/ca165/overview/lectures/w8/problemsolving/progdesign.pdf
www.cse.unr.edu/~bei/examples/pseudocodes/Pseudocode.doc

After reading these, and maybe finding other examples, then give it a try and post your results.
0
Author Commented:
Thank you - I'm having a look at them now
0
Author Commented:
Hi - I've had a go at this but I think I need to go a bit more in depth - please could somebody have a quick look and let me know if I'm getting the hang of it at all as I'm not sure I am :s

Thank you
0
Author Commented:
And here it is....

number = get_from_user()
/*
Ask user to input the selling prices of all homes sold in 2009
To terminate list ask user to enter zero
Array sort
/*Sort list into ascending order
Count numbers input
If N is odd – output middle number
If N is even – find average of two middle numbers and output
0
Commented:
That's a good start. But I think you'll need to be a bit more explicit about each of the separate steps - especially for getting the input from the user. Try adding a loop into your pseudo code that continues for as long as the user doesn't enter 0.
0
Author Commented:
Thank you,

Does this look more like it?  I have been searching for help with pseudocode for hours now as we only had a brief lecture on it which wasn't really very in depth so I'm not really too sure how much infomation I actually need to add and if I need to add the actual code so I'm sorry if this doesn't make much sense.

number = get_from_user()
/* Ask user to input numbers
Question 4
number = get_from_user()
/*
WHILE number >0 Ask user to input the selling prices of all homes sold in 2009
ENDIF
To terminate list ask user to enter zero
Array sort
/*Sort list into ascending order
Count numbers input
If N is odd – output middle number
If N is even – find average of two middle numbers and output
0
Commented:
>> I'm not really too sure how much infomation I actually need to add

That depends on the audience the pseudo code is intended for. If it's just for yourself, then you add as much information as you need. If it's for someone else, then you have to consider what level of detail the other person(s) might require. In the case of an assignment for example, you'd usually need a relatively high level of detail.

>> if I need to add the actual code

pseudo code is a way to quickly write down how something should work, before actually writing the code. So, you don't add actual code in there (unless it's easier to express an idea that way).

One thing I notice, is that you seem to be adding comments in your pseudo code. Don't do that : the whole idea of pseudo code is that it should be self-descriptive enough, so no comments are needed.

And some specific notes :

>>   number = get_from_user()

what's this line there for ?

>> WHILE number >0 Ask user to input the selling prices of all homes sold in 2009

A few remarks here :

(a) use multiple lines to express separate ideas. If you have a loop, then make sure that the loop body is on separate lines between the start and end of the loop
(b) think carefully about what happens in each iteration. Currently you say, that for every iteration, you ask the user to inpu the selling prices of all homes.  That's not what should be happening - for each iteration, you should ask the selling price of only one home
(c) for each iteration, there's a bit more going on than just asking for the selling price of a home. You need to also do something with that selling price.

>> To terminate list ask user to enter zero

The user needs to know this information before entering the loop. Otherwise, he won't know how to stop.
0
Author Commented:
Brilliant - thank you for that - I'm off for my dinner now but will give it a go after some brain food and no doubt come back and start bothering you again :)
0
Commented:
No problem. Enjoy your dinner :)
0
Author Commented:
Dinner was very good thank you :)

Okay - well here's my next attempt when taking into account the advice you gave me:

DECLARE Price, N

DISPLAY “Please input the selling price of the home sold in 2009.  To terminate the input you must enter a zero”

GET Price

Store total of numbers input (N)
WHILE Price >0
DISPLAY “Please input the price of another home sold in 2009”
ENDIF
When  the user has entered the zero to terminate the input count numbers input (N)
Sort input numbers into a list in ascending order
If N is odd the Median = middle number of the sorted list
DISPLAY “The median selling price of house sold in 2009 is:” + Median
ELSE
If N is even the Median =  the average of two middle numbers of the sorted list
DISPLAY “The median selling price of house sold in 2009 is:” + Median
0
Commented:
>> Store total of numbers input (N)

You don't know the amount of values input yet - you'll know that after the loop

>> DISPLAY “Please input the price of another home sold in 2009”

You'll also have to actually get the input, and store it somewhere

>> When  the user has entered the zero to terminate the input count numbers input (N)

You don't need to explicitly say "When  the user has entered the zero to terminate the input" - that is implicit from the while loop just before it. No need to be too verbose ;)

>> DISPLAY “The median selling price of house sold in 2009 is:” + Median

You have this line twice - you could have it only once ... :)
0
Commented:
Other than that, it's starting to look good !
0
Author Commented:
Hello again :).  I was wondering if you could have a quick look at this for me please.  I have taken your advice and tried what I think is right - I also found out some formulas using modulas and added them, if you're around today would you let me know what you think please.  I sincerley appreciate all the help and advice you are giving me.

DISPLAY “Please input the selling price of the home sold in 2009.  To terminate the input you must enter a zero”

GET UserInputPrice

WHILE UserInputPrice >0
DISPLAY “Please input the price of another home sold in 2009”
ENDIF
COUNT numbers input
SORT  PriceList (N)
If (N) PriceList % 2 == 0 (Even)
medianPrice = ( PriceList[N/2] + PriceList[N/2 + 1 ] ) / 2 (Average the middle two prices)
ELSE
medianPrice = ( PriceList[N/2 + 1]  (Get the middle price)
DISPLAY “The median selling price of house sold in 2009 is:” + medianPrice
0
Commented:
It's looking a lot better. Just a few more points :

>> WHILE UserInputPrice >0
>> DISPLAY “Please input the price of another home sold in 2009”
>> ENDIF

You still have a loop that just asks the user to input a price. You don't actually get the price, or do anything with it.

>> ADD UserInputPrice to (N) PriceList

Are you sure about the location of this statement ? Think about what it does, and when it should happen ... Should it be executed more than once ?
0
Author Commented:
Hello again  :) - I'm sorry to be such a nuisance I just really don't want to mess this up, I am so grateful to you.

Does this look any better?

DISPLAY “Please input the selling price of the home sold in 2009.  To terminate the input you must enter a zero.”

GET UserInputPrice

WHILE UserInputPrice >0
DISPLAY “Please input the price of another home sold in 2009.”

ENDIF
0
Commented:
>> I'm sorry to be such a nuisance

You're not a nuisance at all ! I wouldn't sign up as expert on this site, if I wasn't prepared to help people out however I can :)

>> Does this look any better?Does this look any better?

Looks a lot better. But you probably also want to get the price inside the while loop. Otherwise the user enters it for nothing ;)
0
Author Commented:
Hello :)
I was just wondering - is the price not inside the loop from when I did:
0
Commented:
Before the loop, you did a 'GET UserInputPrice', followed by a 'ADD UserInputPrice to (N) PriceList'. So, you get the input price from the user, and then add it to the list.
Inside the loop, you just have 'ADD UserInputPrice to (N) PriceList', so you just add a price to the list - you don't actually get the new input price from the user.
0
Author Commented:
Oh dear - I think I have overdosed on Pseudocode and my head is not taking it in anymore.......:).

I guess this is how I should have done it then?:

Question 4
DISPLAY “Please input the selling price of the home sold in 2009.  To terminate the input you must enter a zero.”

GET UserInputPrice

WHILE UserInputPrice >0
DISPLAY “Please input the price of another home sold in 2009.”
GET UserInputPrice

ENDIF
COUNT numbers input
SORT  PriceList (N)
If (N) PriceList % 2 == 0 (Even)
medianPrice = ( PriceList[N/2] + PriceList[N/2 + 1 ] ) / 2 (Average the middle two prices)
ELSE
medianPrice = ( PriceList[N/2 + 1]  (Get the middle price)
DISPLAY “The median selling price of house sold in 2009 is:” + medianPrice
0
Commented:
That looks like a decent, clear and complete pseudo code :)
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Are you serious????????????????????  Have I finally done it????????????????
I thank you SO, SO much for your help - you have no idea how much this was getting to me, once again THANK YOU :)
0
Author Commented:
Absolutey outstanding and invaluable help - thank you so much
0
Commented:
Glad to be of assistance. You did great :)

Pseudo code no longer has any mysteries for you now ;)
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Algorithms

From novice to tech pro — start learning today.