Notes week of Sep 16

circle-weak.cpp

A correct program, but not a very good one.

// NOT a good example!
#include <iostream>
using namespace std;
int main()
{
    double a;
    cout<<"Enter radius: ";
    cin>>a;
    cout<<"Area is "<< 3.14159265*a*a
    <<endl<<"Circ is "<< 6.2831*a<<endl;
    return 0;
}

circle.cpp

A better program to compute the same thing.

/* This program calculates the area of a circle
 * whose radius is input by the user.
 * 16 Sep 2015 by C League
 */

// A good example illustrating "separation of concerns"
// between input, calculations, and output.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int radius = 0;
    double area;

    cout << "Enter radius of circle: ";
    cin >> radius;

    area = M_PI * pow(radius, 2);
    cout << "Area is " << area << endl;

    // This works too, but doesn't follow
    // "Separation of Concerns"
    //cout << "Area is " << M_PI * pow(radius,2) << endl;

    double circumference = 2 * M_PI * radius;
    cout << "Circumference is "
         << circumference << endl;

    return 0;
}