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

Username:   Password: 
 RegisterRegister   
 Turing Help for a beginner
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
SIXAXIS




PostPosted: Wed Jan 30, 2008 11:17 am   Post subject: 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:

code:
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?
Sponsor
Sponsor
Sponsor
sponsor
fishtastic




PostPosted: Wed Jan 30, 2008 11:38 am   Post subject: Re: Turing Help for a beginner

this works.
Turing:

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




PostPosted: Wed Jan 30, 2008 11:45 am   Post subject: 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




PostPosted: Wed Jan 30, 2008 2:05 pm   Post subject: 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




PostPosted: Wed Jan 30, 2008 2:55 pm   Post subject: Re: Turing Help for a beginner

syntax_error @ Wed Jan 30, 2008 1:05 pm wrote:

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




PostPosted: Wed Jan 30, 2008 5:30 pm   Post subject: Re: Turing Help for a beginner

Just a small improvement on this part:
Turing:
if age > old then
    old := age
end if
if age < young then
    young := age
end if


It can be reduced to:
Turing:
age := max (min (age, old), young)


It's a shorter way to keep numbers within a range. It works like this:
Turing:
value := max (min (value, upperBound), lowerBound)
HeavenAgain




PostPosted: Wed Jan 30, 2008 5:44 pm   Post subject: RE:Turing Help for a beginner

shorter doesnt mean better Shocked
your method with
code:
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 Razz
though it can be shorten to
code:
young := min (young, age)
old := max (old, age)
or
code:
if age > old then
    old := age
%% instead of 2 if, we can use elsif
elsif age < young then
    young := age
end if
Rolling Eyes
fishtastic




PostPosted: Wed Jan 30, 2008 5:55 pm   Post subject: Re: RE:Turing Help for a beginner

code:
age := max (min (age, old), young)

ericfourfour this doesn't work.
look at the situation.

code:
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
code:

young := min (young, age)
old := max (old, age)

this does work! and its shorter! Smile
Sponsor
Sponsor
Sponsor
sponsor
ericfourfour




PostPosted: Wed Jan 30, 2008 6:34 pm   Post subject: 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.
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  [ 9 Posts ]
Jump to:   


Style:  
Search: