I am working on a project for a class and I am having trouble with the output of the geometric mean, which always come out to be 1 and I’m sure that isn’t right.
Here’s my code:
#include<iostream>
#include<math.h>
using namespace std;
int main(int argc, char**argv)
{
float i, j, k;
float a, h, g;
cout<<"Enter 3 floating point numbers"<<endl;
cin>>i>>j>>k;
while(i>0 && j>0 && k>0 )
{
a = (i+j+k)/3;
h = 3/((1/i) + (1/j) + (1/k));
g = pow((i*j*k),(1/3));
cout<<"Arithmetic: "<<a<<endl;
cout<<"Harmonic: "<<h<<endl;
cout<<"Geometric: "<<g<<endl;
cout<<"Enter 3 floating point numbers"<<endl;
cin>>i>>j>>k;
}
return(0);
}
1/3is an integer equal to0. You should write1.0/3.0(or1./3) to get a floating point value.