I have the following function that should return an average of l1– l7. However, it seems to only return an integer. Why is it doing this, and how do I make it return a float rounded to 2 decimal places?
Snippet:
int lab_avg(int l1,int l2,int l3,int l4,int l5,int l6,int l7) {
float ttl;
ttl = l1 + l2 +l3 +l4 +l5 +l6 +l7;
return ttl / 7.0;
}
Because your function’s return type is an
int. Change it tofloatand it’ll work fine.Also, if you just want to print 2 decimal places, use an appropriate format in your output function. You don’t need to do anything with the float itself: