A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

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?

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  6. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  7. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  8. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

  9. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  10. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  11. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  12. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  13. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  14. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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)

  15. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  16. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  17. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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!

  18. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  19. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  20. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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)

  21. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

  22. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yes exactly. Have it print the odd ones.

  23. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

  24. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  25. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    this forum? or the main openstudy one?

  26. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    this forum, i suppose.

  27. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  28. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  29. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  30. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  31. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  32. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.