Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Help finding the median number in a program
Author Message
JR

Posted: 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

Tony

Posted: 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))
Tony's programming blog. DWITE - a programming contest.
nis

Posted: 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

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

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

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

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

got it to work, thx alot
Tony

Posted: 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
Tony's programming blog. DWITE - a programming contest.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 6 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: