Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Distinguish between positive and negative integers in loop?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jinjin




PostPosted: 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:

code:

var c, avg : real
var num, ans : int
put "Please enter a series of positive or negative integers."
ans := 0
c := 1
loop
    get num
    ans := ans + num
    exit when c = 10 or num = 0
    c := c + 1
end loop

if num not= 0 then
    avg := ans / c
elsif num = 0 then
    avg := ans / (c - 1)
end if
if num not= 0 then
    c := c
elsif num = 0 then
    c := (c - 1)
end if

put "You entered ", c, " positive integers."
put "The total of the positive integers is ", ans, "."
put "The average of the positive integers is ", avg, "."
put "You entered ", c, " negative integers."
put "The total of the negative integers is ", ans, "."
put "The average of the negative integers is ", avg, "."


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 Very Happy
Sponsor
Sponsor
Sponsor
sponsor
Mackie




PostPosted: 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.
jinjin




PostPosted: 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 Very Happy The problem was I didn't initialize one of my variables (stupid mistakes always kill me) and one knowledgable person at my school showed me that I had placed my 'exit when' statement in the wrong place if I wanted to exit if 0 was the first entry. Now, all the problems are solved.

Also, sorry for the late reply. I became preoccupied with another assignment (not compsci-related) that had to be handed in.

Thanks,
jinjin
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: