u/Glory_Beckons - 3 Archived Voat Posts in v/programming
u/Glory_Beckons
  • home
  • search

u/Glory_Beckons

0 posts · 3 comments · 3 total

Active in: v/programming (3)

  • ‹‹‹
  • ‹‹
  • ‹
  • 1
  • ›
  • ››
  • ›››
Comment on: I could use a bit of help with an assignment for class.

I find it difficult to get into someone else's head quite often, so following the logic of why the writers wrote this the way they did is tough for me

It's tough for everyone, as you can see by my initial mistake. It's often down to a keyword or two. That's the trouble with examples like this. They tend to be odd and intuitive, and have multiple solutions even though the authors want only a very specific one. It's just how it is.

Why a negative value to exit rather than an exit button...

If you're just starting, you probably won't be dealing with GUI applications any time soon.

... or even an exit command if this were a command line program?

How would you implement an exit command? Type in "exit" instead of a test score?

The test scores are declared as numbers. They won't accept "exit" as an input. Once you encounter an "input testx", the user must input a number to continue. -1 can never be a valid test score, so it's a convenient "get me out of here". You'll encounter -1 being used to indicate "not valid", "error", "exit" all over the place. It's a very common convention.

You could have an additional "Do you want to continue (yes/no):" input as part of the loop. But that would involve more code that has nothing to do with what this example is about and get annoying if you want to input 30 test scores.

0 04 Feb 2018 16:44 u/Glory_Beckons in v/programming
Comment on: I could use a bit of help with an assignment for class.

Actually, scratch that... I should have paid more attention to the comment at the top. ("for any number of students ... until user enters negative value for first test score")

This is what they want it to do:


start:
output "Enter score for test 1 or negative number to quit"
input test1
if test1 < 0: goto end
output "Enter score for test 2"
input test2
output "Enter score for test 3"
input test3
average = (test1 + test2 + test3) / 3
output "Average is ", average
goto start
end:
output "End of program"

This is a bit different. Ignore 2, 4, 5 above. 1 and 3 still apply.

  1. The input of test1 is what they called "housekeeping". Except where is the input?
  2. End is still fine
  3. The big block is what they called mainloop (I wouldn't worry about the repetition, and I assume you haven't covered arrays yet)
  4. The tricky bit is the loop condition. You need "housekeeping" inside the loop, but you cannot check test1 until after it has run.

Have you covered break/continue? Or "do while" loops as opposed to while loops?

0 04 Feb 2018 07:36 u/Glory_Beckons in v/programming
Comment on: I could use a bit of help with an assignment for class.

If we were to unroll the logic, here is what you want the program to do:

output "Enter score for test 1 or negative number to quit"
input test1
if test1 < 0: goto end
output "Enter score for test 2 or negative number to quit"
input test2
if test2 < 0: goto end
output "Enter score for test 3 or negative number to quit"
input test3
if test3 < 0: goto end
average = (test1 + test2 + test3) / 3
output "Average is ", average
end:
output "End of program"

Obviously there is much repetition here, not to mention the goto's, which is why we want to structure it better. But this is what you want your program to achieve, and (usually mentally) laying it out in a linear fashion like this is very useful for debugging (make a habit of it).

  1. Is this what your program does? Go through line by line and look for discrepancies.
  2. Obviously the repeated blocks should be looped, the goto condition becoming the loop condition. Compare to your current loop condition.
  3. Note that the loop condition cannot be checked before the corresponding input, it has to be after.
  4. Note we have to skip the average calculation and output if we aborted.
  5. Have you covered arrays/lists? That would help here considerably.
0 04 Feb 2018 07:06 u/Glory_Beckons in v/programming
  • ‹‹‹
  • ‹‹
  • ‹
  • 1
  • ›
  • ››
  • ›››

archive has 9,592 posts and 65,719 comments. source code.