Tuesday, July 20, 2010

NOTES ON C & C++

Data Types
The following topics are covered in this section:
• Introduction
• Integer
• Floating Type
• Double
• Character
• Boolean
• Data Type Ranges and determining the ranges
• More on Binary Numbers
________________________________________
Every piece of data has to belong to some basic category. Consider a simple example in real life: every number has to be of a particular type. The number 5 is a natural number (or it can be called as a whole number). 6.5 is a real number (it has a decimal point). Similarly, in programming we have what are called as data types. When a variable is declared, the programmer has to specify which data type it belongs to. Only then will the compiler know how many bytes it should allocate for that particular variable. Or in other words, each data type occupies a different memory size and if a variable is declared as belonging to one particular data type it cannot be assigned a different data type value. In simpler terms, suppose the variable ‘x’ is declared such that it can hold only whole numbers; then it cannot (and should not) be assigned some alphabet.
There are two categories of data types: fundamental data types and user-defined data types. The second category of data types will be dealt with later.
The fundamental (or built-in or primitive) data types are:
• Integer
• Floating Point
• Character
• Double
• Bool
The first three data types: integer, floating point and character are used frequently.
________________________________________
Integer (int):
An integer can contain only digits (numbers) from 0 to 9. Examples of integers are:
• 0
• 10
• 345
• 6789
• -23
• -600
It includes positive and negative numbers but the numbers have to be whole numbers. It does accept the decimal point. Hence the following numbers are not integer data types:
• 3.5
• 4.8
• 0.23
These numbers come under the second category (floating point type) and not under integers. If the program has to accept such values from the user do not declare the variable as an integer. If a variable is declared as an integer and the user enters a value of 2.3, the program will assign 2 as the value for that integer variable. Similarly, if the user enters 3.2, the program will assign 3 to the integer variable.
Remember: Once a variable is declared as an integer, it will only store whole numbers (if the user types a value with the decimal point, the program will ignore everything that is typed after the decimal point).
How to declare a variable as belonging to the type integer? The syntax is:
int variable-name;
Each data type occupies a certain amount of memory space. An integer will occupy 2 bytes of memory (which means 16 bits). From this it is possible to calculate the maximum and minimum values that an integer can store. 2^16 = 65536 (hence 65536 different combinations of 16 bits are possible). Divide this by 2 because integers (by default) range from negative to positive values. We have a 0 in between and so subtract one from this to get 32,767. Hence an integer can take values from –32,768 up to +32,767 (a total of 65536 different values).
A natural question springs to mind, "What would happen if a value greater than 32,767 is entered?" Since this value cannot be accommodated within the allocated two bytes, the program will alter the value. It’s not exactly altering the value; it will basically change your value into something different. The user might enter 123456 as the integer value but the program will store it as –7623 or something like that. Whenever you use variables ensure that you have declared them as belonging to the correct data type.
This restriction on maximum range might seem to be a problem. In C++ ‘qualifiers’ can be used to vary the range of fundamental data types. Qualifiers are only supplements to the basic data types and they cannot be used separately on their own. They work only with a basic (or fundamental) data type. The 4 qualifiers available in C++ are:
1. Short
2. Long
3. Signed
4. Unsigned
Signed and unsigned integers were discussed in the first chapter. When an integer is specified as signed, then automatically the most significant bit of the number is used as a sign bit (to denote the sign of the number). Hence it can be used if the programmer needs positive and negative number values for the variable. By declaring a variable as an integer, by default you can specify both positive and negative values. By default an integer is a signed integer. In other words,
int variable-name;
is the same as
signed int variable-name;
In the second form, ‘signed’ is the qualifier and it is used to explicitly state that the variable is a signed integer. For an unsigned integer the syntax will be:
unsigned int variable-name;
An unsigned integer can hold a value up to 65,535 (a signed integer can hold only up to 32,767). Of course, in an unsigned integer you cannot assign a negative value. The range is from 0 to 65,535. To go beyond 65,535 and make use of both positive and negative values as well, the qualifier long should be used.
long int variable-name;
Long integers occupy 4 bytes of memory (32 bits). Remember, long int actually means signed long int (you can give positive and negative values).
If you specify
unsigned long int variable-name;
you can only assign positive values to the variable. Thus, two qualifiers can be used together with a basic data type.
What about the ‘short’ qualifier? Short integer is the same as a signed integer. It occupies two bytes and has the same range of positive and negative values as the normal integer case.
int x;
is usually the same as
short int x;
Compilers (depending on the operating system) will assume ‘int’ as a ‘long int’ or a ‘short int’. VC++ (since it works in the Windows OS) will default to ‘long int’ if you specify a variable as type ‘int’ (i.e. it will allocate 4 bytes to an ‘int’ variable). Turbo C++ (which is a DOS based compiler) will default to ‘short int’ when you specify a variable as type ‘int’. Thus the statement:
int var;
will allocate ‘var’ 4 bytes if you are using VC++ but the same statement will allocate 2 bytes if you are using Turbo C++ compiler.
Programmers sometimes prefer to explicitly state what type of integer they want to use by making use of the ‘short’ and ‘long’ qualifiers. ‘short int’ always occupies only 2 bytes (irrespective of whether the OS is Windows or DOS) while a ‘long int’ always occupies 4 bytes.
Two qualifiers can be used together, but do not try using:
short long int variable-name;
This will cause a compile-time error. So be careful with what qualifiers you use. And remember that the default for int is equivalent to short signed integer.
Floating Types (float):
Floating type data include integers as well as numbers with a decimal point. It can also have an exponent. Exponent means 10 to the power of some integer value (whole number). 20000 = 2 x 10^4 = 2e4 = 2E4.
If you specify decimal numbers, floating point data type will store up to a precision of 6 digits after the decimal point. Suppose 0.1234567 is assigned to a floating-point variable, the actual value stored would be 0.123457 (it will round up to the sixth digit after the decimal place). Valid floating-point numbers are:
• 0.1276
• 1.23
• 1.0
• 10.2
• 2e5 (this will be typed in your code as 2e5)
Do not use an exponent with a decimal point. For example: 2e2.2 is an invalid floating point because the exponent has to be an integer. Floating point numbers use 4 bytes of memory and has a much greater range than integers because of the use of exponents. They can have values up to 10^38 (in positive and negative direction). The same qualifiers used for an integer can be applied to floating point numbers as well. To declare a floating variable, the syntax is:
float variable-name;
Double (double):
This is similar to the floating-point data type but it has an even greater range extending up to 10308. The syntax to declare a variable of type double is:
double variable-name;
Beware: Visual C++ (VC++) usually uses its default as ‘double’ instead of ‘float’. Suppose we type:
float x=31.54;
you will get a warning message saying that a ‘double’ (i.e. 31.54) is being converted into a floating point. It is just to warn you that you are using a ‘float’ and not a ‘double’. (Even if there are warnings, there won’t be any problem in running your program).
Character (char):
A character uses just one byte of memory. It can store any character present on the keyboard (includes alphabets and numbers). It can take numbers from 0 to 9 only. The following are valid characters:
• A
• B
• 3
• a
• :
• ‘
• /
If the number 13 is entered as the value for a character, the program will only store 1 (i.e it will store the first character that it encounters and will discard the rest). A character is stored in one byte (as a binary number). Thus whatever the user enters is converted into a binary number using some character set to perform this conversion. Mostly all computers make use of the ASCII (American Standard Code for Information Interchange). For example, according to the ASCII coding, the letter ‘A’ has a decimal value of 65 and the letter ‘a’ has a value of 97.
There is another form of coding called the EBCDIC (Extended Binary Coded Decimal Information Code) which was developed by IBM and used in IBM computers. However, ASCII remains the most widely used code in all computers. The table at the end of the book gives a listing of the ASCII values and their equivalent characters. The syntax to declare a variable which can hold a character is:
char variable-name;
Boolean Type (bool)
This data type will only accept two values: true or false. In C++, ‘true’ and ‘false’ are keywords. Actually a value of true corresponds to 1 (or a non-zero value) and a value of false corresponds to 0.
#include
int main( )
{
bool check;
check = true;
cout<
int main( )
{
float PI = 3.14; // variables can be initialized during declaration
int rad;
cout<< "Enter the radius" ; cin>>rad;
cout<< "Area of the circle is "<< PI * rad * rad; return 0; } The preprocessor has only one directive and it will include the iostream.h header file into the source code. The compiler will start reading the code from the main ( ) function onwards. Remember: Whatever is typed within the main ( ) function will be executed. The main ( ) function is used as the entry point for a C++ program. PI is a variable name and is declared as a float quantity (because the value of PI has a decimal point). At the point of declaration, PI is initialized to a value of 3.14. This means that whenever PI is used in the program, the compiler will use 3.14 instead of PI. The line: cout<<"Enter the radius"; will cause Enter the radius to be displayed on the screen. This is because "Enter the radius" is typed within double quotes following ‘cout’ and the insertion operator. Anything between double quotes, along with cout<< will be displayed on the screen just as it appears within the double quote. The value entered by the user will be stored in the variable ‘rad’. Then the statement "Area of the circle is " will be displayed on the screen. The compiler will calculate the value of ‘PI * rad * rad’ and display it at the end (* is the multiplication operator in C++). The output for the above program is: Enter the radius 9 Area of the circle is 254.14 Bold indicates that the user entered the value. In this case 9 was entered as the radius. Suppose we type cout<< "rad"; the output will be just the word rad The value of the variable ‘rad’ will not be displayed. Remember: When you want to display some variable’s value on the screen, DO NOT ENCLOSE IT IN DOUBLE QUOTES; just mention the name of the variable after the insertion operator. ________________________________________ Initializing variables: It is a good idea to initialize variables at the time of declaration. Even if you are unsure of the value you can still initialize it to 0. If you are wondering why, just consider the example below: #include
int main( )
{
int correct, choice;
cout<<"\nEnter your guess of the lucky number: "; cin>>choice;
if (choice= =correct)
{
cout<<"\nCongrags. You are correct!"; } else { cout<<"\nSorry. Wrong guess"; } cout<
int main( )
{
char check;
int i;
cout<<"Enter the character that you want to convert to ASCII : "; cin>>check;
i = check;
cout<<"The ASCII value for "<
int main( )
{
int num1, num2;
cout<<"Enter the two numbers : "; cin>>num1>>num2;
cout<<"The product is : "<>num1>>num2;
This is a method of obtaining multiple inputs using a single statement. The above statement is equivalent to writing:
cin>>num1;
cin>>num2;
The two numbers, when entered by the user, can be separated by a space or by a new-line (i.e. the first number is typed and then after pressing the ‘enter’ key the second number is typed).
When you run the program you would get the following on your screen:
Enter the two numbers : 8 4
The product is : 32The sum is : 12The difference is : 4The quotient is : 2The remainder is : 0
Something is not right in this output; the results are correct but the display is on a single line. To display the output in an organized manner, the program should print each output on a new line. For this purpose of formatting the output C++ provides us with ‘escape sequence’.
________________________________________
Escape Sequences/ Backslash Character Constants
If you remember, whatever you type within double quotes following cout<<, will be printed as it is on the screen. There is a problem in case you want to print a new line, or you want to use tabs (because whatever you type within double quotes will be displayed directly on the screen). To solve this problem, escape sequences were developed. Just as the name implies, these escape sequence characters are used to escape from the normal sequence of events. An escape sequence always begins with a backslash ( \ ). For a new line, the escape sequence is \n (n for new line). If you want to push the tab setting then \t should be used (t for tab). The modified program for doing simple arithmetic operations is as follows: #include
int main( )
{
int num1, num2;
cout<<"Enter the two numbers : "; cin>>num1>>num2;
cout<<"\n The product is : "<
int main( )
{
char ch;
double db;
float f;
short int i;
db=55e4;
ch = i = f = db;
cout< , = = , ! = , >= , <= ) Relational operators are also binary operators (since they operate on two operands). They are used for comparing two values and the result of the comparison is either true (value 1) or false (value 0). Some examples are given below: 5>4 will return a value of True (1)
2>3 will return a value of False (0)
In programs that you write, comparisons will usually be made between one variable and a constant or between two variables. For example:
x>y
z>10
> means ‘greater than’ while >= stands for ‘greater than or equal to’.
x>=y
will yield a true value even if x = y whereas x>y will yield a value of false when x = y. Be clear as to what relation you want to test when using these operators.
Suppose you want to test whether two variables are equal, you have to make use of the equality operator. The equality operator is denoted by = = (double equal to signs).
Remember: Many beginners in programming use the equality operator and assignment operator interchangeably. The assignment operator is a single ‘equal to’ sign and it is meant only for assigning values to variables. The equality operator (a double ‘equal to’ sign) is used to check whether two values are equal.
Relational Operator Operation Performed Result of Operation
x>y Is x greater than y? True/False
x=y Is x greater than or equal to y? True/False
x<=y Is x less than or equal to y? True/False x==y Is x equal to y? True/False x!=y Is x not equal to y? True/False We’ll write a simple program for comparing two numbers and displaying the appropriate result. #include
int main( )
{
float num1, num2;
cout<<"Enter the two numbers : "; cin>>num1>>num2;

if (num1>num2)
{
cout<<"The number "<5. Do you think it is valid?
#include
int main ( )
{
int num;
cout<< "Enter the number"; cin>>num;
cout<<(num>5); //Legal?
return 0;
}
Always remember that the result of a comparison yields a value of TRUE (1) or FALSE (0). Hence the above program is perfectly correct. In case you enter a value that is greater than 5 you will get the output as 1 else you will get 0. 0 is considered as false and all other values are considered to be true.
C++ Operators - IV
The following topics are covered in this section:
• Logical Operators
• Unary Operators
5. Logical Operators - AND ( && ) OR ( || ) NOT (!)
These operators are used to combine two or more expressions. The way in which they combine the expression differs depending on the operation used. They are used when we need to test multiple conditions. For example you may write a program that has to check whether the marks scored by a student is greater than 70 and less than 80. If it is so then you will want the program to display a ‘B’ grade. To check whether the average mark is greater than 70 you have to use one expression and to check whether the average is less than 80 you should use another expression. Thus in simple English your statement will be:
If (average mark is greater than 70 AND average mark is less than 80)
Print "B grade"
AND: it combines two conditional expressions and evaluates to true only if both the conditions are true.
First Condition Second condition Result of AND operation
False False False
False True False
True False False
True True True
If the first condition and the second condition are both true then the result of the AND operation will also be true.





Example:

// To check whether the given number is even
# include
int main ( )
{
int num;
cout<< "Enter the number"; cin>>num;
if ( (num!=0) && ((num%2)= =0) ) // Two conditions have to be true
{
cout<<"\n Even Number"; } return 0; } In this program we need to check for two conditions (the number entered should not be zero and the number when divided by 2 should not produce a remainder). Only if both these conditions are satisfied should the program display that the number is even. The AND operator is used to combine the two conditions that are to be tested and if both are true then the message is displayed. OR: operator combines two conditions and evaluates to true if any one of the conditions is fulfilled (i.e. only one of the conditions need to be true). It is designated by using two parallel bars/pipes ( | | ). First Condition Second condition Result of OR operation False False False False True True True False True True True True The ‘AND’ and ‘OR’ operators can be used on a sequence of conditions (i.e. at a time you can check for multiple conditions). For example the following code is legal: if ( (x>y) && (y>5) && (z>y) && (x>4) )
{
//body of the ‘if’ condition…
}
In this case only if all the four conditions are true will the body of the ‘if condition’ be executed.
NOT: NOT is a unary operator. Unlike ‘AND’ and ‘OR’, NOT operates only on one operand. The logical value of the operand is reversed. If the operand is true, then after the NOT operation it will be become false. You might be thinking that the NOT operator can operate only on 1 and 0. Actually, any number greater than 0 will be considered as a true value. Hence the following would give:
• !5 will give 0
• !0 will produce 1
• !1 is equal to 0.
Condition Result of NOT operation
False True
True False
Basically, any number other than zero is considered as true. ‘Not’ of any number (other than 0) will give you FALSE (or zero). Check out the following program that illustrates the NOT operator.
#include
int main ( )
{
int num, result;
cout<< "Enter the number"; cin>>num;
result = (!num);
cout<
int main ( )
{
int a;
cout<
>=
Equality operator ==
Inequality operator !=
Logical Operator &&
||
Conditional operator ?:
Assignment =
Arithmetic assignment *=, /= , %= , += , -=
Beware: It is better to write long expressions using parentheses otherwise it could lead to a lot of confusion and also to potential logical errors.


Associativity
If two operators have a different priority level then their execution order depends on the operator precedence. What will happen if two operators have the same priority level?
When 2 operators in an expression have the same priority, the expression is evaluated using their associativity. Consider the statement:
net = basic + allowance – tax;
Both + and – have the same priority level. Almost all of the operators except the assignment (and arithmetic assignment) operators have associativity from left to right. The assignment operator has associativity from right to left. Since the + and – binary operators have an associativity of left to right the expression for ‘net’ is the same as:
net = (basic + allowance) – tax;
When you perform multiple assignments:
x = y = 0;
the associativity of the assignment operator (=) is taken into account and thus the order of evaluation will be:
y = 0 (which will give ‘y’ a value of 0) followed by x = y (which will assign 0 to ‘x’).
Comma Operator
The comma operator can accept two expressions on either side of the comma. When executed, the left side expression is first evaluated followed by the right side expression. Ultimately it is the expression on the right side that will be the value of the entire expression.
int x,y;
cout<<(x = 1, y = 5); // 5 will be displayed First the ‘x’ will be assigned 1 and then ‘y’ is assigned a value of 5. The comma operator is equivalent to saying "do this task and do this also". In this case the compiler will do: x = 1 and then y = 5 The rightmost expression is y = 5 and hence the value of the entire expression (x=1,y=5) is 5. Be careful while assigning the value of a comma separated expression to a variable. The comma operator has lower operator precedence than the assignment operator. If we type: y = (x = 1 , x = 5 , x + 10); x will have a value of 5 while y will be 15. If we type: y = x = 1, x = 5 , x+10; the value of ‘y’ will be 1 and that of ‘x’ will be 5. This is because the compiler will perform the following operations: y = x = 1; // y and x are set to 1 x = 5; //x value is 5 x + 10; //the value of the entire comma separated expression is 15 The comma operator will be used usually in ‘for’ loops. Bitwise Operator: The bitwise operators available in C++ are tabulated below: Bitwise Operator Symbol Function & AND individual bits | OR individual bits ^ EXOR individual bits ~ NOT of individual bits (or complement operator) >> Right-shift operator
<< Left-shift operator These operators will operate on individual bits of the operand (i.e. on the binary representation of data). These operators are dealt with in detail in Chapter 13. Remember: Do not confuse the && and & operators or the || and | operators (logical operators are entirely different from bitwise operators). Recap • Arithmetic operators are used to perform mathematical operations. • The modulo/remainder operator is applicable only on integer operands. • Escape sequences are character constants that are used to format the output displayed on the screen. • = is the assignment operator and the Rvalue is assigned to the target (or the Lvalue). • Information will not be lost when converting from a smaller data type to a larger data type. • Shorthand operators are a combination of one of the arithmetic operators and the assignment operator. • Relational operators are used to compare two values or expressions. They will return a value of true or false. • Logical operators are used to combine two or more expressions. • The operator precedence determines which operator has a higher priority. • When 2 operators have the same priority the expression is evaluated based on their associativity. • Bitwise operators are used to operate on individual bits. For Loops in Depth The following topics are covered in this section: • Introduction • For Loop Statement • More of For loops • Body of For loops • Nesting For loops Statement and Expression A statement in C++ refers to a part of the code that is terminated in a semicolon. It is the smallest part of the program that can be executed. For example: sum = a + b; is a statement. A C++ program will consist of a set of statements. An expression is a grouping of variables, constants, function calls and operators (arithmetic, logical, relational) to return a value. In the above example, a + b is an expression. While dealing with control mechanisms you will come across blocks of instructions. A block of instruction refers to a set of statements grouped together within a block. The block is identified by using the curly braces ‘{‘ to start the block and ‘}’ to end of the block. Program flow and control: What is program flow? Whenever we write a program we have to decide on the sequence of operations that the program has to perform. Generally this is represented in the form of an algorithm. Writing algorithms for simple programs might seem trivial but when you write complex programs, the algorithm helps reduce programming errors. Let’s take a look at a simple algorithm to calculate the average weight of 3 persons: Step 1: Start the program. Step 2: Obtain the weight of all 3 persons (in weight1, weight2, weight3). Step 3: Calculate the average weight as average = (weight1 + weight2 + weight3)/3 Step 4: Display the result Step 5: Stop the program By looking at the algorithm a programmer can easily write the entire program. When you have a complex program, if you have written an algorithm then you can eliminate potential logical errors in this stage itself. In the algorithm we define the way in which the program control should flow. First the program should get the 3 inputs from the user, then it should calculate the value for average and finally it should display the result. Thus we can say that the program flow has been clearly defined. But program flow needn’t always be so simple. You might need to take some decisions and alter program flow. Control refers to that part of the program which is currently being executed. Let’s write an algorithm to divide 2 numbers obtained from the user. Step 1: Start the program. Step 2: Obtain the 2 numbers (num and den) from the user. Step 3: Check if den is zero. If it is then go to step 4 else go to step 5. Step 4: Display “Denominator is zero. Cannot perform division”. Go to step 7. Step 5: Calculate the quotient by dividing num by den. Step 6: Display the result. Step 7: Stop the program. In this example, there are 2 routes the program can take. If the user enters the denominator as 0 then the error should be produced else normal division should be performed. The pictorial representation of an algorithm is called a flowchart. Loops Suppose you want to add the numbers from 1 to 10, what would you do? Of course you could write a long statement that would calculate 1+2+3+4+…+10. What if 10 were changed to 100 or 1000? Whenever there are statements to be repeated you can make use of loops (in fact you should make use of loops). When using loops, some condition should be specified so that the loop will terminate (otherwise the set of statements within the loop will keep executing infinitely). For loop statement For loop is used for performing a fixed number of iterations (or repetitions). The syntax is: for (initialize-variables; condition-to-test ; assign-new-values-to-variables) { //statements to be repeated (or the body of the loop) } The ‘for’ loop has three expressions that have to specified. Initialize variables: This expression is executed only once (when the program flow enters the loop for the first time). It is usually used to assign the loop variable an initial value. Condition to test: involves relational operators. It is executed each time before the body of the loop is executed. Only if the condition is true will the loop body be executed. If it is false then the loop is terminated and the control passes to the statement following the ‘for’ loop body. Assign new value to variable: It is executed at the end of every loop after the loop body. Usually it is used for assigning a new value to the loop variable. Example: // To find square of numbers from 1 to 10 #include
int main( )
{
int var;
for (var = 1 ; var<=10; var++) { //beginning of ‘for block’ cout<< "Square of "<
int main( )
{
int i = 2;
for ( ; i<8 ; i++ ) // No initialization of loop variable! { cout<
int main( )
{
int i;
for ( i = 1; i != 2 ; i++ )
{
cout<
int main( )
{
int x,y;
for (x=1,y=1; x<10,y<5 ; x++,y++) { cout<<"\n"<>letter;
If you want to check whether the user has entered a ‘y’ or an ‘n’ you can do the following:
if (letter = = ‘y’)
{
//body of if statement
)
You should not compare characters as shown below:
if (letter = = y) //WRONG
This method of comparing a character variable with a character constant is WRONG. It will yield an error. The character constant should always be enclosed in single quotes.
Beware: Beginners often forget the single quotes while using character constants.

//To find the square of a given number
# include
int main( )
{
char reply;
int num, square;
cout<< "Do you want to find the square of a number(y/n)? "; cin>>reply;
while (reply = = 'y') //No blank-space between the two equal signs. See note.
{
cout<<"\nEnter the number : "; cin>>num;
square = num*num;
cout<< "The square is : " <>reply;
}
return 0;
}
Note: A blank space has been left between the two ‘equal to’ symbols just to make it clear that there are two equal signs. Do not leave a space between the two = symbols when you write your program.
At the start of the program, if the user types a ‘y’ the program will execute the loop body. At the end of the while loop, the program will prompt the user to enter the value for ‘reply’. If the user again types ‘y’, then the program will execute the while body a second time. Hence, at the end of each loop the program obtains the value for ‘reply’ and checks whether the test condition is true. As long as the test condition is true, the while loop is executed over and over again.
The difference between the ‘while’ loop and the ‘for’ loop is that the ‘while’ loop does not have a fixed number of repetitions of the loop body. As long as condition is true it keeps executing the loop body. Usually the ‘while’ loop is used when the programmer is not sure about the number of iterations that need to be performed. The ‘for loop’ is used when the programmer knows how many iterations are to be performed. Of course, the use of both can be interchanged as well. In fact, a ‘for loop’ can be modeled into a ‘while loop’. The equivalent ‘while loop’ for a ‘for loop’ will be as below:
Initialize a variable value;
while (condition involving the variable)
{
//body of the loop
//assign a new value to the loop variable;
}
If we use the coding:
while(1= = 1)
{
//body of loop
}
the while loop becomes an infinite loop (because 1 is always equal to 1).
Do…While loop
The ‘do…while’ loop is a modification of the ‘while’ loop. If you noticed in the earlier program for the ‘while’ loop, initially we have to ask the user whether he/she wants to find the square of a number. Only if the user types a ‘y’ will the program enter into the ‘while’ loop. Hence in the program we have to ask the user twice whether he/she wants to square a number (once outside the loop and once inside the loop). The ‘do while’ loop, eliminates this repetition.
When you make use of the ‘do-while’ loop, the body of the loop will be executed at least once. After the first iteration, if the loop condition holds true then the loop is repeated again. Thus the first iteration is compulsory in the ‘do…while’ loop.
The program to find the square of a number can be re-written as follows:
#include
int main( )
{
char reply;
int num, square;
do
{
cout<<"\nEnter the number : "; cin>>num;
square = num*num;
cout<< "The square is : " <>reply;
}while (reply = = 'y');
return 0;
}
In this program, the body of the loop will be executed once for sure. After the first iteration, the user has the option of terminating the program or continuing to use the program for squaring another number.
Beware: when using the ‘do…while’ loop, make sure that you put a semicolon at the end of the while statement:
while (reply = = 'y');
Decision Statements (IF)
So far we have seen statements that help you in repeating a particular task as long as you desire. Another important form of program flow control is to be able to make a decision as to how you want the program to continue. Statements of this kind are referred to as decision statements (sometimes also called selection statements).
The following topics are covered in this section:
• If else
• Nested If
• Conditional Operator

• If…Else If…else…
‘if-else’ is one of the decision statements available in C++. This enables the programmer to provide different paths for the program flow depending on certain conditions. For example: consider a program for dividing two numbers. Division by zero will lead to an error. To avoid this from happening, after obtaining the two numbers from the user the programmer will want to ensure that the denominator is not zero. Hence there needs to be two different program flow options. If the denominator is zero a message saying, "Division not possible" should be displayed. Otherwise the program should carry out the division and display the results. In simpler terms this can be stated as:
If the denominator is zero, then display a message and do not divide
else perform division and display the output.
Syntax:
if (condition)
{
//statements to be executed;
}
else
{
//statements to be executed;
}
The program flow is as follows: If the condition being tested within the ‘if’ statement is true, then the body of ‘if’ will be executed otherwise the body of ‘else’ will be executed. Thus the program takes a decision as to what it should do next.
You can also make use of the ‘if’ statement alone (‘else’ is not compulsory) or you can even make use of a series of ‘if...else’ statements as follows:
if (condition1)
{
//body
}
else if (condition2)
{
//body
}
else
{
//body
}



Example:
// To find the greater among two numbers
#include
int main( )
{
int a, b;
cout<< "Enter the two numbers you want to compare : "; cin>>a>>b;
if (a = =b)
{
cout << "The two numbers are equal"; } else if (a>b)
{
cout <b)
Even if this is not satisfied then only will it go to the next statement, which is an ‘else’ statement. Since ‘a’ was not greater than ‘b’, and equality was also tested earlier the only possibility is that ‘b’ is greater than ‘a’. Thus if the first two conditions have failed then the program flow will go to the body of the ‘else’ block.
Remember: Using the ‘if…else..if’ format, only one of the bodies will be executed (not all). If one condition is satisfied the rest of the ‘else..if…else’ is ignored.
When we use the ‘if..else if’ construct, if one of the conditions is satisfied that corresponding body will be executed and the rest will be ignored. But what would happen if we use a series of ‘if’ statements alone?
#include
int main( )
{
char letter;
cout<<"\n Enter the alphabet 'a' or 'A': "; cin>>letter;
if (letter= ='a')
{
cout<<"\n You entered an 'a'"; } if (letter= ='A') { cout<<"\n You entered an 'A'"; } return 0; } In the above program, the compiler will check for ‘a’ first and then it will check for ‘A’ also. Even if the first condition is satisfied, it will still check for the second ‘if’ condition. In such cases it would be better to use the following: if (letter= ='a') { cout<<"\n You entered an 'a'"; } else if (letter= ='A') { cout<<"\n You entered an 'A'"; } Now if the user enters an ‘a’ then the program will not enter the ‘else if’ statement to check whether the letter is an ‘A’. Remember: It is better to make use of ‘if-else-if’ instead of a series of ‘if’ statements because in that way your program need not check all the conditions unnecessarily. And whenever you use a series of ‘if-else-if’ statements, test the condition that is most likely to be true first (so that the program need not waste time in checking more conditions). Nested If You can have an ‘if’ statement within another ‘if’ statement. This is known as nested ‘if’. The program flow will enter into the inner ‘if’ condition only if the outer ‘if’ condition is satisfied. In general form nested ‘if’ will be of the form: if (condition1) { //code to be executed if condition1 is true if (condition2) { //code to be executed if condition2 is true } } There can be more than one ‘if’ condition (i.e. you can nest as many ‘if’ statements as you want). An example code fragment is given below (‘month’ and ‘year’ are integer variables whose values are obtained from the user): if (month= =2) { cout<< "The month is February"; if ( (year%4) = = 0) { cout<< "This month has 29 days."; } else { cout<< "This month has 28 days."; } } In the above code fragment, only if the variable ‘month’ is equal to 2 will the program enter into the ‘if’ block. Within this block it will print: The month is February Then it will check as to whether the given year is a leap year or not. If it is a leap year then it will display that the month has 29 days else it will say the month has 28 days. Empty ‘if’ statement: We have seen the use of empty ‘for’ statements but empty ‘if’ statements might not be useful. In fact empty ‘if’ statements are usually logical error (because the programmer places the semi colon by mistake at the end of the ‘if’ statement). Example: if (num1b?10:20;
The value of y will be 20 because ‘a’ is not greater than ‘b’. This sort of expression can be written using the ‘if’ statement as:
a=5;
b=6;
if (a>=b)
{
y = 10;
}
else
{
y = 20;
}
Beware: It’s just that you could reduce the amount of coding by using the ternary operator. Be careful that you don’t confuse the logic while trying to reduce the length of the code!
Decision Statements (Switch Case)
There are many instances wherein you may want to test for a series of conditions one after the other. For example: Suppose you obtain the input of month from the user in as a number and you want to display the corresponding name of the month; what would you do? You could write a series of twelve ‘if….else if….else if…else if…’ statements.
The ‘switch…case’ format provides a convenient alternative to using multiple ‘if-else’ statements when you are testing for a single variable alone. The switch statement successively tests the value of a variable against a list of integer or character constants. If a match is found then the statement associated to that particular case will be executed.
First of all, before entering the switch case statement, we have to obtain the value of the switch variable from the user. The switch variable refers to the variable whose value you want to check. In the case of converting numbers into corresponding months, the switch variable will be the month number.


Syntax:
switch (variable/expression that evaluates to an integer)
{
case char/integer-constant :
{
//body
}
case char/integer-constant :
{
//body
}
}
For example:
# include
int main ( )
{
int month;
cout<< "Enter the month of the year: "; cin>>month;
switch (month) // month is the switch variable
{
case 1 : // if month is 1 then the statements are executed
{
cout<<"The month is January"; break; } case 2 : { cout<<"The month is February"; break; } //write case statements for 3 to 10 just as shown above case 11 : { cout<<"The month is November"; break; } case 12 : { cout<<"The month is December"; break; } default : // If value of day is something other than 1 to 7 { cout<<"You entered an invalid number"; break; } } return 0; } As you can see, the compiler gets the value of the variable ‘month’ from the user. This is called the ‘switch variable.’ If the value of ‘month’ is 1, then the compiler performs what is specified under case 1. If the user enters 2, then the output will be February and so on. Since a year has only 12 months, if the user types 0 or 14 then the program should display that the user has typed the wrong number. The ‘default’ statement is used for this purpose. The ‘default’ statement provides the program with an option to do something in case the switch variable does not match any of the case constants. In this program, ‘month’ is called the switch variable and the integer constants from 1 to 12 are called case constants (i.e. they are the values for which the switch variable is tested). Break: It causes an exit from the switch construct (body). For instance, after printing that "The month is January" you don’t want the compiler to go into the other cases. Hence you ask it to break out of the ‘switch…case’ body. If there is no break for each case then the program will perform all the remaining cases as well. Try it: Remove all the break statements from the above program and execute your program. If you now type a value of 1, the program will print: The month is January The month is February …and so on… Beware: A mistake that beginners commit is that they tend to forget the ‘break’ statement. Always use ‘break’ after each case and also make sure that you have a ‘default’ option in your ‘switch…case’ body. The ‘default’ case is executed only if the user enters a value other than the case constants. It does not matter whether you place the default case at the starting of the switch-case body or at the end (but usually programmers prefer to place the default case at the end of the switch-case construct). Suppose you want to test the switch variable against a set of character constants then ensure that you enclose your case constants within single quotes. Suppose the switch variable ‘month’ is a character then the program would be: switch (month) { case 'a' : //‘a’ is within single quotes because it is a character constant { //body of the case break; } //the remaining cases } Beware: There can be an expression in the switch part but it should evaluate to an integer. The switch variable and the case constants should be integers (or characters). You should not use other data types. Controlling flow within a loop statement We’ve seen a few ways of looping within a program. There are a few occasions when you might want to break out of a loop when some particular condition occurs. Or in other words you may not want to go through the all the iterations within a ‘for loop’. Or you may want to break out of the loop for just one particular value of the loop variable. C++ provides a mechanism to break from loops using the ‘break’ and ‘continue’ statements. These are also called as ‘jump statements’. The following topics are covered in this section: • Break • Continue • Go To • Return • Apply what you've learnt • Recap of the entire Unit Break We’ve already seen the use of the ‘break’ statement in the ‘switch…case’ construct. A ‘break’ statement can also be used to terminate (or break out) from a loop (like the ‘for’ and ‘while’ loops). Suppose you are using nested loops, then a ‘break’ specified in the body of the inner loop will lead to breaking out from the inner loop alone. #include
#include
int main ( )
{
int x, i;
for (i = 0; i<15; i++) { x = rand( ); if (x>500)
{
cout<<"\n Iteration number "<
int main ( )
{
int i;
i = 1;
LOOP : if (i<10) { cout<
int main( )
{
char ans;
cout<<"Enter y/n : "; cin>>ans;
if (ans=='n')
{
goto done;
}
int x=5; //ERROR
done:
cout<
int main( )
{
int d,m,y,days;
cout<<"\nEnter the Date (DD MM YYYY): "; cin>>d>>m>>y;
switch(m) //To print the month
{
case 1:
cout<<"\nJanuary"; days=31; break; case 2: cout<<"\nFebruary"; break; case 3: cout<<"\nMarch"; days=31; break; //…write the remaining cases case 11: cout<<"\nNovember"; days=30; break; case 12: cout<<"\nDecember"; days=31; break; default: cout<<"\nInvalid month"; return 0; //quit the program! break; } cout<<" "<
void add ( ); // Function Declaration
int main( )
{
add( ); // Function Call
return 0;
}
void add( ) // Function header
{
int num1,num2,sum; // Body of the function
cout<< "Input the two numbers : "; cin>>num1;
cin>>num2;
sum = num1 + num2;
cout<<"The sum is "<
void add( )
{
int num1,num2,sum;
cout<< "Input the two numbers : "; cin>>num1;
cin>>num2;
sum = num1 + num2;
cout<<"The sum is "<
void add (int var1, int var2) // Parameters var1 and var2
{
int var3;
var3 = var1 + var2;
cout<<"The sum of the two numbers is "<>num1;
cin>>num2;
add(num1,num2); //Arguments num1 and num2
return 0;
}
Two arguments have been passed to add ( ) from the main ( ) function. Observe the function ‘declarator/header’. The parameter specified is ‘int var1’ and ‘int var2’. This means that in the function named ‘add’, ‘var1’ and ‘var2’ are integers that are used within the body of the function. Within the function body we have declared a third integer called ‘var3’.
var1 and var2 are added and stored in var3 using the statement :
var3=var1 + var2;
Now the question might arise, what are the values for var1 and var2? In the main ( ) function the value for two integers ‘num1’ and ‘num2’ are obtained from the user. After obtaining the values, the following statement is encountered:
add(num1,num2);
This statement is the function call. Two arguments namely ‘num1’ and ‘num2’ are passed through this function call. Since the function was declared and defined as having two parameters (and since the data types of the parameters and the arguments are the same), the program will execute the code of the add ( ) function with the value of ‘var1’ being that of ‘num1’ and value of ‘var2’ being equal to ‘num2’. If the data types or number of arguments passed do not match with the parameters, the compiler will produce an error message.
Effectively, var1=num1 and var2=num2. The body of the function ‘add’ is now executed with the values of ‘var1’ and ‘var2’. Thus the arguments are passed from the program (which is the main ( ) function) to the parameters of the function ‘add’.
The variables used to hold the argument values are known as parameters. The function declarator in the function definition specifies both data type and name of parameters. In our example, the parameters were ‘num1’ and ‘num2’. Their data types were int (integer).

Remember: The values of ‘num1’ and ‘num2’ are passed to ‘var1’ and ‘var2’. The function is operating only on ‘var1’ and ‘var2’ (it does not operate on ‘num1’ and ‘num2’).
If we had declared the add ( ) function in the program, it would have been as follows:
void add (int, int);
In the function declaration you don’t have to specify the parameter names (just specifying the data types of the parameter is sufficient).
Remember: You pass arguments to parameters.
A closer look at Functions
The following topics are covered in this section:
• Default Arguments
• Return Values
• Returning void
• int main and void main ( )
• exit( )
• using return to break out from loops
Default Arguments
While using functions with parameters, you can specify default argument values (i.e. in case arguments are not passed to the function then the function will assign the default values to the parameters).
#include
void add (int var1=5, int var2=10) // default values 5 and 10
{
• int var3;
var3 = var1 + var2;
cout<
int add (int , int); // Declaration
int main ( )
{
int sum, var1, var2;
cout<< "Enter the two numbers"; cin>>var1>>var2;
sum = add(var1,var2); //Function call
cout<< "The sum of the two numbers is "<
void convert(int dollar)
{
if (dollar<0) { cout<<"\nCan't convert negative amount!"; return; } cout<<"\nThe equivalent money in Rupees is : "<<46*dollar; } int main( ) { int amount; cout<<"\nEnter the amount in dollars you want to convert: "; cin>>amount;
convert(amount);
return 0;
}
The output when you type a positive value is:
Enter the amount in dollars you want to convert: 5
The equivalent money in Rupees is : 230
The output when you enter a negative value is:
Enter the amount in dollars you want to convert: -1
Can't convert negative amount!
As can be seen from the output in the second case, once the return statement is encountered the program will stop executing the function (thus it does not calculate the value of 46*dollar if the value of dollar is negative).
void main ( ) and int main ( )
The main( ) function can be written in one of two ways. Either you can use:
void main ( )
or you can use:
int main ( )
{
//code
return 0;
}
int main ( ) returns an integer value while void main ( ) doesn’t return anything. Consider the following program:
# include
int main ( )
{

int test;
cout<<"Enter a value :"; cin>>test;
if (test= =1)
{
cout<<"You typed 1"; return 1; } cout<<"This line is displayed if you typed a value other than 1"; return 0; } What do you think happens when the user types 1 as the value of ‘test’? Since ‘test’ is equal to 1, the compiler will go into the body of if. It will display "You typed 1" on the screen. Then the compiler will return a value of 1. Generally a return value of a non-zero number is used when errors are encountered. You can't use this return value anywhere else in your program (Why? Because the caller for the main ( ) function is your operating system). In other functions (functions other than ‘main’ ), you can make use of the return value. After returning the value, the program exits. This means that the compiler will not even read the remaining few lines and hence nothing else will display on the screen. The compiler will quit once any integer has been returned by the main ( ) function. If you type anything other than 1 for test, the compiler will skip the ‘if’ body, display the last statement and then return 0. Better to use int main ( )? We could now delve one step further into the topic. Which one is better? Or which one should we use and why? Void is generally used to declare functions that do not return values. When you use int main ( ), the main function returns an integer to the operating system (since the OS is what calls the program and in turn the main function). Returning a value from main ( ) is like an exit function. The value is returned to the operating system and the program terminates. What will the OS do with your returned value? Actually, the OS never wants to know what you return. The program which started your program might need to know. In any OS, a program when running is called a "process". When booting up, the operating system starts the first process (called "init" in UNIX systems). Thereafter the first process starts other processes such as a shell (shell is just a program, which reads commands from the user and converts it to system calls). So when you write a program and execute it in the shell, the shell starts your program. So now, the shell is the parent process of your program (and your program is the child of the shell). Now, in the same way, suppose you want one of your programs to load another program to do a particular job and you want to know just whether the job was successful or not, then the OS gets the exit code of the child process and gives it to the parent process; just like returning from a function. So it is a standard to give the exit code as '0' for a success and any non-zero integer for an error. When programming in C/C++ you can give this exit code when you return from the main function. So you've to declare main as 'int main( )' to do that. If you declare it as 'void main( )' C++ wont allow you to set a value to be returned to your parent program. So the variable which should contain the return code will be filled by nothing, which means that memory can be in any state (unpredictable). Hence you have no control on what value your parent process gets when your child program exits. This is not just for UNIX, it holds good for MSDOS and Windows too. In the UNIXes the shell is usually 'sh' or 'bash'. In MSDOS the shell is called 'command.com'. In Windows the shell is 'explorer.exe'. Hence it's always better to use int main ( ) along with a return value at the end of the main ( ) function. A return value of zero indicates a normally terminated program. A non-zero value is used in case of errors. Of course this does not mean that a program written with void main ( ) won’t work; but it is better to avoid writing such programs. exit() There is a function called exit ( ) which can be used to terminate (or break out from) the program. The function is: void exit (int r); where ‘r’ is an integer value returned to the OS. Usually a value of 0 is used to indicate normal termination while a non-zero number is used in the case of abnormal termination (due to errors). Thus, the syntax (or function call) will be: exit (0); or exit (1); Instead of using integers we can also use two predefined macros: EXIT_SUCCESS (which is equivalent to 0) or EXIT_FAILURE (which is a non zero number). The exit ( ) function is declared in the stdlib.h library file. Hence you should type: #include
in case you want to use this function.
________________________________________
Using ‘return’ to break out from loops
The ‘return’ statement will return program flow control to the caller and this feature can be used to break out from loops. An example program is given below:
#include
int display( )
{
int i;
for (i=0;i<10;i++) { if (i!=5) { cout<<"\n"<
int main( )
{
int choice;
cout<<"\nWelcome to prog1"; cout<<"\nEnter 1 to skip 2nd program:"; cin>>choice;
if(choice==1)
{
return 5;
}
else
{
return 0;
}
}
Our second program just displays a statement to indicate that we are executing the second program.
//prog2.cpp – This program simply displays a statement on the screen
#include
int main( )
{
cout<<"\nCongrags.You are in the second program!"; return 0; } Build the two executable files: prog1.exe and prog2.exe. Note: If you are using VC++, the exe files will be created in a folder called “Debug” within your project. Turbo C++ will create the exe files in the same folder itself. So, now we have 2 programs but how do we conditionally execute the 2nd one depending on the return code of the first? The answer lies in batch programming (if you are using DOS) or in shell programming (if you are using Unix/Linux). We’ll deal with batch programming. This is basically the creation of *.bat files (so far we’ve been creating *.exe files). I won’t go into this topic deeply but I’ll cover a bit of it so that you can appreciate return codes from programs. Before starting to write a *.bat file, you can copy the 2 exe files we created (prog1.exe and prog2.exe) into the same folder (I’ve copied them into my C:\). I assume that you have used a bit of MS-DOS (at least you should be familiar with the command prompt, changing directories etc.). Command prompt: To get to this from Windows go to START->RUN and type “command” in the pop-up box. You’ll be taken to the DOS prompt (this is the place from where you can give commands to DOS). The prompt on my system is:
C:\MYWIN\Desktop>
Now type cd\ to go to C:\
C:\>
Let’s create a batch file named combo.bat. To do this simply type:
C:\>edit combo.bat
You’ll be taken to an MS-DOS text editor (similar to Notepad in Windows). Type the following in the file, save it and return back to DOS.
@ECHO OFF
ECHO **** WELCOME TO BATCH PROGRAMMING ****
ECHO Executing the prog1
prog1
IF errorlevel 5 GOTO end
prog2
:end
ECHO End of batch file
Perhaps everything seems weird?
Remember: MS-DOS command.com is not case-sensitive (i.e. typing DIR or dir is the same).
The first 3 lines of our batch file are used for the purpose of displaying something on the screen (equivalent to cout<< in C++). On the 4th line we say: prog1 This is equivalent to executing the program ‘prog1.exe’ on the command prompt. Batch files are used basically to execute a set of instructions/programs in a particular sequence. If every time you log into the system, you want to perform 10 commands, then each time you’ll have to keep typing the 10 commands on your prompt. You can save time (and also needn’t worry about remembering the sequence of commands) if you write those 10 command in a batch file. Now, each time you log into the system, you just need to type the name of the batch file and voila! (all your 10 commands will be executed faithfully). Coming back to our program, prog1 in the batch file will execute our first program. You’ll see the display: Welcome to prog1 Enter 1 to skip 2nd program: 1 Let’s assume the user types 1. According to our program: if(choice==1) { return 5;| } Now prog1 returns a value of 5 to the caller. The caller is our batch program combo.bat. The next line of the batch program checks for this return code. IF errorlevel 5 GOTO end The IF condition becomes true if the previous step had a return value of 5 or greater. In our case the previous step was the execution of ‘prog1’ and this returned a value of 5. Thus the condition is true and the combo.bat will go to the section labeled ‘end’. Here we just display a statement saying that it’s the end of the batch program (echo is used to display on the screen). If the user had entered some other value then the batch file would have executed prog2 since the return value would have been less than 5 (in our case it was 0) and so the IF condition would be false. The output if you entered 1 would be: C:\>combo
**** WELCOME TO BATCH PROGRAMMING ****
Executing the prog1
Welcome to prog1
Enter 1 to skip 2nd program:1
End of batch file
C:\>
The output if you entered some other number would be:
C:\>combo
**** WELCOME TO BATCH PROGRAMMING ****
Executing the prog1
Welcome to prog1
Enter 1 to skip 2nd program:3
Congrags.You are in the second program!End of batch file
C:\>
Just follow the above steps, create the batch file and try it out on your system. To execute batch files you don’t need to compile the file (i.e. combo.bat can be directly executed). This is because the command prompt is an interpreter (it executes commands one at a time and does not require compilation as we do for C++ programs).
In Unix/Linux, this is called shell programming (and instead of batch files we call them shell scripts). In large applications, you would need to execute a series of programs everyday and these are written in shell scripts. The execution of programs would depend on the return code of the previous program (if the previous program failed because of some error then you might not want to continue execution of the remaining programs). By now you should have understood about the difference between void main( ) and int main( ).
Note: If you are using Unix/Linux then refer to the Appendix for the above section.



A closer look at Functions
The following topics are covered in this section:
• Types of Functions
• Function Overloading
• Apply what you've learnt
Types of Functions
There are two types of functions namely:
1. Library Functions
• The declaration of library function is in the header file specified at the beginning of the program.
• The definition is in a library file that is automatically linked to the program
• Declaration and definition are not required. We will only call the function. Example : rand( ), clrscr ( ), exit( ) etc.
2. User Defined Functions
• Declaration and definition are part of the source file (*.cpp file).
• Function definition and declaration have to be written by the programmer.
• Example of user defined functions is the add ( ) function that we used in the previous section.
There are many functions which are provided by the compiler. These functions that come with the compiler are called as library functions. The prototype for the library functions will be present in some header file. To use a library function, you only need to include the header file (where the function prototype exists) and should know the name of the function.
It is common for programmers to write coding for functions and then use them later in some other program when needed. In fact in C programming, this was quite common. You can do the same in C++ as well. You could define a set of general-purpose functions in a header file and include the header file in the programs where you want to use those defined functions (by specifying a function call statement). But using functions in this way can lead to some problems, which will be discussed later (the main problem is when there is a clash of function names). In C++ we make use of ‘classes’ and reuse classes, rather than functions directly.
Using library functions: rand( ), srand ( ), time( )
There may be instances wherein you will want to generate numbers randomly. For example if you are simulating a game played with dice then you should be able to produce numbers between 1 to 6 randomly (corresponding to the 6 faces of a dice). Or if you want to simulate the tossing of a coin you should be able to retain the randomness of the event (i.e. these are events which you can’t predict. It could be a head or a tail).
We can make use of the rand ( )library function to perform such tasks. The rand ( ) function is defined in the stdlib.h library. Let us write a program to toss a coin and ask the user for his/her choice. If the user’s choice and the result of the toss are the same then the user wins the game.
#include
#include
int main( )
{
int move;
char choice;
do
{
cout<<"Enter 1 for head and 0 for tail: "; cin>>move;
if (rand( )%2= =move)
{
cout<<"You win."; } else { cout<<"You lose."; } cout<<"\n\nDo you want to play again? "; cin>>choice;
}while(choice= ='y');
return 0;
}
The rand ( ) function will generate integers upto a maximum of 32767. But in the case of tossing a coin we have only two possibilities (a head or a tail). To scale down 32767 to two cases we divide the rand ( ) value by 2 and use the remainder (the remainder will either be 1 or 0). Thus we assume that 1 is head and 0 means tail in the above program. The output for the above program will be:
Enter 1 for head and 0 for tail: 1
You win.
Do you want to play again? y
Enter 1 for head and 0 for tail: 1
You win.
Do you want to play again? y
Enter 1 for head and 0 for tail: 1
You lose.
Do you want to play again? n
If you run the program again a second time the result will be:
Enter 1 for head and 0 for tail: 1
You win.
Do you want to play again? y
Enter 1 for head and 0 for tail: 1
You win.
Do you want to play again? y
Enter 1 for head and 0 for tail: 1
You lose.
As you might have noticed the sequence is the same (i.e. each time the program is run the same set of random numbers are produced). This is because the random number is generated in a sequence and unless you ask the computer to start the sequence from a different position it will always keep starting at the same place. We have another function called srand( ) than can be used to alter the start of the sequence. To do this just add the satement:
srand( time(0) );
before the ‘do’ statement. The function time ( ) is defined in ‘time.h’ header file. The output will now be:
Enter 1 for head and 0 for tail: 1
You win.
Do you want to play again? y
Enter 1 for head and 0 for tail: 1
You lose.
If you run the program there is a good chance of getting a different result for the same inputs. This is because we are setting the random generator differently each time the program is run. The function time (0) will give the present system time in seconds. Each time you run this the time (0) value will be different.
Suppose you want to simulate the throw of a dice then you should scale down the outcomes of the rand( ) function to 6. For this (instead of rand( )%2, you could use the statement:
( ( rand( ) % 6 ) + 1 )
We have to add 1 because rand ( )%6 will produce values between 0 and 5 but we need values from 1 to 6.
Function Overloading
Can two functions have the same name?
More than one function can have the same name but they should have different number of parameters or the types of the parameters should be different. This is known as function overloading. The name of a function along with its parameter data types forms the function signature. The signature helps differentiate between two functions. In function overloading the signatures of the functions with the same name will be different.
In general, overloading is the process of assigning several meanings to a function (or an operator as in operator overloading, which we will discuss in a later chapter). Consider the example given below:
// A PROGRAM TO ILLUSTRATE FUNCTION OVERLOADING
void display( ); // Function declaration – This function has no arguments
void display (char); // One argument
void display (char, int); // Two arguments
int main( )
{
display ( );
display (‘=’);
display (‘+’, 30);
return 0;
}
void display( )
{
cout<< "Hi!"; } void display (char ch) { cout <
#include //to make use of the system("CLS"); function
int menu( ); //provide a menu for the user
void fibo( ); //function for generating fibonacci series
void fact( ); // to find the factorial of a number

int main ( )
{
int ch;
while (1= =1) //an infinite loop which can be broken
{
system("CLS"); //to clear the screen before the menu is displayed
ch=menu( );
switch (ch)
{
case 1:
fibo( );
break;
case 2:
fact( );
break;
case 3:
cout<<"\n Program Terminated."; break; default: cout<<"\n Invalid choice."; break; } if (ch= =3) { break; //break from while loop } } return 0; } //All the function definitions below: int menu( ) { int choice; cout<<"\n\n\n Welcome to my Program"; cout<<"\n 1.) Generate a Fibonacci Series."; cout<<"\n 2.) Find the factorial of a number."; cout<<"\n 3.) Exit."; cout<<"\n\n Enter your choice : "; cin>>choice;
return choice;
}
void fibo( )
{
int max, sum, a1, a2;
cout<<"\n\nHow many terms do you want in the series? "; cin>>max;
a1=1;
a2=1;
cout<<"1,1"; for (int i=2;i>num;
for (i=num;i>0;i--)
{
result = result*i;
}
cout<<"\n\n The factorial of "<
int fact(int n)
{
int result;
if (n= =1)
{
return 1;
}
else
{
result = n * fact(n-1);
return result;
}
}
int main( )
{
int num;
cout<<"\nFor what function do you want to find the factorial : "; cin>>num;
cout<<"\n\n The result is : "<
using namespace std;
void backwards( )
{
char ch;
ch=getchar( );
if (ch!='\n')
{
backwards( );
}
cout<
inline double dollartors (double d) // The function is inline.
{
return 47*d; // Inline functions usually contain only one/two body lines
}
int main ( )
{
double dollar;
cout << "How many dollars :"; cin>>dollar;
cout<< "Rupees : "<>dollar;
cout<< "Rupees : "<< 47*d; //Inline function body substituted return 0; } The advantage is that the program needn’t save its present memory address, go to the function’s memory address, execute the function and return back to the original address. Instead the function code is brought into the main program and it is executed just like a normal C++ statement. Thus processing time can be reduced significantly. Remember: It is useful to make a function inline if its body consists of just one or two lines. Recap • A function is a group of statements written for a specific purpose and grouped within a single block. • A function has to be declared if it is called before being defined. • Arguments are passed to a function’s parameters. • The data type of the arguments should match the data type of the parameters. • If a function is declared as returning a value to the caller then it should return the corresponding data type value (unless it returns void). • The return statement is used to return a value to the caller as well as give back program control to the caller. • The main ( ) function returns value to the OS. • Overloaded functions should have the same name but different parameters. • Return data type cannot be used as a basis for function overloading. • Inline functions are used when the body of the function is very small (one or two lines). • Recursive functions are functions that call themselves. Some provision has to be provided for them to break out of recursivity. • Recursive functions have more overheads and are generally not used. More On Data Types and Variables The following topics are covered in this section: • Scope of Variables • Storage Classes Scope of variables Scope refers to the region where something is valid or the region where something can exist. Variables in C++ have a defined scope, which depends on the way they are declared. There are three places where a variable can be declared: as local variables, formal parameters and global variables. Remember: In C we can declare variables only in the starting of the function. In C++, we can declare variables anywhere within the program. Local Variables Variables declared within a function are called local variables (sometimes called automatic variables). These variables can be used only within the block (or function) in which they are declared. A block starts with an opening curly brace and ends in a closing curly brace. A local variable is created upon entry into the block and destroyed when the program exits that block. If you create a variable in the main ( ) function then it can be used only within the main ( ) function. That variable cannot be accessed by some other function that you may have created. For example: void test ( ) { // Start of block int q; q = 2; } // End of block void test2 ( ) // Start of another block { int q; q = 5; } The two q's declared in the two functions (test and test2) have no relationship with each other. Each q is known only within its own block (since it is a local variable). The main advantage is that a local variable cannot be accidentally altered from outside the block. Try it: Compile the following piece of code in your compiler and check the results int main( ) { int outer; { int inner; cout<<"enter the outer variable value : "; cin>>outer;
}
cout<<"\n Enter inner variable value : "; cin>>inner;
return 0;
}
What do you think will happen? The above program will lead to a compile-time error. Variables are visible only within the block of code where they are declared (unless they are global variables). The coding enclosed within the two braces is called as a ‘block’ of code. Thus:
{
int inner;
cout<<"enter the outer variable value : "; cin>>outer;
}
is a block of code within which we have declared an integer variable ‘inner’. This variable ‘inner’ is not visible outside of this block. Thus the statement:
cin>>inner;
will lead to an error because ‘inner’ is only known inside this block and not outside the block.
Note the difference between the two codes given below:
int main( )
{
int i=5;
{
int i; //This ‘i' is only visible within this block.
i=6;
}
cout<
int count; // count is a global variable
void increment( )
{
count=count+2;
}
int main ( )
{
count=1;
• increment( ); //count is now 3
count=count+1; //count is now 4
}
The above program is not complete but you can see that two functions (increment and main) can access the same variable ‘count’.
count=1;
The increment ( ) function increases the same count value by 2. Now count is 3 and finally the main ( ) function increases count by 1. The final count value is 4. This is a case of more than two variables accessing the same global variable.
Remember: Global variables will take up more memory because the compiler has to always keep it in memory. Avoid using too many global variables. Use it only if the variable is going to be used by a number of functions.
What would happen if a program uses an identifier as both local and global variable name? Consider the program below:
#include
int var=55; //Global variable
int main( )
{
int var=20; //Local variable with same name
cout<<"\nGlobal variable value is : "<<::var; var=var+1; cout<<"\nLocal variable value is : "<
#define PI 3.14 //Macro definition
int main( )
{
cout<
int main ( )
{
int test[20]; //we assume that the maximum limit is 20 numbers.
int size,i,j;
int temp;
cout<<"How many numbers do you want to compare : "; cin>>size;
cout<<"Enter the numbers you want to check : "; for(i = 0; i>test[ i ];
}
for(i = 0; itest[j] )
{
temp = test[ i ];
test[ i ] = test[ j ];
test[ j ] = temp;
}
}
}
cout<<"The numbers in ascending order are : "; for (i = 0; i
int main ( )
{
char name[15];
cout<< "Enter your name : "; cin>> name;
cout<< "Your name is "<
int main ( )
{
char name[20];
cout<<"Enter your full name : "; cin>>name;
cout<<"You entered your full name as : "<
int main( )
{
char name[10];
cout<<"Enter the name: "; cin.get(name,10); cout<
int main( )
{
char name[80];
cout<<"Enter the name: "; cin.get(name,80,'*'); cout<> a[ i ][ j ];
}
}
The outer ‘for’ loop starts with a value of 0.
i = 0
Corresponding to i=0 we will have two values for j (0 and 1). Hence with this you can get the values for a[0][0] and a[0][1]. This corresponds to the first row of the matrix. For the second row, the ‘i’ loop will execute a second time with a value of 1. Hence you can get the values for a[1][0] and a[1][1].
In the cout statement we have mentioned ‘i + 1’ and ‘j + 1’. This is just for the purpose of display. Remember that the compiler will start numbering from zero. The first element for the compiler will be the 0 x 0 element. For the user it is better if you refer to the first element as 1 x 1 rather than referring to it as 0 x 0.
Similarly, two ‘for’ loops can be used to display the values of a two-dimensional array.
Initializing multi-dimensional arrays:
Initializing a 2-D array is similar to that of a 1-D array.
int marks[2][3]={ 40,50,60,
70,80,90};
The above notation is used for readability. You might as well initialize the array as:
int marks[2][3]={ 40,50,60, 70,80,90};
Thus if you use the following initialization (hoping that marks[0][2] will be 0):
int marks[2][3]={ 40,50,
70,80,90};
the compiler will treat it as:
int marks[2][3]={ 40,50,70,80,90};
and marks[0][2] will be 70 while marks[1][2] will be 0. Only trailing elements will be automatically initialized to 0. There is a better way to initialize multi-dimensional arrays. The following initialization:
int marks[2][3]={
{40,50},
{70,80,90}
};
actually produces the result we were looking for earlier. Now, marks[0][2] is zero. The additional pair of parentheses makes a big difference. Readability is improved further and now the compiler will set the trailing elements (which haven’t been initialized) in each row to 0. In our case, only the 3rd element of the first row is missing and hence this is initialized to zero.
You might recollect that in one dimensional arrays we could specify:
int a[] = {1,2,3};
and the compiler would translate this into:
int a[3] = {1,2,3};
The question arises as to whether we can extend this to 2-D arrays as well:
int marks[ ][ ]={
{40,50},
{70,80,90}
};
This will give a compile-time error. The compiler wouldn’t know how many columns you want to specify for the array (you’ll understand this concept when we deal with pointers and 2-D arrays in the next chapter). But for the time being remember that you can forget the 1st dimension but shouldn’t leave out the subsequent ones.
Let’s go one step further. What is a 3-D array?
int a[5];
int ab[2][5];
int abc[3][2][5];
‘ab’ is a 2-D array which consists of 2 one dimensional arrays (each of which can hold 5 elements). ‘abc’ is a 3-D array which consists of 3 two dimensional arrays (each of the 2-D arrays contains a 1-D array which can hold 5 elements). The concept can be extended to higher dimension arrays as well (but generally we wouldn’t use more than 3 dimensions).
How do we initialize a 3-D array?
int abc[3][3][2]={
{
{40,50},
{10,70},
{20,30}
},
{
{45,55},
{15,75},
{25,35}
}
};
The parentheses make the initialization pretty clear. Of course you can remove all the braces but the declaration wouldn’t be easy to understand. Again in the case of a 3-D arrays, you can drop the first dimension but should mention the other 2.
The following declaration is legal:
int abc[ ][3][2]={
{
{40,50},
{10,70},
{20,30}
},
{
{45,55},
{15,75},
{25,35}
}
};
You’ll have to take care of the braces. In the above example:
abc[0][0][0] = 40
abc[0][0][1] = 50
abc[0][1][0] = 10
abc[0][1][1] = 70
abc[0][2][0] = 20
abc[0][2][1] = 30
What happens in the following declaration?
int abc[ ][3][2]={
{40,50},
{10,70},
{20,30},
{45,55},
{15,75},
{25,35}
};
All that we’ve done is removed the braces which were used to denote that the 1st dimension was 2. But the compiler isn’t smart enough to know what’s on our mind and now it would create the array as: int abc[5][3][2].
abc[0][0][0] = 40
abc[0][0][1] = 50
abc[0][1][0] = 0
abc[0][1][1] = 0
abc[0][2][0] = 0
abc[0][2][1] = 0
abc[1][0][0] = 10
abc[1][0][1] = 70
abc[1][1][0] = 0
abc[1][1][1] = 0
abc[1][2][0] = 0
abc[1][2][1] = 0
and so on.
Moral of the story is that you should do your best to make things explicit when dealing with computers rather than assume that the computer would think the way you’re thinking.
Passing an Array to a Function
This topic is dealt with in depth when discussing about pointers. A simple method of passing arrays to functions is illustrated below: #include
void disp(int a[ ] )
{
for (int i=0;i<3;i++) { cout<
#include //needed for using the manipulator setw ( )
int main( )
{
int i , j , r1 , r2 , c1 , c2 , a[20][20] , b[20][20] , c[20][20];
cout<<"Enter the number of rows and columns of first matrix :"; cin>>r1>>c1;
cout<<"Enter the number of rows and columns of second matrix :"; cin>>r2>>c2;

// If the matrix orders are not equal then addition is not possible
if ( (r1! = r2) || (c1!=c2) )
{
cout<>a[ i ][ j ];
}
}
for (i = 0; i>b[ i ] [ j ];
}
}
cout<>a1.name;
cout<<"Enter the city : "; cin>>a1.city;
cout<<"Enter the telephone number : "; cin>>a1.tel;
cout<<"\nThe size of the structure variable is : "<>a1.name;
cout<<"Enter the city : "; cin>>a1.city;
cout<<"Enter the telephone number : "; cin>>a1.tel;
cout<<"Enter the date of birth (day, month and year) : "; cin>>a1.birthday.day>>a1.birthday.month>>a1.birthday.year;
cout<<"\nThe size of the structure variable is : "<
union shirt
{
char size;
int chest;
int height;
};
int main( )
{
shirt mine;
cout<<"\nSize of the union is : "<>mine.size;
cout<<"\nThe size is : "<>mine.chest;
cout<<"\nThe size is : "<>mine.height;
cout<<"\nThe size is : "<
int main ( )
{
int var=100;
cout<<"Value : "<
int main( )
{
int marks[3];
int* p;
p = &marks[2]; // Pointer points to the third element of array.
marks[0]=58;
marks[1]=61;
marks[2]=70;
p = p-1; //pointer decrements by one, goes to the previous integer address
cout<
void clear(int *point, int size)
{
for (int i=0;i
int square (int x)
{
return x*x;
}
int main ( )
{
int num = 10;
int answer;
answer = square(num);
cout<<"Answer is "<
void square (int *x)
{
*x = (*x) * (*x);
}

int main ( )
{
int num = 10;
square(&num);
cout<<" Value of num is "<
int main( )
{

int x;
int &ref = x; //ref is a reference variable
x=5;
cout<
void swap (int &x, int &y) //pass by reference
{
int t;
t = x;
x = y;
y = t;
}
int main ( )
{
int a , b;
cout<<"Enter the value for a : "; cin>>a;
cout<<"Enter the value for b : "; cin>>b;
cout<<"a and b before swap are : "<
int* create( )
{
int marks[3];
int *pt=marks;
for (int i=0;i<3;i++) { marks[i]=80; } return pt; } int main( ) { int *p; p=create( ); cout<
int main ( )
{
int size,i;
cout<<"Enter the size of the array : "; cin>>size;
int marks[size]; //WRONG
cout<<"\nEnter the marks: "; for (i = 0; i>marks[i];
}
cout<>size;
with:
size=4;
Now everything should be fine. Is it so? Try it and you’ll get the same compiler error. The compiler reads and stores the value of 4 for ‘size’ but it still will not substitute that value in:
int marks[size];
The compiler assumes that ‘size’ is a variable (since it was declared like that) and since a variable’s value can change in the program, it will not compile the code. One way to correct this is by declaring the maximum size of the ‘marks’ array by saying:
int marks[4]; //program will compile
There is no need for the variable ‘size’ and the user can enter only a maximum of 4 values because that is the space allocated for the array ‘marks’.
Another way to correct the program is to declare the variable ‘size’ as a constant.
const int size=4;
Now you can use:
int marks[size];
The reason that this is valid is because the compiler makes note of the fact that ‘size’ is a constant and has been given a constant value 4. Since this value will never change in the program and space will be allocated for 4 elements in the array ‘marks’. C programmers made use of macros (instead of ‘const’):
#define SIZE 4
to define constants. When the compiler comes across the term ‘SIZE’ anywhere in the program it will simply substitute the value of 4 for ‘SIZE’.
But whatever you do, the compiler limits you to fixing the size of the array at compile-time. Can you decide the array size at run-time? Dynamic allocation comes to the rescue.
Dynamic allocation means allocating (or obtaining) and freeing memory at run-time. There are certain cases where run-time decisions are better. For example, deciding the size of an array. Similarly you can free up allotted memory in your program when you don’t need a particular array by deleting the entire array itself. The two operators used for this purpose are: ‘new’ and ‘delete’.
‘new’ is used to allocate memory while ‘delete’ is used to free the allocated memory (memory which was allocated by ‘new’). The free memory available is sometimes referred to as the heap. Memory that has been allocated by ‘new’ should be freed by using ‘delete’. If you don't use ‘delete’ then the memory becomes a waste and cannot be used by the system. This is called memory leak (i.e. when allocated memory is never returned to the heap). You could go on taking memory from the heap till it gets exhausted. This will lead to memory leak and can cause problems to your program and to other programs which attempt to take memory from the heap.
The syntax is:
data-type * name = new data-type;
delete name ;
Beware: Both data types should be the same.
Example:
int * p = new int;
delete p;
Remember: delete p;
means that the data pointed to by ‘p’ is deleted. The pointer ‘p’ will not get deleted. The following coding is correct:
int m = 20;
int *p = new int;
*p=5;
cout<<*p<>size;
int *marks = new int[size];
cout<<"\nEnter the marks: "; for (i = 0; i>marks[i];
}
cout<
int sum(int a, int b)
{
return (a+b);
}
void func(int d, int e, int (*p1)(int,int))
{
cout<<"The result is : "<<(*p1)(d,e); } int main( ) { int (*p)(int,int); p=sum; func(5,6,p); return 0; } The output is: The result is : 11 Though the program is simple a few statements might appear confusing. The statement int (*p) (int,int); declares a pointer to a function that has a return value of integer and takes two arguments of type integer. sum( ) is a function with return data type of integer and also with two arguments of type integer. Hence the pointer ‘p’ can point to the function sum ( ). Thus we assign the address of the function sum ( ) to ‘p’: p = sum; We’ve also defined another function called as ‘func ( )’ which takes two integer arguments and a third argument which is a pointer to a function. The idea is to pass the function sum ( ) to the function ‘func( )’ and call the sum ( ) function from func ( ). func(5,6,p); will call the func ( ) function and it will pass pointer ‘p’ to func ( ). Remember that p is a pointer to sum ( ). Hence in reality we are actually passing a function as argument to another function. Let’s expand the program one step further by creating another function called product( ) which will return the product of the two arguments. #include
int sum(int a, int b)
{
return (a+b);
}
int product(int x,int y)
{
return (x*y);
}
void func(int d, int e, int (*p1)(int,int))
{
cout<pin=60004;
p->tel=23451;
p=p+1; //Now points to record[1]
p->pin=50023;
p->tel=89732;
p=record;
cout<pin;
cout<tel;
p=p+1; //Points to record[1]
cout<pin;
cout<tel;
return 0;
}
The output is:
The pincode is : 60004
The tel. no. is : 23451
The pincode is : 50023
The tel. no. is : 89732
You’ll notice that to access the individual elements we have made use of different operators. When you use pointers, you should not use the dot operator. Instead we make use of the arrow operator (->).
p->pin=60004;
Actually, the dot operator (or the member operator) can be used but you have to be careful about operator precedence. To use the member operator we’ll have to dereference the pointer and then use it. The following expression:
*p.pin
would be wrong. The dot operator is a post-fix operator and it has higher precedence over the dereferencing operator (which is a pre-fix operator). So to set it right we will have to use:
(*p).pin
Thus we could also have used the following code in our program:
cout<>id;
cout<<”Salary of that employee is:”<
#include
/* This function can accept varying number of integer arguments.
It will sum the arguments and return the result.
*/
int sum(int a, ...)
{
va_list args;
va_start(args,a);
int result=a;
for(; ;)
{
int temp=va_arg(args,int);
if (temp==0) //can also check for NULL
{
break;
}
else
{
result+=temp;
}
}
va_end(args);
return result;
}
int main( )
{
cout<<"\nThe sum is : "<