Module 1
Module 1
A typical C++ program is made up of several functions which may be contained in one or
more sources files. Every C++ program must have a function named main which is
where program execution always begins. Each source file is compiled separately, and
then all are linked together to form the executable program. Quite often declarations or
functions in one source file are referenced in another source file. In order for the
compiler to be aware of the external references, "include" files are usually used.
Examples of commonly used "include" files are stdio.h, conio.h, iostream.h, iomanip.h,
math.h, and string.h.
1.
#include <iostream.h>
#include <math.h>
include other library files here if any(from the system or created by the programmer
himself)
void main()
{
statements... }
2.
#include <iostream.h>
#include <math.h>
include other library files here if any(from the system or created by the programmer
himself)
void main()
{
statements... }
When functions are used in the program, the programmer may first declare(let the system
know that you are about to construct functions) them or may construct directly the
functions(or define the functions). In the first program structure, the functions first() and
second() are first declared. They are defined after the main program. Although
functions can be defined before the main function. This is seen in the second program
structure. It is necessary therefore that if you are not yet ready to define your functions
they should first be declared.
Every program consists of different elements. Here are the different elements in C++.
C++ ELEMENTS
• Reserved Words - words that have special purposes and may not be used as program
identifiers; examples: case, char, short, if, void, while, do, int
• Constants - program elements that do not change their value during the course of
program execution; types: integer, character, float, string, enumeration, symbolic;
examples: 56, -9, 5.6, 'a', "abcdef"
• Integer constants - one word in length, whole numbers only; may either be short, long,
signed, or unsigned; examples: 31165, 100000, -7612
• Character constants - delimited by single quotes; examples: '\n' - new line, '\t'-
horizontal tab, '\"'- double quote, '\b' - backspace, 'v', '1', '#'
• Floating point constants - decimal numbers with an optional signed integer exponent
specified with either e or E; the number may also be suffixed by f, F, l or L to specify
float or long double; the default is double; example: 90.34, -34.01, 10000.87398
• Symbolic constants - handled by the C++ preprocessor; defined using the #define
construct and const qualifier; examples: #define SIZE 21, const int x = 30;
BASIC TYPES
#include <iostream.h>
void main()
{
char cCh;
cout << " Enter a character: "; cin >> cCh;
cout << "Entered character is: "<< cCh << endl;
}
The program simply asks a character to be entered and will be displayed on screen. The
program uses the include file "iostream.h". Without this file the compiler can not
recognize the cout and cin. cChr is a variable of type char and used to hold the character
to be entered. cout is used for displaying and cin is used for reading data. The operators
"<<" and ">>" are used with cout and cin respectively. endl is short for end-of-line.
When used with cout, the cursor of the output screen is moved to the next line. It means
that if there are five endls there will be five blank lines.
The int type specifies an integer whose size depends on the particular machine. It may
be short or long.
#include <iostream.h>
void main()
{
long int a = 100000, b = 100000, sum = 0;
sum = a + b;
cout << "Sum is: "<< sum << endl;
}
The program computes for the sum of a and b. With this kind of values long int is used
instead of integer or short int. The type int or short int cannot be used because the values
are quite big including the sum. If the sum is between -32K and 32K, short int or int can
be used. In this case short int can not be used because the sum is 200, 000. It is also very
important to consider that variables especially output variables should be initialized. That
is why in the example sum is first set to 0. sum = a + b computes for the sum.
The types float and double specify single precision and double precision floating point
numbers respectively. The type qualifier long may be applied to double to specify
extended-precision floating point.
#include <iostream.h>
void main()
{
double a = 13.821, b = 3.712, prod = 0;
prod = a * b;
cout << "Product is: "<< prod << endl;
}
This program displays the product of two numbers. Again the output variable prod is set
to 0. The product is determine by the formula prod = a * b. The asterisk(*) is the
multiplication operator.
There is also a special type called void. It is usually used to declare a function that has no
return value or takes no arguments.
#include <iostream.h>
void display()
{
cout << "This is just an example on"
<< endl
<< "to use the type \"void\"."
<< endl; }
void main()
{
display();
}
The program has two functions: main and display. The display() function is called in
main(). It simply displays the text " This is … "void" ". It doesn't return any value and
takes no arguments. \" inside the display() function is used to display double quotes.
OPERATORS
• Mathematical - * / % + -
• Logical - && ||
• Equality - == !=
• Assignment - = += -= *= /= %=
To better understand how the different operators function, let us assume that we are given
three variables a, b, and c with the following values 6, -8, and 4 respectively. Let us
examine how the following outputs are obtained in the different expressions below.
1. b * (c + 10) = -112
Although the multiplication operator is of higher priority the expression (c + 10) will be
evaluated first. This expression is enclosed by a pair of parenthesis, hence 14. (c+10) =
14 * -8 = -112.
2. c / 3 * 10 = 10
Multiplication and division operators belong to the same level. There is a rule that if two
or more operators belonging to same level are used in an expression, they are evaluated
from left to right. This rule is known as the left to right rule. In the given expression, c /
3 will be evaluated first. The output is 1 since c is an integer. c/3 = 1*10=10.
3. a % c = 2
The modulus operator(%) is used to compute for the remainder. Therefore, a % c = 6 %
4=2.
4. a && 2 = 1
A pair of ampersands is the AND operator. When used it may return 0 or 1. 0 means
FALSE and 1 means TRUE. Numbers and expressions are treated as statements. A
number equal or greater than 1 or less than 0 is evaluated as TRUE, otherwise FALSE.
In logic, given an AND compound proposition or statement is TRUE if all simple
propositions are true. a is equal to 6 and it is greater than 1. a in the expression is
therefore TRUE. The expression a && 1 = 2 && 2 = 1 && 1 = 1.
5. a || 0 || -9 = 1
A pair of pipes is the OR operator. When used it may return 0 or 1. 0 means FALSE and
1 means TRUE. Numbers and expressions are treated as statements. A number equal or
greater than 1 or less than 0 is evaluated as TRUE, otherwise FALSE. In logic, given an
OR compound proposition or statement is TRUE if at least one statement is TRUE. a is
equal to 6 and it is greater than 1. a in the expression is therefore TRUE. The expression
a || 0 || -9 = 2 || 0 || -9 = 1 || 0 || 1 = 1.
6. (b + 8) || 0 && (a - 2) = 0
The expression (b + 8) will be evaluated first. Thus, we obtain 0. This is followed by (a
- 2) and the output is 4. && is of higher priority than ||. So the expression (b + 8) || 0
&& (a - 2) = 0 || 0 && 4 = 0 || 0 = 0.
7. ++a * 3 / 3 + c++ = 11
++a will be evaluated first. The variable a has now a value of 7. This is an example of a
pre-increment operation. a is then multiplied to 3 giving 21. 21 is now divided by 3
giving 7. 7 is then added to c giving 11. There's no change in the value of c because the
assigning is done first before adding unlike for ++a. c++ is a very good example of a
post-increment operation.
8. --c * 3 / 3 + a-- = 9
--c will be evaluated first. The variable c has now a value of 3. This is an example of a
pre-decrement operation. c is then multiplied to 3 giving 9. 9 is now divided by 3 giving
3. 3 is then added to a giving 9. There's no change in the value of a because the assigning
is done first before subtracting unlike for --c. a-- is a very good example of a post-
decrement operation.
{
statement1;
statement2;
}
• If
Syntax: if (expression)
statement/s...
Example: 1. if ( a > b)
cout << "a is greater than b " << endl;
2. if ( a > b)
{ a = a * b;
cout << "the value of a is " << a << endl;
}
where a and b are variables.
In (1) if the expression "a > b" is TRUE then the line "cout << "a is greater than b " <<
endl;" will be executed otherwise this statement will be bypassed. In (2), a group of
statements will be executed if the expression is evaluated as TRUE.
• If..Else
Syntax: if (expression)
statement/s...
else statement/s...
2. if (a == 0) // if true
cout << "a is exactly equal to 0" << endl; // do this
else if (a < 0) // if true
cout << "a is a negative number " << endl; // do this
else cout << "a is a positive number " << endl; // last option
• Switch
Syntax:
switch (expression) {
case constant_expression: statement/s
case constant_expression: statement/s
/* as many cases as needed... */
default: statements
}
• Loops
3 loop statements:
1. While
Syntax:
while (expression)
statement/s
Example: a = 1;
while ( a <= 10)
{ cout << a << endl; a ++; }
The first value of a is 1. Therefore the expression (a <= 10) is TRUE. The value of a
will then be displayed. a will increment and evaluated again. The display of a will be
terminated when the expression (a <= 10) will become FALSE. That is if a reaches 11.
2. For
Syntax:
a. for (expression1; expression2; expression3)
statement/s...
b. expression1;
for (; expression2; expression3)
statement/s...
c. for (expression1; expression2; )
expression3;
statement/s...
d. expression1;
for ( ; expression2; )
expression3;
statement/s...
3. Do-While
Syntax:
do {
statement/s...
} while (expression);
FUNCTIONS
• subroutines/modules that do specific tasks
• may either be built-in or user defined
• supports structured programming, and OOP
• through it, the divide-and-conquer style of programming is empowered
• format:
type name( parameter; argument)
{ statement...
}
where type is the type the function returns, name is the name of the function, parameter is
the information the function receives, argument is the information that is being passed to
the function and statement is the list of statements the function executes
BUILDING FUNCTIONS
#include <iostream.h>
int my_val(int x)
{ int iVal = 0;
iVal = (100 + x) * 10;
return iVal;
}
void main()
{int iX;
cin >> iX;
cout << "My Value: " << my_val(iX) << endl;
}
----------
Drill/s:
- For Statements -
1. Construct a program that will compute for the sum of the first ten positive even
numbers. Sum of the first ten negative numbers. Sum of the first ten negative even
numbers. (use for, while, do while statements).
2. a. Design a program that will determine if the value entered by the user is odd or
even.
b. If the value entered by the user is odd and divisible by 5.
3. Design a program that will compute for the value of x in the given equation below.
The variables a, b, and c are to be entered by the user.
10b^2 (a-b)*c
x = ------------ + 6 ----------
3ac 10
b. 0123456789
012345678
:
0
6. Prepare a program that will determine if the value entered by the user is numeric or
alphabetic. If alphabetic, the computer should also determine if it is lower case or upper
case.
7. Design a program that will determine the lowest and the highest of 10 numbers
entered by the users.
8. Construct a menu driven program that outputs the sum, difference, product and
quotient of 2 entered numbers. Let the items below be the different options:
1 - Sum
2 - Difference
3 - Product
4 - Quotient
5 - Exit
- For Functions -
3. Write a function that will compute for the value of ( a^2 + b^2 + c^2) ) / 3 where a is
17, b is 18 and c is 21.