2

I have a problem to calculate the average

let's admit, we need to know the average vote, so we add the votes of the surveys and we do the average.

So that if we have

Survey 1 : +1
Survey 2 : -1, -1, -1, +1
Survey 2 : +1

The average will be (+1 -1 -1 -1 +1 +1) / 6 = 0

So first thing to modify, have the average mark of all the surveys

Survey 1 : 1
Suveey 2 : -0,75
Survey 3 : 1 

And then calculate the average of the surveys (1 - 0,75 +1) / 3 to have 0.41

I calculate the average of each survey as follows, take the case of the 2nd survey :

 int survey2[] = {-1, -1, -1, 1};
 int somme = 0;
 for (int nombre : survey2) {
 somme += nombre;
 }
 float moyenneSurvey2 = (float) somme /survey2.length;
 System.out.println(moyenneSurvey2);

but I have a result of -0.5 instead of -0.75 which is wrong

asked Sep 27, 2020 at 12:12
2
  • 1
    You're just wrong it seems -1, -1, -1, +1 => -2 and -2/4 = -0.5 Commented Sep 27, 2020 at 12:15
  • In order to avoid integer division, you'll need to cast things to float or double, before the division. Commented Sep 27, 2020 at 12:18

1 Answer 1

3

The problem you have is a math problem, not a programming problem.

You think that survey 2 should have a mean of -0.75, but it should not. It should have a mean of -0.5. Let me show you why.

sum of survey 2 = -ひく1 +たす -ひく1 +たす -ひく1 +たす 1
sum of survey 2 = ( -1 + -1 ) + ( -1 + 1 )
sum of survey 2 = -2 + 0
sum of survey 2 = -2
count of survey 2 = 1 +たす 1 +たす 1 +たす 1
count of survey 2 = 4
mean of survey 2 = (sum of survey 2) / (count of survey 2) 
mean of survey 2 = ( -2 ) / ( 4 )
mean of survey 2 = -0.5

this is an important lesson in programming. If you construct a unit test (which even if you are not unit testing this program, you basically built a unit test you are following manually), you can have errors in your test (as well as errors in your program).

Good luck, and be happy your program works.

answered Sep 27, 2020 at 12:19
Sign up to request clarification or add additional context in comments.

2 Comments

your reason I want the average of the votes that have +1. So I just have to count of the +1 and divide by the size of the survey to have the right mean which I want
@Rodik I don't have a reason in this approach. I'm using the definitions of sum, count, and mean. I only have an explanation why you got answers that didn't match your program. If you wan the average of only the votes that have a certain value (+1) then the average is +1, because you discard all the other values (which reduces the count). I'm not sure what you want; as you express it in a confusing way. If confusion exists in math, no program can make the confusion clear, as programming is a kind of discrete math.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.