Computer Science Canada Distinguish between positive and negative integers in loop? |
Author: | jinjin [ Tue Mar 18, 2008 1:10 am ] | ||
Post subject: | Distinguish between positive and negative integers in loop? | ||
Ok, this question may be a little bit hard to explain so please bear with me. I'm in Gr. 10 Programming and we are doing loops in Turing right now. The program I have to make is one where it should ask the user for a series of positive or negative integers. The loop exits when 10 integers have been entered or the user enters a zero. The output should print the number of positive integers entered, the sum of the positive integers, the average of the positive integers, the number of negative integers entered, the sum of the negative integers, and the average of the negative integers. Also, when a 0 is entered, the program should NOT count that as an integer or include it in average calculations. Here's what I have so far:
This program can currently only count, total, and average the integers correctly and it is also able to exclude 0 from integer counts and averages. However, it cannot determine the difference between positive and negative integers. (i.e. in the "you entered ... integers" part of it, it only totals all the integers entered for both positive and negative but it cannot distinguish between the two). Another problem with it is that it crashes when 0 is the first entry because Turing cannot divide by 0 (as in the average part of it). I honestly have no idea how to fix these problems. My only guess is that an if statement like the one I used to check if the number was 0 would also be used to check if it is positive or negative but then even if I was to do that, I do not know what variables I should enter from there. I'm sorry if this explanation is too confusing; I tried explaining it as clearly as possible. If anyone can provide any help on how to distinguish between the positive and negative integers and/or help with the first entry being 0 issue, I would be EXTREMELY appreciative because I have gone insane trying to fix these issues. Thanks ![]() |
Author: | Mackie [ Tue Mar 18, 2008 2:14 am ] |
Post subject: | RE:Distinguish between positive and negative integers in loop? |
Ok, well you know about the > and < operators. So, what can you say about and a negative number? There usually (actually always) less than zero. And a positive number is always greater than zero. Now with if statements. and the > and < signs you can figure out if a number is positive of negative. |
Author: | jinjin [ Fri Mar 21, 2008 10:41 pm ] |
Post subject: | Re: Distinguish between positive and negative integers in loop? |
Mackie wrote: Ok, well you know about the > and < operators. So, what can you say about and a negative number? There usually (actually always) less than zero. And a positive number is always greater than zero. Now with if statements. and the > and < signs you can figure out if a number is positive of negative. Thanks for the advice Mackie, I think I got it now ![]() Also, sorry for the late reply. I became preoccupied with another assignment (not compsci-related) that had to be handed in. Thanks, jinjin |