Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help finding the median number in a program
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JR




PostPosted: Thu Feb 26, 2004 9:01 am   Post subject: Help finding the median number in a program

ok, this is my program and i need to find the median(middle) of the numbers. heres my code:

code:
var count : int
var num : int
put "How many integers are in the list? " ..
get count
var list : array 1 .. count of int
for i : 1 .. count
    get list (i)
end for
var sortList : array 1 .. count of int
for i : 1 .. count
    var smallest := 999
    var where : int
    for j : 1 .. count
        if list (j) < smallest then
            smallest := list (j)
            where := j
        end if
    end for
    sortList (i) := smallest
    list (where) := 999
end for
for i : 1 .. count
    put sortList (i) ..
    put " ; " ..
end for
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Feb 26, 2004 2:03 pm   Post subject: (No subject)

what you do is you sort your array in decending/accending order (doesn't matter... should be same answer) using something like a bubble sort for example...

then just pick the number in the middle of the array by dividing it's upper limit into half.

code:

put myArray(round(upper(myArray)/2))
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
nis




PostPosted: Thu Feb 26, 2004 5:16 pm   Post subject: (No subject)

to sort your list use this code

code:

for decreasing i : count - 1 .. 1
   for j : 1 .. i
      if list(j) < list(j+1) then
          temp := list(j)
          list(j) := list(j+1)
          list(j+1) := temp
      end if
   end for
end for

then just add the code

code:

put "The median is ", list(count div 2)
Cervantes




PostPosted: Thu Feb 26, 2004 5:27 pm   Post subject: (No subject)

Rolling Eyes
he's already got the sorting part. It's in his post...

Tony, what's a bubble sort? Is it what he has there? I haven't learned the ways of sorting in class yet, only on my own.
JR




PostPosted: Thu Feb 26, 2004 5:43 pm   Post subject: (No subject)

got it to work, thx alot
Tony




PostPosted: Thu Feb 26, 2004 8:52 pm   Post subject: (No subject)

Cervantes - yeah, that's whats posted up... you check variables with each other and if one is larger then the other, you switch them
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: