Computer Science Canada

finding sum of all numbers until an eof of -99 is entered?

Author:  baykko [ Mon Sep 15, 2008 7:18 pm ]
Post subject:  finding sum of all numbers until an eof of -99 is entered?

i have kind of forgotten my basic turing knowledge
i am required to create a program where you can enter an unlimited amount of numbers, and it will end once you enter -99 and it will find the sum of all the numbers that you entered
i only know how to do this for a set number of entries, but how would I be able to keep entering an unlimited amount of numbers with no limit?

Author:  Insectoid [ Mon Sep 15, 2008 7:53 pm ]
Post subject:  RE:finding sum of all numbers until an eof of -99 is entered?

You would have a loop, with an exit when num= -99, then get a number from the user and add it to a variable 'total'.

Author:  baykko [ Mon Sep 15, 2008 8:04 pm ]
Post subject:  Re: finding sum of all numbers until an eof of -99 is entered?

how can i make it so when i enter the eof of -99, it isnt added to my total sum? this is what ive done so far
code:

    var numb, totalsum : int := 0
    loop
        get numb
        totalsum := totalsum + numb
        exit when numb = -99
    end loop
    put totalsum

Author:  Insectoid [ Mon Sep 15, 2008 8:09 pm ]
Post subject:  RE:finding sum of all numbers until an eof of -99 is entered?

you do the exit when at the top of the loop, the first thing

Author:  baykko [ Mon Sep 15, 2008 8:14 pm ]
Post subject:  Re: finding sum of all numbers until an eof of -99 is entered?

I moved the exit in between get numb and totalsum:= and it works properly now
thanks guys

Author:  Insectoid [ Mon Sep 15, 2008 8:16 pm ]
Post subject:  RE:finding sum of all numbers until an eof of -99 is entered?

no, no, no, put it at the VERY TOP of your loop. This is important as you get into larger programs.

Author:  baykko [ Mon Sep 15, 2008 8:18 pm ]
Post subject:  Re: finding sum of all numbers until an eof of -99 is entered?

when i put it at the very top of the loop, the -99 gets added to my totalsum

Author:  gitoxa [ Mon Sep 15, 2008 8:19 pm ]
Post subject:  Re: RE:finding sum of all numbers until an eof of -99 is entered?

insectoid @ Mon Sep 15, 2008 8:16 pm wrote:
no, no, no, put it at the VERY TOP of your loop. This is important as you get into larger programs.


He's got it in the right place. There isn't a set place for an exit to go, it's just wherever it'll be most efficient. In this case, it's where he's moved it to.


: