Alice Community  

Go Back   Alice Community > Alice 2 > How do I...?

Reply
 
Thread Tools Display Modes
counting
Old
bopgaard
Junior Member
 
Status: Offline
Posts: 5
Join Date: May 2012
Default counting - 05-10-2012, 09:13 PM

Please excuse my limited knowledge with Alice, I just started a introduction course and feel very overwhelmed with the algorithms.

On one my my assignments a questions asks

The following algorithm was intended to result in the numbers 10 to 1 being printed. What will it actually do? What is wrong with it, and how can it be corrected?

BEGIN
COUNT = 10
While count > 0
{
Print count
Count = Count +1
}
Print "The countdown is finished."
END




I get the basic idea of sequence I just have a hard time understanding what the =, > { symbols mean is this algorithm.

any help would be really appreciated.
   
Reply With Quote
Old
TauTrumpsPi
Senior Member
 
TauTrumpsPi's Avatar
 
Status: Offline
Posts: 399
Join Date: Mar 2011
Location: The diamond layer
Default 05-10-2012, 09:28 PM

Well, I can tell you what the symbols mean, but you'll have to figure out what's wrong with it, although if you need a hint I'll be willing:

1. The "=", is referred to as the assignment operator in Java programming. It reassigns a new value to a variable. So "COUNT = COUNT + 1" would take the original value of count, add one, and then make that new value the new value of count. So, in simple terms, if COUNT was originally 1, the new value of COUNT would be 2

2. The ">" is "more than". So the loop reads, in english: While COUNT is more than 0. So, the command inside the loop will read until count is no longer > 0.

3. The "{" and "}" symbols are the open and close symbols for the loop. So if the command was

Code:
loop 5 times
{
print "hi"
}
"hi" would be printed 5 times. However, in this case you have a while loop, so the loop will execute until count is no longer more than 0.


Hope this helps, feel free to ask if you're still having trouble.


--------------------------------------------------------------------------------------------------------------------------
I have plenty of common sense. I just choose to ignore it.
-Calvin and Hobbes

Wondering about my Avatar?
http://en.wikipedia.org/wiki/Tardigrade

Wondering about my username?
http://www.youtube.com/watch?v=jG7vhMMXagQ
   
Reply With Quote
Old
bopgaard
Junior Member
 
Status: Offline
Posts: 5
Join Date: May 2012
Default 05-10-2012, 09:40 PM

thank you for your help.

BEGIN
COUNT = 10
While count > 0
{
Print count
Count = Count +1
}
Print "The countdown is finished."
END

I could be wrong but I think it should read as follows.

BEGIN
COUNT = 10
While count > 1
{
Print count
Count = Count +1
}
Print "The countdown is finished."
END


I changed the while count > 0
to a 1 so it would stop at 1 instead of 0.

hope I'm on the right track.

thank you for responding.
   
Reply With Quote
Old
TauTrumpsPi
Senior Member
 
TauTrumpsPi's Avatar
 
Status: Offline
Posts: 399
Join Date: Mar 2011
Location: The diamond layer
Default 05-11-2012, 08:10 AM

Quote:
Originally Posted by bopgaard View Post
thank you for your help.

BEGIN
COUNT = 10
While count > 0
{
Print count
Count = Count +1
}
Print "The countdown is finished."
END

I could be wrong but I think it should read as follows.

BEGIN
COUNT = 10
While count > 1
{
Print count
Count = Count +1
}
Print "The countdown is finished."
END


I changed the while count > 0
to a 1 so it would stop at 1 instead of 0.

hope I'm on the right track.

thank you for responding.
Nope, you need to keep it at 0. When the loop starts, it check what it is at, and if it is at one, which is not more than one, it will not display the loop. Therefore, you need to keep it at 0.

Your real problem lies elsewhere, inside the loop. Think about the logic involved with the loop, inside the loop, and your statement that updates the value of the count variable.


--------------------------------------------------------------------------------------------------------------------------
I have plenty of common sense. I just choose to ignore it.
-Calvin and Hobbes

Wondering about my Avatar?
http://en.wikipedia.org/wiki/Tardigrade

Wondering about my username?
http://www.youtube.com/watch?v=jG7vhMMXagQ
   
Reply With Quote
Old
bopgaard
Junior Member
 
Status: Offline
Posts: 5
Join Date: May 2012
Default 05-11-2012, 08:42 PM

I'm lost on this one...for now I'm going to revisit the textbook. ill check back if I come up with a solution. thanks for your help buddy.
   
Reply With Quote
Old
TauTrumpsPi
Senior Member
 
TauTrumpsPi's Avatar
 
Status: Offline
Posts: 399
Join Date: Mar 2011
Location: The diamond layer
Default 05-12-2012, 11:09 AM

If you do come back and you still need another hint, notice that the update statement, the one that changes the value of the variable within the loop, adds to the value of the variable.

Then notice that your loop executes when the value of the variable is larger than 0. Will the loop ever terminate while this is still the case?


--------------------------------------------------------------------------------------------------------------------------
I have plenty of common sense. I just choose to ignore it.
-Calvin and Hobbes

Wondering about my Avatar?
http://en.wikipedia.org/wiki/Tardigrade

Wondering about my username?
http://www.youtube.com/watch?v=jG7vhMMXagQ
   
Reply With Quote
Old
bopgaard
Junior Member
 
Status: Offline
Posts: 5
Join Date: May 2012
Default 05-16-2012, 10:02 PM

i was able to solve this problem thanks to your help. thank you for the guidance.
   
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Copyright ©2020, Carnegie Mellon University
Alice 2.x 1999-2012, Alice 3.x 2008-2012, Carnegie Mellon University. All rights reserved.