Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 average help
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ruscutie




PostPosted: Thu Jan 25, 2007 11:04 pm   Post subject: 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..

code:

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
Sponsor
Sponsor
Sponsor
sponsor
apomb




PostPosted: Thu Jan 25, 2007 11:13 pm   Post subject: 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




PostPosted: Thu Jan 25, 2007 11:16 pm   Post subject: 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




PostPosted: Thu Jan 25, 2007 11:39 pm   Post subject: 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




PostPosted: Thu Jan 25, 2007 11:41 pm   Post subject: Re: average help

You can try
Turing:
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




PostPosted: Thu Jan 25, 2007 11:47 pm   Post subject: 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




PostPosted: Fri Jan 26, 2007 12:07 am   Post subject: RE:average help

can some1 plz explain to me what is [upper]... it was used in the code above...?
agnivohneb




PostPosted: Fri Jan 26, 2007 12:20 am   Post subject: RE:average help

Sry CompWiz333.
Sponsor
Sponsor
Sponsor
sponsor
apomb




PostPosted: Fri Jan 26, 2007 12:20 am   Post subject: 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




PostPosted: Fri Jan 26, 2007 12:24 am   Post subject: 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




PostPosted: Fri Jan 26, 2007 12:30 am   Post subject: 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




PostPosted: Fri Jan 26, 2007 12:38 am   Post subject: RE:average help

thnx guyz
apomb




PostPosted: Fri Jan 26, 2007 1:35 am   Post subject: Re: average help

y'r wlcm
Bored




PostPosted: Fri Jan 26, 2007 12:32 pm   Post subject: Re: average help

CompWiz333 @ Fri Jan 26, 2007 12:30 am wrote:
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:
Turing:
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




PostPosted: Fri Jan 26, 2007 1:26 pm   Post subject: 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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: