tags:

views:

149

answers:

4
% calculates the population of a city from year 2000 to 2020

var popstart : int := 80000
var popgrowth : real
var popend : real
var growthrate : real := 0.03

% popgrowth := popstart * growthrate
    for i : 2000..2020 by 1
popgrowth := popstart * growthrate
end for

put "year  population"
put "====  =========="
put  i, "  ", popgrowth

when I run the program, i get the error variable "i has not been declared" when I declare i as a variable, I get the error "i has already been declared"

the output should look something like this:

year population
==== ==========
2000 xxxxxxxxxx ~ 2020 XXXXXXXXXX

here's a similar, but much simpler program, where i was successful in doing what i'm attempting to do in the program above.

for i : 4 .. 19 by 3
    put i
end for

HELP PLEASE! and thanks in advance!

+1  A: 

You have to declare i. i is a variable that starts from 2000 and goes up to 2020.

var i: int should do the trick.

masfenix
thanks masfenix, but i tried that.i got the error "i has already been declared"it seems i have have stumbled upon some sort of programing paradox.
starbuck
Maybe its the fact that theres a space between the "two dots" in the number range. man turing brings back memories from highschool lol. 5 - 6 years ago.
masfenix
A: 

I don't know much about Turing, but I suspect that the for i... is an implicit declaration of i.

So, I don't know how you could fix it, but you could get around it by doing this:

var last_year: int
for i : 2000..2020 by 1
    popgrowth := popstart * growthrate
    last_year = i
end for

put "year  population"
put "====  =========="
put  last_year, "  ", popgrowth
David Wolever
+1  A: 

Another answer, knowing even less about Turing.... I'm guessing that i is only in scope during the loop since that is where it is declared, so then it is throwing the error when you try to use i outside the loop at the end. Which explains why the smaller program you posted works but the larger one doesn't. And also why it would throw an error when you try to declare i first outside the loop and then declare it again in the loop statement.

I believe that is the same thing that Wolever was getting at, but didn't really explain why. (His answer should fix it if we're right about i only being valid in the loop)

Goog
that hadn't occurred to me, but it appears your both right. now to figure out whats wrong with my math...
starbuck
A: 
% calculates population growth for city of Whitby between 2000 and 2020.

var popstart : int := 80000
var popgrowth : real
var growthrate : real := 0.03

put "year  population"
put "====  =========="
put "2000  80000"

popgrowth := popstart
for i : 2001 .. 2020 by 1
    popgrowth := popgrowth + (popgrowth * growthrate)
    put i, "  ", popgrowth:0:2
end for
starbuck