answersLogoWhite

0


Best Answer

Use the following function:

/* returns the average of two real numbers */

double average (const double a, const double b) {

return a/2.0 + b/2.0;

}

Note that we do not use (a+b) / 2.0 because the expression a+b could overflow the range of a double. By dividing each value by 2 before summing we ensure the result can never overflow no matter how large a and b are.

Ideally, you should write separate functions to cater for float and long double arguments independently:

/* returns the average of two long doubles */

long double average_lng (const long double a, const long double b) { return a/2.0 + b/2.0;

}

/* returns the average of two floats */

float average_flt (const float a, const float b) {

return a/2.0F + b/2.0F;

}

For mixed-mode arithmetic, always use the highest precision argument. E.g., the average of a float and a double is a double, so use the function that returns a double, not a float. The float argument will be implicitly cast to a double.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

double getAverage(const double a, const double b) { return (a + b)/2.0;}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you Calculate the average of two numbers then return the average in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the average of 12.3 plus 6.4 plus 0.44?

6.38 is the average of the three numbers.


What is these numbers average 29.7 plus 48.9 plus 8?

Just add the numbers, and divide the result by 3 (since there are 3 numbers).


What is the formula to calculate consecutive even numbers like 2 plus 4 plus 6 plus 8 or 20 plus 22 plus 24 plus 26?

do something


What is the mean of 70 plus 70 plus 70 plus 10 plus 10 plus 8 plus 4 plus 18 plus 5 plus 40?

The mean of a set of numbers is the average. That is to say, all the numbers added together and divided by the number of the numbers. the answer is 30.5


C plus plus program calculate the power value of input base and exponent numbers?

cn = c0 *( 1 + i ) pow n


What is the average of 80 plus 85 plus 90 plus 88 plus 86 plus 83 plus 89 plus 70?

The average is all the numbers added together and then divided by the # of numbers.(80+85+90+88+86+83+89+70)/8=671/8=83.875Order of operations has the brackets first and then the division.


What is the averageof 132 plus 140 plus 145?

The average is the sum divided by the number of original numbers. The sum of 132, 140, and 145 is 417. There were three original numbers, so the average is one-third of 417 or 139


What is the average of 2.5 plus 3 plus 4?

2.5 + 3 + 4 = 9.5 then divide by 3 (the number of numbers) = 3.167


How can you calculate this numbers 12345 to have total 50?

1 plus 2 plus 3 plus 4 equal 10. 10 times 5 equals 50.


What is 123 plus 4567 plus 345678 plus 444546846468446466466464 plus 4464646 plus 763157 plus 4684748 plus 1236545687654489875435?

We have the resources to calculate the answer to that example, and we know how to use them. Tell us how such unusual numbers arose, and how the answer will help you in your work, and we'll put our staff to work to find it for you.


Write a program in c plus plus language to add 2 numbers?

int main() { int num1; int num2; int result = num1 + num2; return 0; }


C plus plus program to find average of two numbers?

#include <iostream> using namespace std; int main() { double num1, num2, avg, sum; //use double instead of int because int only recognize whole numbers cout<<"Enter a number: "; cin>>num1; cout<<"Enter a number: "; cin>>num2; sum=num1+num2; avg=sum/2 cout<<" The average is: "<<avg; system("pause"); return 0; }