The arithmetic mean of two numbers is the result of dividing their sum by 2. The geometric mean of two numbers is the square root of their product. The harmonic mean of two numbers is the arithmetic mean of their reciprocals. Write a C++ program that asks the user for two real numbers as inputs and displays these three means. (hint: you will need to include cmath library)

We won't do your homework for you, but if you have hit a snag, post what you have done so far and where you are are stuck and we'll see if we can help you through it.

sqrt is the squareroot function in C++.
Harmonic mean is the reciprocal of the mean of the reciprocals or 2/(1/number1 + 1/number2).

To complete a task like this you just need to break the task down into little chunks.

Let me show you what I mean:

  1. Start by writting a simple 'hello world' program.
  2. Modify that program to read input from the user.
  3. Then modify that program to check and convert that user input into float values.
  4. Next use those float values to do the required calculations.
  5. Finally write those calculations back out to the user.
// Joshua Haglund

#include <iostream>
#include <cmath>

float a = 0;
float b = 0;
float c = 0;

int main()
{

    while (1) {
        std::cout << "\n";
        std::cout << " Enter 2 numbers greater than 0.\n";
        std::cout << "\n";
        std::cout << " First number: ";
        std::cin >> a;
        std::cout << " Second number: ";
        std::cin >> b;

        std::cout << "\n";
        std::cout << " Doing some magic...\n";
        std::cout << "\n";

        c = (a + b) / 2;
        std::cout << " arithmetic mean of two numbers: (" << a << " & " << b << ") = " << c;
        std::cout << "\n";

        c = sqrt(a * b);
        std::cout << " geometric mean of two numbers: (" << a << " & " << b << ") = " << c;
        std::cout << "\n";

        c = 2 / (1 / a + 1 / b);
        std::cout << " harmonic mean of two numbers: (" << a << " & " << b << ") = " << c;
        std::cout << "\n\n";

    }
}
commented: Are you OK Joshua? I haven't seen you active for 5 years. +16
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.