/* 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; /* For the next example, we will print an ASCII art letter with the same * format as the letters at the end of the previous example. * This time, we will print the letter C. * The letter C looks something like this: * ***** * * * * * * * ***** * Here, we have "size" number of stars on the top and bottom row. Every * other row has only one star. * How do we know whoch row is the top row? * It is the "first", or in our case, the 0th row */ cout<<"\nPrinting c"< 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>angle; cout<<"S - Sine\nC - Cosine\nT- Tangent\nE- Exit\nEnter your choice: "; cin>>option; switch(option) { case 'S': cout<< sin(angle); break; case 'C': cout<