
-----------------------------------
Misha
Fri Oct 01, 2004 1:26 pm

help with sum and average calculator
-----------------------------------
hi, i'm new to turing and my teacher assigned us to make a sum and average calculator. it should ask the user to input how many numbers they want and then it is supposed to add them all together and as well give the average of all those numbers. so far this is what i have

 [code]
%------------------------------------------------------------------
% sum and average
% Date: Oct 1 2004
%------------------------------------------------------------------

% Declare variables

var num,sum,avg : int                   % user's number
var cont : string               % does user want to continue or not
var count : int

loop
cls
put " you could quit any time by pressing q or Q"
        put "please enter a number"
        get num
        sum + = num
        count += 1 
exit when cont = "Q" or cont = "q"
end loop

avg :=sum
put "the sum of all the number is"
put sum + = num
put "the avg of all the numbers is"
put sum / 4
% End of program

[/code]

thanks

-----------------------------------
wtd
Fri Oct 01, 2004 1:34 pm


-----------------------------------
Looks good, but a few suggestions:

var count : int

You never prompt the user for how many numbers they want to input.

exit when cont = "Q" or cont = "q"

You never actually "get cont" in the loop.  :)

avg := sum

You never use the "avg" variable again, and you don't need to, so eliminate this as unnecessary.

put sum / 4

Here you dvide by a hardcoded number, even though the user may have input more or less than 4 numbers.  

put sum / count

-----------------------------------
josh
Fri Oct 01, 2004 1:37 pm


-----------------------------------
darn you beat me to posting.

Misha if I remeber correctly Miss Pooley said that we didn't have to ask the user for the amount of numbers to input. It has to go untill the user wants to quit.

-----------------------------------
Misha
Fri Oct 01, 2004 1:38 pm


-----------------------------------
thanks for the help, i will give it a try

-----------------------------------
Delos
Fri Oct 01, 2004 1:40 pm


-----------------------------------
Okiez....

1)
Press F2 please, this will format everything nicely for you.
Thank you for using trying to 'put' in this case was the raw value of the variable...no need to include another assignment with that.

4)
Exits
You've tried to include an exit condition, but it won't work as it is dependant on a static variable (a variable whose value never changes).  In this case that is 'cont'.  Instead of a string-based exit, try an integer based input - like "Enter -3 to exit" or soemthing like that.

5)
Averaging.
You've got a counter in there, make use of it.  The User will not always enter '4' numbers.


Meh!  Beat to posting by 3 people!
