Finding middle numbers in pseudocode

  • Thread starter Thread starter Travian
  • Start date Start date
  • Tags Tags
    Numbers
Click For Summary
SUMMARY

The discussion focuses on creating a pseudocode algorithm to determine the median selling price of homes sold in Kingston during 2009. The algorithm requires users to input selling prices, terminating with a zero or negative number. Participants suggest using an array to store the prices, followed by sorting the array to find the median. The median is defined as the middle number for an odd count of prices or the average of the two middle numbers for an even count.

PREREQUISITES
  • Understanding of pseudocode syntax and structure
  • Familiarity with array data structures
  • Knowledge of sorting algorithms
  • Basic concepts of median calculation
NEXT STEPS
  • Implement a pseudocode algorithm for median calculation
  • Research various sorting algorithms suitable for arrays
  • Explore dynamic array allocation techniques in pseudocode
  • Learn about computational efficiency in sorting and median finding
USEFUL FOR

This discussion is beneficial for students learning algorithm design, programmers working with data analysis, and anyone interested in implementing statistical calculations in pseudocode.

Travian
Messages
64
Reaction score
0

Homework Statement


Write an algorithm in pseudocode that allows the user to input the selling prices of all homes at Kingston 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 numbers in the sorted list if N is even.

The user will terminate the input by entering a zero or a negative number.


Homework Equations





The Attempt at a Solution



Code:
Declare Counter as integer
Declare Number as float
Set Counter = 0
Set Number = -1
Display "Enter a number. Enter a zero or negative number to finish"
WHILE Number > 0
    ?
ENDWHILE
i can't imagine how to do it? i think there should be some arrays involved. any ideas?
 
Physics news on Phys.org
In comparison to the other problems you posted recently, this one is a lot more difficult. One approach would be to use an array that is more than large enough, then cycle through the sales prices, storing them in the array. After the numbers are in the array sort the sales prices (the non-zero values in the array), count them, and then pick the middle number (odd number of prices) or the average of the two numbers in the middle (even number of prices).
 
I think that there are various sorting algorithms, however, you don't need to sort your entire array. By that, I mean you're only interested in whatever number lies right in the middle. So, if you have N number of prices, then you can do something like:
Code:
REAL,DIMENSION(:),ALLOCATABLE :: data,sorted

!--Input your data here

DO i=1,N/2
  !--Find the smallest entry 
  temp = 1.e99
  DO j=1,N
    IF ( data(j) .LT. temp ) THEN
      temp = data(j)
    END IF
  END DO
  sorted(i) = temp
END DO

!--We now have the first half of data sorted.

You then need to find a clever way to do your DO loop such that you always loop the correct number of times such that you have enough (and just enough for computational efficiency) data to find the median.
 

Similar threads

  • · Replies 45 ·
2
Replies
45
Views
18K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
5
Views
2K
  • · Replies 7 ·
Replies
7
Views
8K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
6
Views
5K