I'm having trouble calculating ratings

173 Views Asked by At

I'm not a wiz at math and this problem has me a little stumped. I need the average score out of 5 but it's a little different how I do it.

I have 5 fields, field one is rating one, field two is rating two, all the way up to field 5.

I need to grab the average of all these fields together, this is currently what I have.

Sorry, it's in programming syntax

round((($rating_one / 5) + ($rating_two / 5) + ($rating_three / 5) + ($rating_four /5) + ($rating_five / 5) / 5));

So I divide each field by 5, and add them together and then divide the total amount of the fields for the average. The problem is I need it to be 1-5, and I keep getting 109.

Here are the numbers in the fields

13
432
42
33
23

What am I doing wrong? If you need more details, I'll happily provide (or try) more.

1

There are 1 best solutions below

0
On BEST ANSWER

If you know the ranges for each field, the simplest thing is to first scale them so they are all out of $5$, and then average them.

To convert a quantity $Q$ that takes values between $a$ and $b$ to a quantity that takes values between $0$ and $N$ linearly, you compute $$\frac{N(Q-a)}{b-a}.$$ Note that $Q-a$ takes values between $0$ and $b-a$, so dividing by $b-a$ gives you a number between $0$ and $1$, and multiplying by $N$ gives you a number between $0$ and $N$.

Say the first field is takes values from $0$ to $N_1$, the second from $0$ to $N_2$, the third to $N_3$, the fourth to $N_4$, and the last to $N_5$ (which may or may not be different). Then to convert them to be out of $5$ and then averaging them, you can take $$\frac{\text{Field 1}}{N_1} + \frac{\text{Field 2}}{N_2} + \frac{\text{Field 3}}{N_3} + \frac{\text{Field 4}}{N_4} + \frac{\text{Field 5}}{N_5}.$$ If you then need it to be an integer, you can round; this may give you $0$ as the answer, which you may or may not want. (There is no multiplication and division by $5$ because here you have five fields, each out of five, so the multiplication and division by $5$ cancel each other out).

In general, for $k$ fields, you would take $$\frac{5}{k}\left(\frac{\text{Field 1}}{N_1} + \frac{\text{Field 2}}{N_2} + \cdots + \frac{\text{Field }k}{N_k}\right).$$