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

Username:   Password: 
 RegisterRegister   
 How to find median, minumum and maximum with random integers?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ylcc23




PostPosted: Fri Nov 28, 2014 5:05 pm   Post subject: How to find median, minumum and maximum with random integers?

I'm trying to find the median of a set of numbers randomly picked by the computer. I'm also trying to find the maximum and minimum values in the data, but I don't know how to do it. I have tried looking online and in other threads on this site and other sites as well as guessing at what I need to do but I can't seem to get it. I'm using turing version 4.1.2

Turing:


var student : int
var mark : array 1 .. 100 of int
var temp, Max, Min : int
var mean, median : real

randint (student, 30, 75)
put "There are ", student, " students in this class"

for i : 1 .. student
    put "Enter a mark"
    get mark (i)
    if mark (i) > 100 or mark (i) < 0 then
        cls
        put "Please enter a mark between 1 and 100"
        delay (1000)
        cls
    end if
end for
for i : 1 .. student
    put mark (i): 5 ..
end for
put ""
for i : 1 .. student
    for j : 1 .. student - 1
        if mark (j) > mark (j + 1) then
            temp := mark (j)
            mark (j) := mark (j + 1)
            mark (j + 1) := temp
        end if
    end for
end for
for i : 1 .. student
    put mark (i) : 5 ..
end for
put ""
for i : 1 .. student
    for j : 1 .. student - 1
        mean := (mark (j) + mark (j + 1) + temp) / student
    end for
end for
put mean
if student mod 2 = 0 then
    median := (mark () + mark ()) / 2
elsif student mod 2 = 1 then
    median := mark ()
end if
put median





Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Fri Nov 28, 2014 5:17 pm   Post subject: Re: How to find median, minumum and maximum with random integers?

ylcc23 @ Fri Nov 28, 2014 5:05 pm wrote:
as well as guessing at what I need to do but I can't seem to get it.

Tell us about your guesses. Your code seems to be almost there -- if you wrote it yourself, then you are likely to be very close to finding a solution, and just need that last push.

It looks like you are already printing out all of the marks after sorting them
code:

for i : 1 .. student
    put mark (i) : 5 ..
end for

If you were to look at that sorted list of marks, where would you find the max, min, and median?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
ylcc23




PostPosted: Fri Nov 28, 2014 5:35 pm   Post subject: RE:How to find median, minumum and maximum with random integers?

I've guessed at putting random numbers into the brackets but that never actually gets me my median. I know where I would find the median, max and min looking at the list, but I'm not sure how to put that into code. I've made another guess at Max and Min.

Turing:


Min := mark (1)
Max := mark (*)
put Min
put ""
put Max



I'm pretty confident in my guess for min, but I'm not overly confident in my guess for max, as I have a feeling I'll get a syntax error there.
Tony




PostPosted: Fri Nov 28, 2014 5:42 pm   Post subject: Re: RE:How to find median, minumum and maximum with random integers?

ylcc23 @ Fri Nov 28, 2014 5:35 pm wrote:
but I'm not sure how to put that into code.

See, you are almost done Smile

that min looks pretty good to me. max -- you can test syntax validity by actually running the code and let the compiler tell you what is wrong. The intention looks to be "the last element", but I would guess that the syntax (if it works) will give you the last of marks array, and not marks entered. You already have a variable that will tell you the last index though.

median follows the same approach -- you take the math definition of median and translate it into code. Math and code are often very close, and you already have reference points for min and max from above.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
ylcc23




PostPosted: Fri Nov 28, 2014 11:14 pm   Post subject: RE:How to find median, minumum and maximum with random integers?

I tried running the program, and just as I thought, min works perfectly, but I get a syntax error at max. Still haven't quite figured out what the hell to put in the brackets for median. Also haven't quite figured out which variable tells the last index. I'm kinda doing guess and check with that one by putting in one of my variables and seeing if it works. The main thing that's giving me trouble is the random integer part. If it weren't for that I would easily be able to do all of this.
Tony




PostPosted: Fri Nov 28, 2014 11:23 pm   Post subject: Re: RE:How to find median, minumum and maximum with random integers?

ylcc23 @ Fri Nov 28, 2014 11:14 pm wrote:
The main thing that's giving me trouble is the random integer part. If it weren't for that I would easily be able to do all of this.

How would you do it if the "random integer" wasn't involved?

re: median -- from the definition on https://en.wikipedia.org/wiki/Median
Quote:

The median of a finite list of numbers can be found by arranging all the observations from lowest value to highest value and picking the middle one...

You've already done the sorting step, so now you'd need to find where the middle is. You can try it out with a few small examples so see that your approach works.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
ylcc23




PostPosted: Fri Nov 28, 2014 11:30 pm   Post subject: RE:How to find median, minumum and maximum with random integers?

I have another program that I did earlier that was similar to this except I used 5 numbers entered by the user. for that I did
Turing:

if 5 mod 2 = 0 then
    median := (num (2) + num (3)) /2
elsif 5 mod 2 = 1 then
    median := num (3)
end if

Also I'm fairly sure I was able to correctly guess the variable I needed to get max to work.
Turing:

Max := mark (student)
Tony




PostPosted: Sat Nov 29, 2014 12:07 am   Post subject: RE:How to find median, minumum and maximum with random integers?

almost there! You've got the logic for 5 down, but now instead of "5" you have "student" number of items.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
ylcc23




PostPosted: Sat Nov 29, 2014 12:12 am   Post subject: RE:How to find median, minumum and maximum with random integers?

I was able to figure out the number of items really easily. What I really need help with is finding the middle of a set of data where the number of items changes every single time. That's the part that I've been struggling with the most. I've been able to get everything else but that one part.
Tony




PostPosted: Sat Nov 29, 2014 1:09 am   Post subject: RE:How to find median, minumum and maximum with random integers?

half of 2 is 2/2 = 1
half of 4 is 4/2 = 2
half of 8 is 8/2 = 4
half of N is N/2

You'd need to do something special for even vs. odd lengths of lists, but you've already figured that out in the example that you have.

Some useful functions are floor, round, and ceil, which let you round numbers into integers in various ways (follow links for documentation).
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
ylcc23




PostPosted: Tue Dec 02, 2014 2:00 pm   Post subject: RE:How to find median, minumum and maximum with random integers?

Thanks for the help Tony. I managed to figure it out using a different technique. I created two more variables to make it a bit easier as well as to avoid having to do a large amount of math in brackets all in one line.

Turing:

var student : int
var mark : array 0 .. 100 of int
var temp, Min, Max, p1, p2 : int
var mean, median : real

randint (student, 30, 75)
put "There are ", student, " students in the class"

for i : 1 .. student
    put "Enter a mark"
    get mark (i)
    if mark (i) > 100 or mark (i) < 0 then
        cls
        put "Please enter a mark between 0 and 100"
        delay (1000)
        cls
    end if
end for
for i : 1 .. student
    put mark (i) : 5 ..
end for
put ""
for i : 1 .. student
    for j : 1 .. student - 1
        if mark (j) > mark (j + 1) then
            temp := mark (j)
            mark (j) := mark (j + 1)
            mark (j + 1) := temp
        end if
    end for
end for
for i : 1 .. student
    put mark (i) : 5 ..
end for
put ""
for i : 1 .. student
    for j : student .. student - 1
        mean := (mark (j) + mark (j + 1) + temp) / student
    end for
end for
put "The mean is ", mean
put ""
if student mod 2 = 0 then
    p1 := student div 2
    p2 := p1 + 1
    median := (mark (p1) + mark (p2)) / 2
elsif student mod 2 = 1 then
    p1 := student div 2 + 1
    median := mark (p1)
end if
put "The median is ", median
put ""
Min := mark (1)
Max := mark (student)
put Min
put ""
put Max
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  [ 11 Posts ]
Jump to:   


Style:  
Search: