
-----------------------------------
chibitenshi_03
Thu Mar 04, 2004 6:10 pm

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, "."

-----------------------------------
Cervantes
Thu Mar 04, 2004 6:29 pm


-----------------------------------
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 :wink:


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
Thu Mar 04, 2004 6:39 pm


-----------------------------------
Do you kno how to do the mode?

-----------------------------------
Cervantes
Thu Mar 04, 2004 6:43 pm


-----------------------------------
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".


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
Thu Mar 04, 2004 6:45 pm


-----------------------------------
Thanks for the help! ^^
