C++ as a Programming Language

C++ is a general purpose programming language created as an extension of the C programming language. It was designed with an orientation toward system programming and embedded, resource-contained software and large systems, with performance, efficiency, and flexibility of us as its design highlights.

C++ is an object oriented, staticly typed, compiled language. As a static typed language, all objects have their data type declared upon definition. As a compiled language, the source code is translated using compilers to produce machine code. Once compiled, the machine code is often undecipherable to humans. Because the compilation is performed in one step, compilation errors will prevent the code from compiling, and the code must debugged before the code is run and executed.

Comparing Python and C++"

Python by comparison is a high-level, mutli-paradigm, dynamically typed, garbage-collected, interpreted language. As an interpreted language, Python code is not compiled into machine instructions and is ready to be read and interpreted as is. Because it is dynamically written, data types are not declared, making Python more flexible and easy to use. However C++ is able to perform more efficiently because all data types are declared at the point of compilation, so the machine does not have to consume any extra time to interpret the code to determine what data types are being used.

In terms of syntax, C++ is a more rigorously structured language. As all object need to be declared, and syntax accuracy is more important, C++ may use more lines of code to accomplish the same objective in Python. On the other hand, as a language Python is sensitive to whitespace, meaning code written in python uses indents to separate sections and layer of code. C++ ignores whitespace, and uses curly braces {} to delineate sections of code and ; to separate lines. Between the two, only Python forces the programmer to organize the code visually through indents, although it is good practice to or intent in C++ to make the code more legible.

C++ also uses various shortcuts or conventions which I feel add some character to the language. As in the namesake, C++ has an increment operator (++) as well as a decrement operator (--). C++ will also use the conventions of || and && for or and and respectively.

Debugging C++

Because C++ is compiled language, the code must be free of errors for the code to compile successfully. C++ has a strict syntax, thus the compilers are able to identify any and all deviations in the code from expected syntax that will prevent the compilers from interpreting to code as we expect. As C++ will try to give full details and insight about each instance of error it encounters, it often produces large returns of error messages that become ironically increasingly less useful to the human programmer to troubleshoot and fix.

Riemann Sum in C++

A Riemann sum is an approximation of an integral using a finite sum of areas of uniform width. The formula for the Riemann sum is as follows: $$S = \sum_{i = 1}^{n} f(x_{i}) \Delta x$$ For our particular approximation we implemented Midpoint Riemann Sum, where the height $f(x_{i}$ of the box is calculated at the midpoints of the boxes, at points $a + \frac{\Delta x}{2} , a + \frac{3\Delta x}{2} , ... , b - a + \frac{\Delta x}{2}$ . We used C++ to define the function of the integrand, and calculated the midpoints of our boxes to determine the height of our boxes to be summed. The code for the calculation of the Riemann sum of the integral $\int_{0}^{1} \frac{1}{1-x^2} dx$ in C++ is as follows

double integrand(double x)
{
    return 1/(1+x*x);
}

double integral(double a, double b, int n_points)
{
    double Riemann = 0 ;
    double dx = (b - a)/n_points;
    for (int i=0; i<n_points; i++)
        {
            double boxArea = dx*integrand(a + i*dx + dx/2);
            Riemann += boxArea;
        }
    return Riemann;
}

int main (void)
{
    for (int i=0; i<50;i++)
        {
            double pi = 4*integral(0,1,i);
            std::cout<<"For "<<i+1<<" boxes, pi is approximately " << pi << "."<<std::endl;
        }
    return 0;
}

This exercise was very valuable for us to understand the data types and the scope of the objects we are declaring in our code and if they are able to be called in a given step.