Computer Science Canada

help with sum and average calculator

Author:  Misha [ Fri Oct 01, 2004 1:26 pm ]
Post subject:  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



thanks

Author:  wtd [ Fri Oct 01, 2004 1:34 pm ]
Post subject: 

Looks good, but a few suggestions:

code:
var count : int


You never prompt the user for how many numbers they want to input.

code:
exit when cont = "Q" or cont = "q"


You never actually "get cont" in the loop. Smile

code:
avg := sum


You never use the "avg" variable again, and you don't need to, so eliminate this as unnecessary.

code:
put sum / 4


Here you dvide by a hardcoded number, even though the user may have input more or less than 4 numbers.

code:
put sum / count

Author:  josh [ Fri Oct 01, 2004 1:37 pm ]
Post subject: 

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.

Author:  Misha [ Fri Oct 01, 2004 1:38 pm ]
Post subject: 

thanks for the help, i will give it a try

Author:  Delos [ Fri Oct 01, 2004 1:40 pm ]
Post subject: 

Okiez....

1)
Press F2 please, this will format everything nicely for you.
Thank you for using [code] tags though, very few n00bs do, there is hope for you yet.

2)
Initialization.
If you have any variables that will be acting as counters of accumulators (like a number that will increase each time another number is inputed) then you need to have an initial value to start with. If you're given a number, say 3, and you want to add it to a variable, say 'numCount', then numCount would have to have a value before 3 is added to it! Or you'd be trying to add 3 to a piece of empty space...not going to happen.

3)
Syntax
[code]
put sum += num
[/code]
Either you made a mistake, or you don't know what the commands actually do. I'm going to go with the latter as if you had run the proggie you'd have picked up on this one.
The '+=' notation is OOT shorthand for
[code]
variableName := variableName + otherVariable
[/code]
In other words, it is an assignment. You cannot 'put' an assignment...as the computation is not an output one...it's done up in RAM.
What you are 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!


: