calculating float type average with for loop C++

220 views Asked by At

I am trying to get an float type average number from int inputs using the for loop. But it doesn't show me a correct answer. Could anyone tell me what i have done wrong? I think probably I made mistakes in this part,

for( int i = 0; i < size; i++ )
{
    sum =+ myArray[ i ];
}
return static_cast< float >( sum ) / size; 

I have attached picture so you can see the whole code.

code picture

Thank you for any advices

1

There are 1 answers

2
cigien On

This line:

sum =+ myArray[ i ];

will apply unary+ on right hand side value and then assign that to sum, which is not what you want.

You're looking for:

sum += myArray[ i ];

which will add the right hand side to sum.