Showing posts with label format. Show all posts
Showing posts with label format. Show all posts

Friday, December 15, 2017

C++ Math Library Function Table

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

double sqrt (double x)
{
    return sqrt (x);
}
double Exp (double x)
{
    return exp (x);
}
double Log (double x)
{
    return log (x);
}
double Powa (double x)
{
    return pow(2,x);
}
double Logten (double x)
{
    return log10(x);
}
double Fabs (double x)
{
    return fabs(x);
}
double Ceil (double x)
{
    return ceil(x);
}
double Floor (double x)
{
    return floor(x);
}
double Sin (double x)
{
    return sin(x);
}
double Cos (double x)
{
    return cos(x);
}
float Tan (float x)
{
    return tan(x);
}

int main()
{
    cout << "Function" << setw(10) << "1" << setw(8) << "2" << setw(8) << "3" << setw(8)<< "4" << setw(8) << "5" << endl;
        cout << endl;

    cout << "sqrt()" << setw(12);
    for (int x = 1; x < 6; x++)
    {
        cout << setprecision(4) << sqrt(x) << setw(8);
    }

    cout << endl;

    cout << setw(0) << "exp()" << setw(13);
    for (int x = 1; x < 6; x++)
    {
        cout << setprecision(4) << Exp(x) << setw(8);
    }

    cout << endl;

    cout << setw(0) << "log()" << setw(13);
    for (int x = 1; x < 6; x++)
    {
        cout << setprecision(4) << Log(x) << setw(8);
    }

    cout << endl;

    cout << setw(0) << "Log10()" << setw(11);
    for (int x = 1; x < 6; x++)
    {
        cout << setprecision(4) << Logten(x) << setw(8);
    }

    cout << endl;

    cout << setw(0) << "pow()" << setw(13);
    for (int x = 1; x < 6; x++)
    {
        cout << setprecision(4) << Powa(x) << setw(8);
    }

    cout << endl;
    cout << endl;

    cout << "Function" << setw(10) << "-1.5" << setw(8) << "-0.04" << setw(8) << "0.70" << setw(8) << "1.8" << setw(8) << "2.90" << endl;
        cout << endl;

    cout << setw(0) << "fabs()" << setw(12);
    for (double x = 0; x < 5; x++)
    {
        double y = (-1.5) + (x * 1.1);
        cout << setprecision(4) << Fabs(y) << setw(8);
    }

    cout << endl;

    cout << setw(0) << "ceil()" << setw(12);
    for (double x = 0; x < 5; x++)
    {
        double y = (-1.5) + (x * 1.1);
        cout << setprecision(4) << Ceil(y) << setw(8);
    }

    cout << endl;

    cout << setw(0) << "floor()" << setw(11);
    for (double x = 0; x < 5; x++)
    {
        double y = (-1.5) + (x * 1.1);
        cout << setprecision(4) << Floor(y) << setw(8);
    }

    cout << endl;

    cout << endl;

    cout << "Function" << setw(10) << "-1.57" << setw(8) << "0.00" << setw(8) << "1.57" << setw(8) << "3.14" << setw(8) << "4.71" << endl;
        cout << endl;

    cout << setw(0) << "sin()" << setw(13);
    for (double x = 0; x < 5; x++)
    {
        double y = (-1.57) + (x * 1.57);
        cout << setprecision(2) << fixed << Sin(y) << setw(8);
    }
    cout << endl;

    cout << setw(0) << "cos()" << setw(13);
    for (double x = 0; x < 5; x++)
    {
        double y = (-1.57) + (x * 1.57);
        cout << setprecision(2) << fixed << Cos(y) << setw(8);
    }
    cout << endl;

    cout << setw(0) << "tan()" << setw(13);
    for(double x = 0; x < 5; x++)
    {
        double y = (-1.57) + (x * 1.57);
        cout << Tan(y) << setw(8);
    }

    cout << endl;

    return 0;

}

C++ Fahrenheit into Celsius & Celsius to Fahrenheit in Table format

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

double F (double);
double F (double celsius)
{
    return((celsius - 32)*.56);
}
double C (double);
double C (double fahrenheight)
{
    return((1.8)*fahrenheight + 32);
}

int main()
{
    double celsius;
    double fahrenheight;

    cout << "Fahrenheit equivalents of Celsius temperatures: " << endl;
    cout << setw(5) << "Cel" << setw(11) << "Fahr" << setw(6) << "Cel" << setw(11) << "Fahr" << setw(6) << "Cel"
    << setw(11) << "Fahr" << setw(6) << "Cel" << setw(11) << "Fahr" << endl;

    for (int i = 1; i <= 26; i++)
    {
        for (int j = 1; j <= 4; j++)
        {
            celsius = 25*(j - 1) + i - 1;

            if (i == 26 && j != 4)
                cout << setw(19) << "\b";
            else
                cout << setw(5) << celsius << setw(11) << C (celsius) << " ";

            if (j == 4)
                cout << endl;
        }
    }


    cout << "Celsius equivalents of Fahrenheit temperatures: " << endl;
    cout << setw(5) << "Cel" << setw(11) << "Fahr" << setw(6) << "Cel" << setw(11) << "Fahr" << setw(6) << "Cel"
    << setw(11) << "Fahr" << setw(6) << "Cel" << setw(11) << "Fahr" << endl;

    for (int i = 1; i <= 46; i++)
    {
        for (int j = 1; j <= 4; j++)
        {
            fahrenheight = 45*(j - 1) + i + 31;

            if (i == 46 && j != 4)
                cout << setw(19) << "\b";
            else
                cout << setw(5) << setprecision(2) << static_cast< int >(fahrenheight) << setw(11) << F (fahrenheight) << " ";
                // setprecision(2) << static_cast< int >() ; added
            if (j == 4)
                cout << endl;
        }
    }

    return (0);
}