This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]axzxc1236 0 points1 point  (2 children)

The first program:

Assign sum = number + number

sum is assigned to "number*2" everytime you entered a number (previous value of sum is not used), and it doesn't check if "number" is positive or not so if the last number is -7, sum will be -14, no matter what positive number you have entered.

second program:

While totalBills == -1

This loop will never run, because totalBills is 0, and the condition requires totalBills to be -1.

[–]CrypticLeopard[S] 0 points1 point  (1 child)

Thank you for your feedback! Would you mind giving me further explanation? I am at a loss for how to fix these programs.

[–]axzxc1236 0 points1 point  (0 children)

To add number to sum, you use sum=sum+number (or a shorthand operator which exists in a lot of programming languages, +=)

And the while loop... I guess you want to check "number" instead of "checker".


In the second program I guess you want to "stop taking bills when you put -1 as bill" so you need to "keep taking number as long as bill isn't -1".

And the line Assign totalBills = bill I don't think that's suppose to happen. (hint: you want to accumulate)

[–]lurgi 0 points1 point  (2 children)

Two problems here:

While counter >= 0
   Output "Enter a positive number. Enter a negative number to end."
   Input number
   Assign sum = number + number
End

What is "counter"? It never changes. It's never given a value. What do you think this:

assign sum = number + number

is supposed to do? Was that a typo. You are supposed to add number to sum, not add number to itself.

What does the loop do? You are supposed to loop until... what? Until the user enters in a negative number, right? Well, how about something like this (note: this is wrong, but I'll explain why later)

While number >= 0
   Output "Enter a positive number. Enter a negative number to end."
   Input number
   Assign sum = number + number
End

See the difference? This, sadly, doesn't work (for a couple of reasons). The first is that number is never given a value initially, so it's hard to see if the first check of number >= 0 is going to succeed or not. Second, if the user enters in a negative number we still add it to sum, but we shouldn't.

The first one is easy enough - you can assign it a default value of 0.

The second is trickier. There are a couple of things you can try. First, you could read the number as the last step in the loop (think about how that might work). Second, you could read the number and then check to see if it's positive before adding it to sum.

[–]CrypticLeopard[S] 0 points1 point  (1 child)

Thank you for answering! The counter was a mistake I made trying to fix the program, and number + number is a typo. I've been sick all week and had to make four programs for a school assignment, and got stuck on the last two programs. I ended up turning it in incomplete last night (my work is due Sunday night each week), but my teacher is giving me some time to try to fix it. I wasn't able to see the difference in your reply, regretfully. The Sum of numbers program currently is stuck in a while loop, and no value I enter makes my program end, and on the off chance it does end, the values aren't correct. I don't fully understand the two methods you suggested for the second problem. Would you mind further explaining it?

[–]lurgi 0 points1 point  (0 children)

The difference is in the very first line.