Get the average value ?

  • Thread starter Thread starter jesper_lofgren
  • Start date Start date
J

jesper_lofgren

Hello,

I need to write a function to calculate the average value from a coupe
of parameters from database.

Question 1

(value) (people that answer the specific value)
[1] 5
[2] 21
[3] 67
[4] 233
[5] 348

Any ideas would be very welcome.

Thanks
Jess
 
If you want to impress your teacher, you should use a recursive
function. Else just use a for(each) function.

Good luck.

Jeroen


(e-mail address removed) schreef:
 
Well, if the data is in a database, then the best option is to do this *at*
the database, using SQL with GROUP BY and AVG(); however, this then becomes
the wrong group ;-p

Marc
 
Hello,

I need to write a function to calculate the average value from a coupe
of parameters from database.

Question 1

(value) (people that answer the specific value)
[1] 5
[2] 21
[3] 67
[4] 233
[5] 348

Any ideas would be very welcome.

Thanks
Jess

Please get help from your tutor or another student.
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Hi,

You can do it in the DB using AVG , like:

sqlCommand.Text =" SELECT avg( answers) from table1 "

Now, IMO the above (or you question) has no mean at all, I think you meant
percent of answers for each value, like :

[1] 6%
[2] 20%
.....

Confirm if so, it should be easy to generate the above result using a SP
 
Thanks for the answers,

Oh yea AVG() is a great way to do it, but i need to do it in C# code.
Any ideas on that case ?

/ Jess

Otis Mukinfus skrev:
Hello,

I need to write a function to calculate the average value from a coupe
of parameters from database.

Question 1

(value) (people that answer the specific value)
[1] 5
[2] 21
[3] 67
[4] 233
[5] 348

Any ideas would be very welcome.

Thanks
Jess

Please get help from your tutor or another student.
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Erm... add 'em up and divide by the number of items? Loop, with an
accumulator and a counter... Am I missing the tricky bit?

To be honest, you can overdo these things. If you want something more
advanced, there are plenty of examples via google, but to be honest this
sounds so much like homework that you aren't actually doing yourself any
favors unless you simply try a few things... see what works... see what
doesn't. You need to develop the ability to deduce a new solution to a
problem...

That said, try google, or for a simple aggregation example look at the last
post here:
http://tinyurl.com/y863ew

Marc

Thanks for the answers,

Oh yea AVG() is a great way to do it, but i need to do it in C# code.
Any ideas on that case ?

/ Jess

Otis Mukinfus skrev:
Hello,

I need to write a function to calculate the average value from a coupe
of parameters from database.

Question 1

(value) (people that answer the specific value)
[1] 5
[2] 21
[3] 67
[4] 233
[5] 348

Any ideas would be very welcome.

Thanks
Jess

Please get help from your tutor or another student.
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Y'know if you work it out yourself you'll actually deserve the grade you
get.

Iterate, add then divide by the count. Someone else has already mentioned
foreach so you have more than enough to start with.

Thanks for the answers,

Oh yea AVG() is a great way to do it, but i need to do it in C# code.
Any ideas on that case ?

/ Jess

Otis Mukinfus skrev:
Hello,

I need to write a function to calculate the average value from a coupe
of parameters from database.

Question 1

(value) (people that answer the specific value)
[1] 5
[2] 21
[3] 67
[4] 233
[5] 348

Any ideas would be very welcome.

Thanks
Jess

Please get help from your tutor or another student.
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Hello,

I need to write a function to calculate the average value

Your value calculated as below average!

from a coupe
of parameters from database.

Question 1

(value) (people that answer the specific value)
[1] 5
[2] 21
[3] 67
[4] 233
[5] 348

Any ideas would be very welcome.

Thanks
Jess
 
I need to write a function to calculate the average value from a coupe
of parameters from database.

Question 1

(value) (people that answer the specific value)
[1] 5
[2] 21
[3] 67
[4] 233
[5] 348

Any ideas would be very welcome.

Well, how far have you got, and which bit are you stuck on?
 
Ok, so I take it you do not have to write the code to fetch the data,
no? So what kind of thing is the data in? An array? a datagrid? what?
Point is you need to look up how to reference the values in that thing.

Then, write out the program / algorithm / strategy in english. Refine
the english until you can substitue real Csharp code for the english
statements. You know how calculate average; write out the steps in
english. Hint. There's going to be a loop where you successively get
values one at a time from your "data storage thing" and add them up.

No one ever writes a program by writing the first line of Csharp, then
the second, third, ... in order until done. You must first have an
algorithm, or strategy, or whatever you want to call it, for solving
the problem. Write that down in english statements. Even experienced
programmers don't skip the first step - develop a strategy (aka
algorithm) for solving the problem. But experience allows us to often
skip the "write it in english part." You should not skip this part.
Sometimes when I'm stuck I resort to writing it out and darned if it
doesn't help every time!

You'll probably be skipping around, refining different pieces here and
there; not strictly in top-to-bottom order. That's OK. In fact that's
good.

Final hint. You know that the code bits you need to solve this problem
were presented in class (you did go to class, didn't you?). Review your
book and notes!!!!!

I need to write a function to calculate the average value from a coupe
of parameters from database.

Question 1

(value) (people that answer the specific value)
[1] 5
[2] 21
[3] 67
[4] 233
[5] 348
Any ideas would be very welcome.

Well, how far have you got, and which bit are you stuck on?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top