Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 6 Online
OpenStudy (anonymous):

problem set 1: polpak suggested i write a program that prints integers from 1 to 100. i wrote one that computes up to the value 100 through an iterative loop but i don't know how to actually print each integer from 1 through 100. here's a link to the program. http://dpaste.com/490649/ how do i print the list of integers?

OpenStudy (anonymous):

replying to my own post here! i just saw that polpak suggested i simply count to 100. which this program accomplished. . .

OpenStudy (anonymous):

True. Ok, so now make it print each in turn.

OpenStudy (anonymous):

i don't know how to do that. eek! i hate asking for you to do this for me but i am unfamiliar with how to print each value.

OpenStudy (anonymous):

that's ok. Let me ask you this, what would happen if you removed line 5 from the program?

OpenStudy (anonymous):

not sure. . .i'll go remove it and see what happens.

OpenStudy (anonymous):

also, while you're doing that. Print out the value of itersLeft before and after the loop

OpenStudy (anonymous):

when i remove y=y+x, the print value returned is simply 1. if I print itersLeft before the loop, I get 99. if i print it after, i get 0.

OpenStudy (anonymous):

Ok, so itersLeft is 99 before you execute the loop so we can actually remove the y and the x variables entirely and just say itersLeft = 99. That might make things more clear. Now what happens when you print itersLeft inside the loop?

OpenStudy (anonymous):

hmmm. i put the statement 'print itersLeft' in the iterative statement and nothing happened.

OpenStudy (anonymous):

dpaste your current version of the code and I'll try to figure out what the problem is. It should certainly have done something.

OpenStudy (anonymous):

i just redid it. something had gone wrong the first time with my spacing. this time i got a list with values from 98 down to 0. i'm going to increase my itersLeft to 102-x and see what comes back.

OpenStudy (anonymous):

wait. You should have removed x and y. You only need to have itersLeft. The x and y variables were specific to the problem trying to be solved in the lecture. For standard iteration you only need 1 counter variable (or some other boolean condition) Just set itersLeft = 99 and see what happens without the x and y

OpenStudy (anonymous):

am i just removing y=y+x or am i completely removing y from the program?

OpenStudy (anonymous):

you don't need x or y at all. Where you initialize the itersLeft variable, you can just set it directly to 99 (the result of 100-x when x is 1)

OpenStudy (anonymous):

alright. now i've gotten a list from 99 to 0. here's the program: http://dpaste.com/490719/

OpenStudy (anonymous):

You don't need the x. It's not doing anything useful. See http://dpaste.com/490708/

OpenStudy (anonymous):

ah! makes sense. being so new to this, i still don't understand a lot of what i'm doing. so, i'm parroting what i see in lecture until the comprehension sets in!

OpenStudy (anonymous):

no problem. I just wanted to simplify the example so you have an easier time understanding how it works. Now, you said that you had a list of numbers from 99 to 0. Techically your version goes from 98 to 0, while mine goes from 99 to 1. See if you can modify my version so that it only goes from 9 to 1.

OpenStudy (anonymous):

Also I wanted to clarify something you said. This code does not 'give' a list of numbers. To be correct we can simply say that it will loop 99 times. Each time it loops it will print one number (the current number stored in itersLeft) then change the current number to be the next number.

OpenStudy (anonymous):

Or even more correctly. It will loop until itersLeft is NOT greater than 0. And each time it executes the body of the loop we move itersLeft closer to that end case. (itersLeft is less than or equal to 0)

OpenStudy (anonymous):

i modified the program so it prints from 9 down to 1. i see how the print statement needs to come before the operation on itersLeft in order to start the list at the current value of itersLeft. i don't have a ton of time to continue working on this so i will need to take a break for now. my next step then will be to have the program only print odd numbers?

OpenStudy (anonymous):

yes exactly. Have it print the odd ones.

OpenStudy (anonymous):

ok. i'll work on that later today or tomorrow and then come back for help if i need it. are you on the open study forum often?

OpenStudy (anonymous):

as a hint, think about how far the next odd number is from the current odd number

OpenStudy (anonymous):

this forum? or the main openstudy one?

OpenStudy (anonymous):

this forum, i suppose.

OpenStudy (anonymous):

I'm here often. I don't use the regular openstudy thing as much.

OpenStudy (anonymous):

ok, great! that's really nice of you to hang out in an intro forum in order to help us newbies.

OpenStudy (anonymous):

I also tutor elsewhere, and I'm hoping to someday teach, so this is good practice =)

OpenStudy (anonymous):

well you're off to a good start helping me so i really appreciate it! and, i hope that if you get a job teaching basic CS, it pays well so you aren't scratching your eye balls out over all the dumb questions for nothing.

OpenStudy (anonymous):

It's actually pretty rewarding by itself. I've done things that pay well, and they weren't nearly as much fun.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!