Finding middle numbers in pseudocode

  • Thread starter Travian
  • Start date
  • Tags
    Numbers
In summary: The easiest way to do this is by using an IF statement, as follows:REAL,DIMENSION(:),ALLOCATABLE :: data,sorted!--Input your data hereIF (data(j) .LT. temp) THEN temp = data(j) sorted(i) = temp ELSE sorted(i) = data(j) + 1 END IFEND DO!--We now have the second half of data sorted.The IF statement will only execute the DO loop if the data in the jth row is smaller than
  • #1
Travian
64
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
  • #2
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).
 
  • #3
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.
 

Related to Finding middle numbers in pseudocode

1. What is pseudocode?

Pseudocode is a simplified, non-programming language used to describe a sequence of steps or an algorithm. It is often used as a tool for planning and designing programs before they are written in a specific programming language.

2. Why is finding middle numbers important?

Finding middle numbers is important because it allows us to identify the median, or middle value, in a set of numbers. This can be useful in data analysis and calculations, as well as in sorting algorithms.

3. How do you find the middle number in pseudocode?

To find the middle number in pseudocode, you can first sort the numbers in ascending or descending order. Then, if the total number of values is odd, the middle number will be the one at the center of the list. If the total number of values is even, the middle number will be the average of the two numbers at the center of the list.

4. What is the purpose of using pseudocode to find middle numbers?

The purpose of using pseudocode to find middle numbers is to provide a clear and easily understandable description of the steps involved in the algorithm. This can help in the planning and debugging process, and can also be useful for collaboration and communication between team members.

5. Can pseudocode be directly translated into a programming language?

While pseudocode can serve as a helpful guide for writing actual code, it is not meant to be directly translated into a programming language. Pseudocode is a rough outline of the steps involved in an algorithm, and it is up to the programmer to decide on specific syntax and logic when implementing the code.

Similar threads

  • Engineering and Comp Sci Homework Help
2
Replies
45
Views
16K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
965
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
29
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Back
Top