Search code examples
c#logic

how to calculate average rating


Sorry if I am not clear:

How would I calculate the average rating based on the MinRate and MaxRate

for example: minRate = 0 or 1 and MaxRate = 2 or 3 or 4 or 5 or 0

How would I calculate the rating?

Update End

I am implementing a 5 star rating system and my table structure has two columns: 1) MinRate 2) MaxRate

so my question is: how do calculate the average rating?

here is how I have in my 5 star rating implementation.

int[] {7, 0, 0, 8, 5}; 

This means that:

  • 7 users rated the article with a 1
  • 0 users rated the article with a 2
  • 0 users rated the article with a 3
  • 8 users rated the article with a 4
  • 5 users rated the article with a 5

Solution

  • Here is some pseudo code:

    
    double avg = 0;
    for (int i = 0; i < arr.Length; i++) // arr.Length should be the same as MaxRate
    {
        avg += arr[i] * (i + MinRate);
    }
    avg /= arr.length;