
-----------------------------------
ruscutie
Thu Jan 25, 2007 11:04 pm

average help
-----------------------------------
hi, i have to take out the average (mean) of 20 numbers...soo first i have to ask the users to enter the numbers..


var num: array 1..20 of real 
var mean:real


for i: 1..20
put "Enter the 20 numbers"
get num(i)
end for



here is the code... the user can enter the numbers using the code but i can't think of  a method to find the average (mean)...plzz help

-----------------------------------
apomb
Thu Jan 25, 2007 11:13 pm

Re: average help
-----------------------------------
Simply add the numbers up, and divide by the number of numbers entered :/  this is basic, and i mean BASIC math... what grade ye be in?

-----------------------------------
ruscutie
Thu Jan 25, 2007 11:16 pm

RE:average help
-----------------------------------
i know... but that makez the program 2 long... i'm preparing 4 my exam and my teacher told us 2 use for i... but i'm stuck and can't think of how do u add them...

-----------------------------------
apomb
Thu Jan 25, 2007 11:39 pm

Re: average help
-----------------------------------
hmm ... adding a successive number of numbers? which are already in an array?  ... something about having a total coems to mind, however. :wink:

-----------------------------------
agnivohneb
Thu Jan 25, 2007 11:41 pm

Re: average help
-----------------------------------
You can try
var num : array 1 .. 20 of real
var mean : real := 0

for i : 1 .. 20
    put "Enter number ", i : 2, ": " ..
    get num (i)
    mean += num (i)
end for

mean := mean / upper (num)
put mean


-----------------------------------
apomb
Thu Jan 25, 2007 11:47 pm

Re: average help
-----------------------------------
damnit man ... dont just give the code away like that ... ruscutie was on the right track. im sure they could have figured it out eventually ... its people like you who make this website a haven for people who hijack any code they see and pass it off as their own.  please, respect the rules, and dont give in and just supply code . especially when its such an easy, yet important thing to learn, now, whoever asks for help is just going to use that code, and not know whats happening; the least you could have done was comment your code, so the person asking for help knows what the code is doing.  jeeze

-----------------------------------
ruscutie
Fri Jan 26, 2007 12:07 am

RE:average help
-----------------------------------
can some1 plz explain to me what is [upper]... it was used in the code above...?

-----------------------------------
agnivohneb
Fri Jan 26, 2007 12:20 am

RE:average help
-----------------------------------
Sry CompWiz333.

-----------------------------------
apomb
Fri Jan 26, 2007 12:20 am

Re: average help
-----------------------------------
that grabs the largest number in the array num, which, actually is incorrect, and should actually be changed, so that mean is divided by 20, not the largest number in the array. on a side note, Im concerned for your mark on this exam, ruscutie ...

and dont apologize to me, agnivohneb  though i do thank you for apologizing.

-----------------------------------
agnivohneb
Fri Jan 26, 2007 12:24 am

RE:average help
-----------------------------------
Upper is the same thing as saying 20.
in that code num is an array of 1 to 20. upper gets the highest number in the array (20) and uses it. there is also lower which dose the opposite.

-----------------------------------
apomb
Fri Jan 26, 2007 12:30 am

Re: average help
-----------------------------------
but thats not whats in the array, since the user is entering the numbers, it will pick the highest number entered, which is very unlikely to be 20.

-----------------------------------
ruscutie
Fri Jan 26, 2007 12:38 am

RE:average help
-----------------------------------
thnx guyz

-----------------------------------
apomb
Fri Jan 26, 2007 1:35 am

Re: average help
-----------------------------------
y'r wlcm

-----------------------------------
Bored
Fri Jan 26, 2007 12:32 pm

Re: average help
-----------------------------------
but thats not whats in the array, since the user is entering the numbers, it will pick the highest number entered, which is very unlikely to be 20.
compwiz, I'm afraid your wrong here. upper gets the upper bound in the array. That is not the largest number stored but the index of the upper element. For example:
var a : array 1..5 of int := init (1, 10, -5, 100, 7)
var b : array -3..1 of int := init (1, 10, -5, 100, 7)
var c : array 2..6 of int := init (1, 10, -5, 100, 7)
put "A: ", lower (a), " to ", upper (a) %Outputs "A: 1 to 5"
put "B: ", lower (b), " to ", upper (b) %Outputs "B: -3 to 1"
put "C: ", lower (c), " to ", upper (c) %Outputs "C: 2 to 6"
Notice how upper returns the upper bound of the array and not the highest value, and lower the lower bound and not the lowest value. Go ahead try it yourself.

-----------------------------------
BenLi
Fri Jan 26, 2007 1:26 pm

RE:average help
-----------------------------------
thats not wha he means. Take this for example array 10..20. There are only 10 elements in the array, yet the upper function would return 20. Because turing arrays do not start at a default value of 1 or 0, the upper function does not accurate reflect how many elements of the array there are

-----------------------------------
apomb
Fri Jan 26, 2007 3:21 pm

Re: average help
-----------------------------------
I dont program in Turing, im my understanding of
in that code num is an array of 1 to 20. upper gets the highest number in the array (20) and uses it. there is also lower which dose the opposite. 
was that it indeed got the highest number in the array.

Thank you for pointing out what upper actually does, Bored.

-----------------------------------
Bored
Fri Jan 26, 2007 4:32 pm

Re: RE:average help
-----------------------------------
thats not wha he means. Take this for example array 10..20. There are only 10 elements in the array, yet the upper function would return 20. Because turing arrays do not start at a default value of 1 or 0, the upper function does not accurate reflect how many elements of the array there are
In this situation however, upper will return the size of the array as the array starts on 1. In other istuations the size of the array can easily be found using the formula, size = upper (array) - lower (array) + 1
