
-----------------------------------
SIXAXIS
Wed Jan 30, 2008 11:17 am

Turing Help for a beginner
-----------------------------------
Hi guys,

I'm new here and I'm also new to Turing. I have to make a program that calculates the average, youngest, and oldest age in a class that's size is determined by the user. Here is what I have so far:

var age, age1, age2, n : int
put "How many students are in your class?"
get n
age1 := 0
age2 := 0
cls
for i : 1 .. n
    put "What is your age?"
    get age
    if age > age1 then
        age := age1
    elsif age < age1 then
        age := age2
    end if
    cls
end for
cls
put "The youngest person is ", age2, ", the oldest person is ", age1, ", and the average age is ", round ((age1 + age2) / 2), "."

Whenever I run this, it works fine, but at the end, it says that the youngest, oldest, and average age is 0. What am I doing wrong? Is it the if statements?

-----------------------------------
fishtastic
Wed Jan 30, 2008 11:38 am

Re: Turing Help for a beginner
-----------------------------------
this works.

var total, age, young, old, n : int
put "How many students are in your class?"
get n
total := 0
old := 0
young := maxint

for i : 1 .. n
    put "What is your age?"
    get age
    total += age
    if age > old then
        old := age
    end if
    if age < young then
        young := age
    end if
    cls
end for

put "The youngest person is ", young, ", the oldest person is ", old, ", and the average age is ", total / n : 2, "."


This is really simple so you should get this.

also.
use better variable names please.

-----------------------------------
SIXAXIS
Wed Jan 30, 2008 11:45 am

Re: Turing Help for a beginner
-----------------------------------
Thank you for your help. I understand it now. And yeah, after I posted this I changed the names in my Turing file, but didn't re-post it.

-----------------------------------
syntax_error
Wed Jan 30, 2008 2:05 pm

Re: Turing Help for a beginner
-----------------------------------
yes thank fishtastic since he/she just did your work for you 
how graces of him/her 
make sure you do understand the differnce in what he/she did for not always will someone here do the work for you

btw fishtastic dont mind my satire here but next time just explain them what they need to do NOT do it for them

-----------------------------------
fishtastic
Wed Jan 30, 2008 2:55 pm

Re: Turing Help for a beginner
-----------------------------------

btw fishtastic dont mind my satire here but next time just explain them what they need to do NOT do it for them
Yes.. perhaps I should use pseudo code instead.  So people will try to understand it more.

But, it was easier just to fix the problem and show the difference than explaning, becasue the problem already gave you the logic you need to solve it.

-----------------------------------
ericfourfour
Wed Jan 30, 2008 5:30 pm

Re: Turing Help for a beginner
-----------------------------------
Just a small improvement on this part:
if age > old then
    old := age
end if
if age < young then
    young := age
end if

It can be reduced to:
age := max (min (age, old), young)

It's a shorter way to keep numbers within a range. It works like this:
value := max (min (value, upperBound), lowerBound)

-----------------------------------
HeavenAgain
Wed Jan 30, 2008 5:44 pm

RE:Turing Help for a beginner
-----------------------------------
shorter doesnt mean better :shock:
your method with age := max (min (age, old), young) works, but how do you know whats old (upperBound) and whats young (lowerBound)? the problem asked to find the oldest and youngest in a give N. so with that you just lost whats old and whats young, because old and young is not given, therefore you gotta keep track of it :P
though it can be shorten to 
young := min (young, age)
old := max (old, age)or
if age > old then
    old := age
%% instead of 2 if, we can use elsif
elsif age < young then
    young := age
end if:roll:

-----------------------------------
fishtastic
Wed Jan 30, 2008 5:55 pm

Re: RE:Turing Help for a beginner
-----------------------------------
age := max (min (age, old), young) 
ericfourfour this doesn't work.
look at the situation.

if age > old then
    old := age
%% instead of 2 if, we can use elsif
elsif age < young then
    young := age
end if
HeavenAgain this doesn't always work either.
there are chance where input is the oldest and the youngest but you only checked one that way.

however

young := min (young, age)
old := max (old, age)
this does work! and its shorter! :)

-----------------------------------
ericfourfour
Wed Jan 30, 2008 6:34 pm

RE:Turing Help for a beginner
-----------------------------------
Woops. Misread his code. I read it like he was trying to keep the age within a range. The example fishtastic just concluded with is the better solution.
