Solved

Write an algorithm in pseudocode

Posted on 2010-08-16
26
780 Views
Last Modified: 2012-05-10
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.
0
Comment
Question by:peploe
  • 13
  • 12
26 Comments
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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 Comment

by:peploe
Comment Utility
Thank you - I will give it a go and post on here
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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 Comment

by:peploe
Comment Utility
Thank you - I'm having a look at them now
0
 

Author Comment

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

by:peploe
Comment Utility
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
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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 Comment

by:peploe
Comment Utility
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
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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 Comment

by:peploe
Comment Utility
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
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
No problem. Enjoy your dinner :)
0
 

Author Comment

by:peploe
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
Other than that, it's starting to look good !
0
 

Author Comment

by:peploe
Comment Utility
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
ADD UserInputPrice to (N) PriceList
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
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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 Comment

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

ADD UserInputPrice to (N) PriceList

WHILE UserInputPrice >0
DISPLAY “Please input the price of another home sold in 2009.”
      ADD UserInputPrice to (N) PriceList

ENDIF
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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 Comment

by:peploe
Comment Utility
Hello :)
I was just wondering - is the price not inside the loop from when I did:
ADD UserInputPrice to (N) PriceList
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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 Comment

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

ADD UserInputPrice to (N) PriceList

WHILE UserInputPrice >0
DISPLAY “Please input the price of another home sold in 2009.”
      GET UserInputPrice
      ADD UserInputPrice to (N) PriceList

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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
Comment Utility
That looks like a decent, clear and complete pseudo code :)
0
 

Author Comment

by:peploe
Comment Utility
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 Closing Comment

by:peploe
Comment Utility
Absolutey outstanding and invaluable help - thank you so much
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
Glad to be of assistance. You did great :)

Pseudo code no longer has any mysteries for you now ;)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now