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

Username:   Password: 
 RegisterRegister   
 Finding the mode and median if the given numbers
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MysticVegeta




PostPosted: Wed Nov 17, 2004 5:42 pm   Post subject: Finding the mode and median if the given numbers

I have 10 input numbers, now i know how to calculate the mean and maximum and minimum but i am stuck with how to calculate the mode
Sponsor
Sponsor
Sponsor
sponsor
Neo




PostPosted: Wed Nov 17, 2004 6:25 pm   Post subject: (No subject)

Well, I dont know if there is an easier way but this is how I would do it...

code:
var num, samenum : array 1 .. 10 of int
var show : array 1 .. 10 of boolean
var z := 9

for i : 1 .. 10
    put "Enter the ", (i), " number: " ..
    get num (i)
    samenum (i) := 1
    show (i) := true
end for

for x : 1 .. 10
    for i : 1 .. z
        if num (x) = num (x + i) and show (x) then
            samenum (x) += 1
            show (x + i) := false
        end if
    end for
    z -= 1
end for

put""

for i : 1 .. 10
    if show (i) = true then
        put "The number ", num (i), " appears ", samenum (i), " times."
    end if
end for
Andy




PostPosted: Wed Nov 17, 2004 6:26 pm   Post subject: (No subject)

have two variables
one maned maximum, one namedm minimum and set the minimum to maxint and maximum not minint

run a for loop through the list of numbers, and check if the number is larger than maximum or smaller than minimum, then set that number to the maximum/minimum.. after the whole list, u'd have the max and min
zylum




PostPosted: Wed Nov 17, 2004 10:20 pm   Post subject: (No subject)

he knows how to do that, hes asking how to find the mode Wink

code:
var numbers : array 1 .. 10 of int := init (1, 2, 3, 4, 2, 3, 2, 2, 1, 4)
var mode, maxNum, num, tempMax : int
maxNum := 0

for i : 1 .. upper (numbers)
    num := numbers (i)
    tempMax := 0
    for j : 1 .. upper (numbers)
        if num = numbers (j) then
            tempMax += 1
        end if
        if tempMax > maxNum then
            mode := numbers (i)
            maxNum := tempMax
        end if
    end for
end for

put mode
MysticVegeta




PostPosted: Thu Nov 18, 2004 8:34 pm   Post subject: (No subject)

Uh thanks ppl but can some1 help me with median?????????????????
zylum




PostPosted: Thu Nov 18, 2004 8:53 pm   Post subject: (No subject)

common, just sort the array and get the middle element Confused
MysticVegeta




PostPosted: Fri Nov 26, 2004 7:35 am   Post subject: (No subject)

I tried to sort the array but somehow it doesnt work, the highest number goes in the last or in the middle, can you post an example?
Blade




PostPosted: Fri Nov 26, 2004 11:00 am   Post subject: (No subject)

basically to sort an array, you will need 2 for loops... one from 1 to your max(call it "i") .. and one from "i" to your max (call it "j")... because this type of sorting is so unefficient, you can make it more efficient by running the second loop this way, because you will have either the smallest or the largest number at the beginning of the array by the time you run through "i" once, so you can cut out comparing that value in future runs.. now if you are going to sort it from smallest to largest, you're going to have to compare it to see if number(i) is greater than number(j) .. if it is, you will then switch them.. obviously you will have to have a temporary variable to store one of the numbers temp:=number(i)... then you will set the other number to the one you have a backup of number(i):=number(j) ... then you can set the first number to the other number(j):=temp ... not too complicated eh?

now you can get your median by dividing maxnum by 2 and getting the value of that element

btw, this is the most unefficent sort method there is, but its also the simplest, i'm not gonna get into the other types
Sponsor
Sponsor
Sponsor
sponsor
MysticVegeta




PostPosted: Sat Nov 27, 2004 5:20 pm   Post subject: (No subject)

Any examples???? Pls
Blade




PostPosted: Sun Nov 28, 2004 5:21 am   Post subject: (No subject)

i dont know how it can be any clearer than that without giving you the answer :S

code:

  %declare variables
   for i:1..max
     for j: i .. max
        if(numbers(i) > numbers(j)) then
           temp := numbers(i)
           numbers(i) := numbers(j)
           numbers(j):=temp
        end if
       end for
   end for
MysticVegeta




PostPosted: Tue Nov 30, 2004 5:33 pm   Post subject: (No subject)

Error at max
zylum




PostPosted: Tue Nov 30, 2004 5:55 pm   Post subject: (No subject)

you're not even trying are you Confused max is supposed to represent the amount of numbers you are sorting
MysticVegeta




PostPosted: Sun Dec 19, 2004 6:59 pm   Post subject: (No subject)

yep gotcha

code:
var temp : int
var numbers : array 1 .. 6 of int
for a : 1 .. 6
    get numbers (a)
end for
put ""

%declare variables
for i : 1 .. 6
    for j : i .. 6
        if (numbers (i) > numbers (j)) then
            temp := numbers (i)
            numbers (i) := numbers (j)
            numbers (j) := temp
        end if
    end for
    put numbers (i)
end for
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  [ 13 Posts ]
Jump to:   


Style:  
Search: