Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Help with arrays!! ASAP!
Author Message
chibitenshi_03

Posted: Thu Mar 04, 2004 6:10 pm   Post subject: Help with arrays!! ASAP!

i need to make a program using arrays that randomly output numbers. Then i need to calculate the average, median and mode. I know how to get the average but how do you find the median and mode?
This is what i have so far:
var count, sum : int
sum := 0

% ask the user how many numbers there are
put "How many numbers do you want to generate?"
get count

var number : array 1 .. count of int

for a : 1 .. count
randint (number (a), 1, 10)% randomly choose numbers
put number (a), " " .. % output numbers
sum := sum + number (a) % add up all the numbers
end for

put skip
put "The average is ", sum / count, "."
Sponsor
Sponsor

Cervantes

Posted: Thu Mar 04, 2004 6:29 pm   Post subject: (No subject)

to find the median you need to sort the numbers into another array.
Ways to sort are many and found all across compsci.ca. Here is a bubble sort

 code: var count : int 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 : int := 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) end for

once sorted, you output the middle element of the array.
chibitenshi_03

Posted: Thu Mar 04, 2004 6:39 pm   Post subject: (No subject)

Do you kno how to do the mode?
Cervantes

Posted: Thu Mar 04, 2004 6:43 pm   Post subject: (No subject)

been fiddling around with that one. It's kinda tricky.

Here's what I've done. It's the basis. However you need to expand it to make it so that you can enter the number of random nums to be created. When you do that, however, you will also need to error proof it, as you will have problems with "array subscript is out of range".

 code: var list : array 1 .. 10 of int for i : 1 .. 10     list (i) := Rand.Int (1, 10) end for var mode : array 1 .. 10 of int for p : 1 .. 10     mode (p) := 0 end for for k : 1 .. 10     mode (list (k)) += 1 end for for o : 1 .. 10     put "# of ", o, "'s: ", mode (o) end for

from there you sort the mode array and output the biggest one.
chibitenshi_03

Posted: Thu Mar 04, 2004 6:45 pm   Post subject: (No subject)

Thanks for the help! ^^
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 5 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: