On the problem of round function in CPP

I have seen the calculation of the average in a piece of CPP sample code, but I don"t quite understand that this round () + 0.5 is a magic operation. Please take a look at it

.
stu[i].grade[0]=round((stu[i].grade[1]+stu[i].grade[2]+stu[i].grade[3])/3.0)+0.5;

add: this is a programming problem, the purpose is to find the average of three courses, and then sort the mean of many people. Individuals do not quite understand the average operation of the above code.

CPP
Jun.09,2022

Hello, is the grade attribute a floating point type or an integer?
generally uses + 0.5 to perform upwards rounding operations.


I guess you grade is the int type, such as (91mm 92y92) / 3.091.666666 , and then if you directly assign a value to grade , then the floating-point integer will round the decimal part to 91 , and the operation of + 0.5 is like rounding up, 91.666666 and then turning it to 92

.
Menu