# Simple Programming Query

• ### Question

• I have recently started using small basic in my IT course. I've been asked to create a program;

Input an unknown number of integers.  Output the total and the average.  Terminate the series of numbers with -999 (this is not to be included in the calculations).

I would be grateful if you could post the code for this program. It is aimed to help us understand the 'while' statement.

Tuesday, September 27, 2011 6:23 PM

• Hi, you could use an array to store all the values input, perhaps something like:

numbers[Counter] = Num

set after Num is entered and Counter is updated.  You can the process the numbers at the end to calc total and average.

Alternatively, you could just keep the running total since this is all you need for the average and total.

total = total+Num

• Marked as answer by Tuesday, September 27, 2011 7:33 PM
Tuesday, September 27, 2011 7:19 PM

### All replies

• Its great you are using SmallBasic, but were not going to do your homework for you.  Start by using the command TextWindow.ReadNumber and read up and try writing programs using While .. EndWhile.

When you have some code and any questions related to it, ask again.

Tuesday, September 27, 2011 6:34 PM
• Thanks for getting back so quickly!

I've gotten as far as getting it to read multiple numbers and keeping a counter of how many numbers are imputed so I can use the final counter number to calculate the average. This is how it reads so far;

TextWindow.WriteLine("Please enter a number ")

Counter=1

TextWindow.WriteLine("counter = " + counter)

While(Num>-999)

TextWindow.WriteLine("Please enter another number")

Counter=Counter+1

TextWindow.WriteLine("counter = " + Counter)

EndWhile

I can't work out how to keep a record of the different variables of 'num' so I can add them at the end or if I'm on the wrong track altogether??

Tuesday, September 27, 2011 7:06 PM
• Hi, you could use an array to store all the values input, perhaps something like:

numbers[Counter] = Num

set after Num is entered and Counter is updated.  You can the process the numbers at the end to calc total and average.

Alternatively, you could just keep the running total since this is all you need for the average and total.

total = total+Num

• Marked as answer by Tuesday, September 27, 2011 7:33 PM
Tuesday, September 27, 2011 7:19 PM
• Thank you very much. I don't know why I didn't think of that keeping a running total! (we haven't got as far as arrays yet)

There is only one more thing, how do I get it to ignore the final value (-999)

• Edited by Tuesday, September 27, 2011 7:46 PM
Tuesday, September 27, 2011 7:33 PM
• To ignore the -999, you could do an If (Num <> -999) to isolate adding the value, but it is tidier (better) to change the order of the readnumber and setting of the value in the While loop i.e. do the read at the end so if it is -999 the loop ends before the number is added - note this means we don't set the first value before the While loop, also use Num <> -999, just in case -1000 is a valid number entry!

Just work through in your head what is being set when, perhaps the following:

Counter=0
total=0
TextWindow.WriteLine("Please enter a number ")