Turing Help for a beginner
Author |
Message |
SIXAXIS
|
Posted: 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? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
fishtastic
![](http://compsci.ca/v3/uploads/user_avatars/2112433479479e80d9034af.png)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
SIXAXIS
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
syntax_error
![](http://compsci.ca/v3/uploads/user_avatars/196798963948cf16794b6e5.jpg)
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
fishtastic
![](http://compsci.ca/v3/uploads/user_avatars/2112433479479e80d9034af.png)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
ericfourfour
|
Posted: 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 ) |
|
|
|
|
|
![](images/spacer.gif) |
HeavenAgain
![](http://compsci.ca/v3/uploads/user_avatars/139122102045e603120b143.jpg)
|
Posted: Wed Jan 30, 2008 5:44 pm Post subject: RE:Turing Help for a beginner |
|
|
shorter doesnt mean better
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
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 Rolling Eyes](images/smiles/icon_rolleyes.gif) |
|
|
|
|
![](images/spacer.gif) |
fishtastic
![](http://compsci.ca/v3/uploads/user_avatars/2112433479479e80d9034af.png)
|
Posted: 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 Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
ericfourfour
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
|
|