C++ Examples - Basic Syntax

Question: Write a program to calculate the area and circumference of a circle of radius 5.0. Change the code so that the radius may be specified by the user. For input you will need to use the input cin command.



For the first part of this question we will need to do some calculations and get access to the value of the mathematical constant pi. But before we think about that, you should think about what the question asks, and therefore what are the following:-

  • Libraries/Methods
  • Variables/Parameters/Inputs
  • Calculations
  • Outputs
In this example we are going to need input output and also a mathematical constant, so it would be a good idea to include the cmath library. Programming is best split up into stages and you should try to compile and run you programs as frequently as possible. The idea is simple, if you have only edited one or two lines and your program breaks, you know where the problem is, if you have added 50+ it is much harder to know where you have gone wrong. As such, you should begin by writing an empty program, and compile and run it to check for errors. Your initial program should look like:


// include the input output stream library
// so that you can write to the screen
#include <iostream>
// we will also need the cmath library to
// get access to mathematical functions
#include <cmath>
// use the std namespace
using namespace std;

int main()
{
// do nothing yet
return 0;
}

The output should be blank.

Next we think about the variables and parameters and how to input values. At first it is always better to input the values of variables into the program itself to make it quicker whilst you are debugging, so we will assign the variable inside the code first. Now what do we need? The question asks for the calculation of the area and the circumference of a circle, which indicates that we will need

  • radius, a real number
  • pi, a real number
  • area, a real number
  • circumference, a real number

Now we can declare these variables inside the main function, allowing them to be assigned values and involved in calculations. To declare we write double as the type of the variable, here you can think of double as a real number, and then list the variable names separated by commas and finished by a semi colon. You should use as descriptive a name as possible, and those variable names can include letters from the alphabet in upper or lower case and numbers (cannot begin with a number) and underscores.


double radius,pi,area,circumference;

Next assign those variables with a value, remember to finish statements with a semi colon. Multiple statements can be written on the same line as long as they are closed off.


radius=5.;pi=4.*atan(1.);

Next we need to do the calculations. The only thing to note here is that there is no operator for powers, only the pow(x,y) function from the math library, so when calculating a square of a number it is easier to just write number*number. The calculations are:


area = radius * radius * pi;
circumference = 2. * pi * radius;

Finally we can output the solution to screen:


cout << " A circle with radius " << radius << " has area ";
cout << area << " and circumference " << circumference << "." << endl;

At this stage your program should look like:


// use the input/output libraries and mathematical functions
#include <iostream>
#include <cmath>
// again use the namespace
using namespace std;

int main()
{
// use descriptive names for variables
double radius,pi,area,circumference;
// assign those variables with a value
radius=5.;pi=4.*atan(1.);
// now do the calculations
area = radius * radius * pi;
circumference = 2. * pi * radius;
// output results to screen
cout << " A circle with radius " << radius << " has area ";
cout << area << " and circumference " << circumference << "." << endl;
return 0;
}

and the output in the terminal should be:

A circle with radius 5 has area 78.5398 and circumference 31.4159.

Finally we add in the ability to let the user change the value of the radius. To do this we need to add in two extra lines in between the declaration of radius and the calculations, as well as removing the assignment of value we already have. The two extra lines consist of one prompting the user for input, and one getting a value from the keyboard.


cout << " Input the value of the radius " << endl;
cin >> radius;

Check that you program compiles and runs as expected. Your final program should look like:


// use the input/output libraries and mathematical functions
#include <iostream>
#include <cmath>
// again use the namespace
using namespace std;

int main()
{
// use descriptive names for variables
double radius,pi,area,circumference;
// assign those variables with a value
pi=4.*atan(1.);
// prompt the user for input
cout << " Input the value of the radius " << endl;
// get the value from the keyboard
cin >> radius;
// now do the calculations
area = radius * radius * pi;
circumference = 2. * pi * radius;
// output results to screen
cout << " A circle with radius " << radius << " has area ";
cout << area << " and circumference " << circumference << "." << endl;
return 0;
}

Comments

Popular posts from this blog

Exclusive Interview: 2011 8TV Hot Chef (美食型男) Jason Cha

Career Lessons from Breaking Bad

Things We Now Know