On top of the program always include a section between /* and */ which contains the following items:
|
/* A sample program file: 1ex1.cpp ( or used as 1ex1.appended) FALL 1998 ___________________________________ Jacob Y. Kazakia jyk0 July 14, 1998 Programming assignment 1 of week 1 Recitation Instructor: J.Y.Kazakia Recitation Section 01 ___________________________________ Purpose: This program calculates the area of a sector of circle, given the radius of the circle in meters and the angle of the sector in degrees. Algorithm: The area of the entire circle is given by the formula: Area = pi * radius * radius ( here pi = 3.1415927 ) If the sector angle is theta degrees, then its area is: Sector_area = ( theta / 360 ) * pi * radius * radius */ |
Include the necessary headers Avoid global variables but there may be exceptions to the rule |
#include<iostream.h> // header which contains the functions // needed for input and output const float PI = 3.1415927; // No one should be allowed to alter // this important constant void main() { |
On top of each function declare all the variables to
be used. Using the // type comment describe the physical significance
of the variables and their units of measurement. Use properly selected
variable names |
// variables are typed and defined float radius; // the value of the radius of the circle ( meters) float theta; // The angle of the sector ( degrees) float area; // The area of the sector ( it will be in square meters) |
Use clear prompts for the user. Specify the units. |
// prompt for and read the radius cout<< endl<<"Enter the radius of the circle (in meters) >>>"; cin>>radius; // prompt for and read the sector angle cout<< endl<<"Enter the angle of the sector (in degrees) >>>"; cin>>theta; |
Print all values entered via the keyboard. Catch errors. |
// Echo printing ( we must always double check ourselves and // others) cout<<endl<<endl; cout<<"We will now calculate the area of a sector of a circle"<<endl; cout<<"The radius of the circle was given as :"<<radius ; cout <<"meters"<<endl; cout<<"The angle of the sector was given as: "<<theta ; cout <<"degrees"<<endl; |
Use simple arithmetic expressions. If you have complicated relations break them into smaller ones |
// calculation ( do you really need a computer for this? ) area = (theta/360 )* PI * radius * radius; |
When you output numbers, always include a label explaining the number and always give the units |
cout<<endl<<"The area is calculated as : "; cout <<area<< " square meters"<<endl; cout<<endl<<endl; |
This part will keep the default black screen open until we read its contents |
cout<<" enter e (exit) to terminate the program"; |