Unit-2 BCE Notes
Unit-2 BCE Notes
…………………………………………………………………………………………………………………………………………………….
Introduction to Algorithms, Complexities and Flowchart, Introduction to Programming, Categories of
Programming Languages, Program Design, Programming Paradigms, Characteristics or Concepts of OOP,
Procedure Oriented Programming VS object-oriented Programming.
Introduction to C++: Character Set, Tokens, Precedence and Associativity, Program Structure, Data Types,
Variables, Operators, Expressions, Statements and control structures, I/O operations, Array, Functions.
………………………………………………………………………………………………………………………………………………………...
2.1 Algorithm: An algorithm is a finite set of steps which describes step-by-step procedure to solve a
particular computational problem.
Algorithm exhibits following properties
I. Algorithm takes input.
II. Algorithm process data on supplied data and produces output.
III. Algorithm consists finite number of steps.
IV. Algorithm is written in English like language which is called pseudo code.
V. Algorithm is written prior to implement problem in some programming language.
VI. Algorithm is useful to calculate the efficiency of solution.
The Algorithm is :
1. Read a, b, c
2. Check if ( a>b)
3. { then check if(a>c)
4. { then print a
5. otherwise print c
}
6. otherwise
7. { check if (b > c)
8. then print b
9. otherwise print c }
10. Exit
2.4
Arrow: To represent flow of sequence of statements.
START
Read
a, b, c
No Yes No No
Print c a>c a>b b>c Print c
Yes Yes
END
Print a Print b END
END END
2.5 Complexity:
Complexity is a measure of an algorithm. It determines the amount of time and space necessary to execute
them. The computational problem can be solved by different algorithms, Calculation complexity helps to
select efficient algorithm.
Types of Complexity:
Time Complexity: Time require to solve a computational problem is called Time complexity of the
algorithm.
Space Complexity: The space requirement of a computational problem is called Space complexity.
Introduction to Programming
2.6. Programming Language:
A programming language is an artificial language designed to communicate instructions to a computer.
Programming languages are used to create application and system software.
There are many programming languages viz. C,C++,Pascal, Java, Python, BASIC, FORTRAN,COBOL, and LISP
are just a few. These are all high-level languages. One can also write programs in low-level languages called
assembly languages, although this is more difficult. Low-level languages are closer to the language used by
a computer, while high-level languages are closer to human languages.
Compiler and Interpreter:
Eventually, every program must be translated into a machine language that the computer can understand.
This translation is performed by compilers, interpreters, and assemblers.
When you buy software, you normally buy an executable version of a program. This means that the
program is already in machine language it has already been compiled and assembled and is ready to
execute.
Main Program
The top-down design support modular approach of the program design by separating the lower-level
program from higher level program.
In this approach we move top to bottom of a program
Here function is basic unit
This approach is used when we have a good idea of final result but we have very less details about
each section
This approach mainly focus on “ How the task Done ” i.e procedure Ex – C , Fortan , VB
Procedure Oriented Programming
Microsoft Excel can wrap text so it appears on multiple lines in a cell. You can format the cell so the text
wraps automatically, or enter a manual line break.
Notes:
Data in the cell wraps to fit the column width, so if you change the column width,
data wrapping adjusts automatically.
If all wrapped text is not visible, it may be because the row is set to a specific
height or that the text is in a range of cells that has been merged.
1. Select the cell or range for which you want to adjust the row height.
2. On the Home tab, in the Cells group, click Format.
3. Under Cell Size, do one of the following:
To automatically adjust the row height, click AutoFit Row Height.
To specify a row height, click Row Height, and then type the row height that you
want in the Row height box.
Tip: You can also drag the bottom border of the row to the height that shows all
wrapped text.
Step 1: Start
Step 2: Read Principle Amount(PA), RateOfInterest(ROI), Time
Step 3: SI= ((PA*ROI*Time)/100)
Step 4: Print SI
Step 5: Stop
#include<iostream>
using namespace std;
class average
{ private:
int a,b,c;
float avg;
public:
void getIntegers();
void calculateAvg();
void displayAvg();
};
void average::getIntegers()
{ cout<<"Enter three integer numbers:";
cin>>a>>b>>c;
}
void average::calculateAvg()
{ avg = (a+b+c)/3.0;
}
void average::displayAvg()
{ cout << " Calculated average is :"<<avg<<endl;
}
int main()
{ cout<<"This is Object Oriented Programming"<<endl;
cout<<"Average of three integer numbers "<<endl;
average a1;
a1.getIntegers();
a1.calculateAvg();
a1.displayAvg();
cout<<"************ End of Program ************"<<endl;
return 0;
}
Introduction to C++
C++ is object-oriented programming language. It is extension of C programming language. It is developed
by Bjarne Stroustrup in the early 1980s at Bell Laboratories. C++ provides many features that more than of
the C language, but more importantly, it provides capabilities for object-oriented programming.
2.13 Tokens: Smallest individual units in a program are knows as token. They are categorized into keywords,
identifiers, Literal (constants), operator etc.
2.14 Keyword: They are reserved words of a language and can-not be used for any other purpose other
than they are defined. Reserved words have predefined meaning in program. Keywords can -not be used
for name of identifier. Some comments used keywords in c++ are
2.15 Identifier: It refer to names of variable, function, array class etc. Identifier are created by programmer
in the program.
Integer Constants: Integer constants are the whole number without any fractional part. C++ allows three
types of integer constants.
Decimal integer constants: It consists of a sequence of digits and should not begin with 0 (zero). For
example, 124, - 179, +108.
Octal integer constants: It consists of a sequence of digits starting with 0 (zero). For example. 014, 012.
Character constants: A character constant in C++ contain one or more characters and enclosed in single
quotation marks. For example, 'A', '9', etc. C++ allows nongraphic characters which cannot be typed directly
from the keyboard, e.g., backspace, tab, etc.
Floating constants
They are also called real constants. They are numbers having fractional parts. They may be written in
fractional form or exponent form. A real constant in fractional form consists of signed or unsigned digits
including a decimal point between digits. For example, 3.0, -17.0, -0.627 etc.
String Literals
A sequence of characters enclosed within double quotes is called a string literal. String literal is by default
(automatically) added a special character ‘\0' which denotes the end of the string. Therefore, the size of the
string is increased by one character. For example, "COMPUTER" will be re-represented as "COMPUTER\0" in
the memory and its size is 9 characters.
2.17 Variables
It is a location in the computer memory which can store data and is given a symbolic name for easy
reference. The variables can be used to hold different values at different times during the execution of a
program.
int a; // Declaration of variable of type int.
a = 10; // initialization of variable a;
float b = 2.5; //Declaration and initialization of variable b of type float.
2.18 Operators: Operators are special symbols used for specific purposes. C++ provides various types of
operators. Assignment operator, Arithmetical operators, Relational operators, Logical operators, Unary
operators, increment / decrement operators, Conditional operators, size of operator.
Following are operators which are defined in C++ but not in C.
:: Scope resolution operators
. dot operators
new Memory allocation operator
delete Memory delete operator
→ Arrow operator
Operator precedence determines which operator is performed first in an expression with more than one
operator with different precedence. For example, 10 + 20 * 30 is calculated as 10 + (20 * 30) and not as (10
+ 20) * 30.
Operator of higher precedence executed first.
Operator Associativity determine direction of evaluation of expression when two or more operators of
same precedence appear in an expression. Associativity can be either Left to Right or Right to Left. For
example, ‘*’ and ‘/’ have same precedence and their associativity is Left to Right, so the expression “100 /
10 * 10” is treated as “(100 / 10) * 10”.
(Please refer text book for complete list of operators and their precedence/ Associativity)
2.20 Data Types: Data types are used to declare variable for type of value they can handle. C++ supports
many data types. The built-in or basic data types supported by C++ are an integer, floating point, and
character. These are summarized in table along with description and memory requirement
Type Byte Range Description
Int 2 -32768 to +32767 Small whole number
long int 4 -2147483648 to +2147483647 Large whole number
-38 +38
Float 4 3.4x10 to 3.4x10 Small real number
-308 +308
double 8 1.7x10 to 1.7x10 Large real number
long double 10 3.4x10-4932 to 3.4x10+4932 Very Large real number
char 1 0 to 255 A Single Character
2.21 Expression: Combination of operators, Constant and variables which produce a particular type of
value is called expression.
I. Constant Expression: 20 + 5/2
II. Integer Expression: Produce integer type value result.
III. Float Expression: Produce float type value.
IV. Relational Expression: Produce result of type boolean (true or false)
V. Logical Expression: Combining two or more relational expression by logical operator which produce
boolean type value.
2.22 Statements
Statements are the instructions given to the computer to perform particular action. Action may be in the
form of data movement, decision making etc. Statements form the smallest executable unit within a C++
program. Statements are always terminated by a semicolon.
a. Selection or Conditional Statements: In this type of structure the execution of group of statements
depends on the test condition. If condition is true then one group of statement get executed otherwise
other group of statements get execute.
Following are statements create selection structure
•if statement
•if else statement
•nested if statement
•switch statement
b. Iterative or Looping Statements: This type structure provide facility to execute some group of
statements for certain number of times. The number of iterations depends upon test condition. Group of
statements executed again and again till test condition remains true. Following are looping statements in
C++
a. for loop b. while loop c. do-while loop.
a. for loop
It is a count-controlled loop in the sense that the program knows in advance how many times the loop is to
be executed.
syntax of for loop for (initialization; decision; increment/decrement)
{
statement(s);
}
The flow diagram indicates that in for loop three operations take place:
• Initialization of loop control variable
• Testing of loop control variable
• Update the loop control variable either by incrementing or decrementing.
Operation (i) is used to initialize the value. On the other hand, operation (ii) is used to test whether the
condition is true or false. If the condition is true, the program executes the body of the loop and then the
value of loop control variable is updated. Again, it checks the condition and so on. If the condition is false, it
gets out of the loop
b. while loop
Syntax of while loop
while(condition)
{
statement(s);
}
The flow diagram indicates that a condition is first evaluated. If the condition is true, the loop body is
executed and the condition is re-evaluated. Hence, the loop body is executed repeatedly if the condition
remains true. As soon as the condition becomes false, it comes out of the loop and goes to the statement
next to the ‘while’ loop.
c. do-while loop
Syntax of do-while loop
do
{statements;
} while (condition);
Note: That the loop body is always executed at least once. One important difference between the while
loop and the do-while loop the relative ordering of the conditional test and loop body execution. In the
while loop, the loop repetition test is performed before each execution the loop body; the loop body is not
executed at all if the initial test fails. In the do-while loop, the loop termination test is Performed after each
execution of the loop body. Hence, the loop body is always executed least once.
Object oriented
preprocessor directives
class class_name
{ private:
data members;
public:
member functions;
};
// member functions definitions;
int main()
{ class_name obj // object creation
obj.member function ;
return 0;
}
2.27 Function
A function is a block of code which is written to specify certain course of action. It has special property that
it is reusable that it can be executed as many different points of program as required. To execute the
function, it has to called in the program.
A function has
a. Name
b. Return value or void
c. Parameter list
3. Function Call
Function must be called if it has to be executed. When a function is called, the program flow of control
transferred to function definition, body of function is executed and control returns to the statement
following the function call.
Syntax
1. function_name(parameter values);
2. variable = function_name(parameter values);
2.28 Array
Array is derived data type. An array is collection of similar data items.
An array is used to handle multiple value of same type by single variable, these multiple values, also called
array element, are placed in consecutive memory location. Each element of array is referenced by single
variable the name of array and index. An array has four things array name, index, elements and size of
array.
Syntax
An array is declared as
data_type array_name[SIZE];
int number[10]; This is an array of name number and can store 10 int data items.
2. Multidimensional array: It is also collection of similar data items where each data items is also an array
of fixed size array. For example, Two-dimensional array is simple case of multidimensional array where each
element is a one-dimensional array itself. Therefor two-dimensional array is collection of one-dimensional
arrays.
*********