Lab2_1

rofels
 avatar
unknown
c_cpp
3 years ago
1.6 kB
7
Indexable
#include <iostream>
#include <cmath>
using namespace std;

enum CycleOperator
{
    CFor,
    CWhile,
    CDoWhile
};

double Function(double y);
void PrintFunction(double a, double b, double h, CycleOperator cOp);

int main()
{
    double a = 0;
    double b = 1;
    double h = 0.2;
    
    CycleOperator cFor = CFor;
    CycleOperator cWhile = CWhile;
    CycleOperator cDoWhile = CDoWhile;
    
    PrintFunction(a, b, h, cFor);
    cout << endl;
    a = 1;
    b = 2;
    PrintFunction(a, b, h, cWhile);
    cout << endl;
    a = 2;
    b = 3;
    PrintFunction(a, b, h, cDoWhile);
    cout << endl;
    
    return 0;
}

void PrintFunction(double a, double b, double h, CycleOperator cOp)
{
    switch(cOp)
    {
        case(CFor):
            cout << "Printing function with for loop" << endl;
            for(; a < b; a += h)
                cout << "Function value at " << a << " is: " << Function(a) << endl;
            break;
        
        case(CWhile):
            cout << "Printing function with while loop" << endl;
            while(a < b)
            {
                cout << "Function value at " << a << " is: " << Function(a) << endl;
                a += h; 
            }
            break;
        
        case(CDoWhile):
            cout << "Printing function with doWhile loop" << endl;
            do
            {
                cout << "Function value at " << a << " is: " << Function(a) <<endl;
                a += h;
            } while(a < b);
            break;
    } 
}

double Function(double x)
{
    double absValue = fabs(2 / pow(1 + x, 3));
    return sqrt(sqrt(absValue));
}    
Editor is loading...