Lab2_Q2

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include <cmath> using namespace std; double AM(double a, double b) { return (a+b)/2; }// AM = (a + b + c + d + e)/5 double GM(double a, double b) { if(a*b < 0){ return -1; }else{ return pow(a*b,0.5); } }// GM = (abcde)^(1/5) double HM(double a, double b) { if(a == 0 || b == 0){ return -1; }else{ return 1/((1/a + 1/b)/2); } }// GM = 1/ ((1/a + 1/b +1/c + 1/d + 1/e)/5) double mean(double a, double b, int meanType = 0){ if(meanType == 0) return AM(a,b); if(meanType == 1) return GM(a,b); if(meanType == 2) return HM(a,b); } int main() { cout << "The arithmetic mean of 3 and 5 is: " << mean(3, 5) << endl; cout << "The geometric mean of 3 and 5 is: " << mean(3, 5, 1) << endl; cout << "The harmonic mean of 3 and 5 is: " << mean(3, 5, 2) << endl; return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines