/* This program demonstrates * 1. Nested Loops * 2. Deriving loop conditions from patterns * 3. Introduction to functions from the caller perspective * * Deriving loop conditions and formulae in general from examples. * Sometimes, it not obvious how many times a loop should run. * In this case, we can write out a few examples, and derive a formula for * the number of iterations from those examples. * * Functions * Functions are small modules of code. They are considered to be a unit * consisting of a few lines of code that solves a particular problem. * Functions usually serve only one pruprose. For example, we can write a * function to find out if a number is prime, and aother to find the area * of a circle, but we will ideally never combine the two. * * C++ already offers a variety of pre-defined (pre-implemented) functions * through libraries. To use a particular function, we just need to include * the library that contains the function. For example, in this program, we * will use the functions in the math library. * * cmath is a C++ library that contains several functions that will * solve select math problems. * You can look up the cmath reference at cplusplus.com * * User(Programmers, as the "users" of C++) Defined Functions * Programmers can also write their own functions. These are called user-defines * functions. Here, we are the user (for C++), just like we call someone running * our program, the user. * * Functions have the following syntax: * return_type name ( list of arguments) * 1. The name of the function is whatever we are calling this function. Function * names are identifiers, so we should follow the rules and conventions for * naming identifiers, just like we do with variables. * 2. The list of arguments is a comma separated list of variables that are sent * into the function. These are also called parameters. Each of the parameters * requires its own type, even if they are all the same type. * 3. The return_type is the type of data that the function is going to send back * to the place it was called from. */ #include #include // for the math functions using namespace std; int main() { int size; cout<<"Enter the letter size: "; cin>>size; /* Print H * For the H, we see some space between teh 2 stems. We have to put the * spaces in there ourselves. The *'s will not be automatically spaced out. * As usual, we complete one row before moving on to the next. * * 0 * * * 1 * * * 2 ***** * 3 * * * 4 * * * * 5/2 => 2. 7/2 => 3. 9/2 =>4 middle row -> size/2 * If size =5, 3 space. IF size=7, 5 space. If size=9, 7 spaces. Number of sapce => size-2 * * middle row -> row full of * * any other row -> *, then size-2 spaces, then * */ cout<<"\nPrinting H\n"; for(int r=0; r>num; cout<<"Enter the exponent: "; cin>>exp; ans1 = sqrt (num); // call the sqrt funtion to get the square root cout<<"The square root of "<