[go: up one dir, main page]

0% found this document useful (0 votes)
35 views15 pages

Unit-2 BCE Notes

The document covers fundamental concepts of computer engineering, including algorithms, programming languages, and programming paradigms. It introduces C++, detailing its character set, tokens, and the differences between procedure-oriented and object-oriented programming. Additionally, it explains algorithm complexity, flowcharts, and programming design methodologies such as top-down and bottom-up approaches.

Uploaded by

ayushchohan633
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views15 pages

Unit-2 BCE Notes

The document covers fundamental concepts of computer engineering, including algorithms, programming languages, and programming paradigms. It introduces C++, detailing its character set, tokens, and the differences between procedure-oriented and object-oriented programming. Additionally, it explains algorithm complexity, flowcharts, and programming design methodologies such as top-down and bottom-up approaches.

Uploaded by

ayushchohan633
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Department of Computer Science & Engineering

BT205 Basic Computer Engineering


B. Tech, EC/ME/CE/AI&DS (I Semester)
Unit -2

…………………………………………………………………………………………………………………………………………………….
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.

2.2 Algorithm to find the largest number in given three numbers.


Problem: To find the largest number.
Input: Three numbers a, b and c
Output: Largest number.

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.3 Flow Chart:


A flowchart is a type of diagram that represents an algorithm or process, showing the steps as boxes of
various kinds, and their order by connecting them with arrows. This diagrammatic representation illustrates
a solution to a given problem. Therefore, Flow chart is a pictorial and structured representation of the
solution of problem.
Commonly used symbol in constructing a flow chart is

Rectangle with rounded ends: To represent START and END of algorithm

Parallelogram: To represent INPUT/READ and OUTPUT/ PRINT operation of algorithm

Rectangle: To represent processing/ operations of the instruction.

Diamond: To represent condition / branching.

2.4
Arrow: To represent flow of sequence of statements.

Flowchart to find the largest number in given three numbers.

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

Fig 2.2 Example of Flow Chart

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.

2.7 Category of Programming Language: Accordingly, to the history of development, programming


languages are categorised into machine language, assembly language, High level language
I. First Generation language: Machine Language:
 The program is written in binary language.
 The program is directly processed by CPU.
 It does not require any translator or compiler.
 The program is difficult to understand and difficult to debugs as it is written in binary mode.
 The program is machine dependent.
 It requires to have good knowledge of hardware to write program in machine language.
 It requires more time to write program.
 It is also called low level programming language.
 It requires very less amount of memory.

II. Second Generation Language: Assembly languages


 The language consists more friendly code called Mnemonics. For example, ADD, MOV, CMP etc.
 The program is not process directly by processor.
 It requires translator called assembler to convert program written in assembly language into machine
language.
 It is somewhat easier to understand and debug the assembly program.
 The program is machine dependent.
 The program needs to have little knowledge of architecture of processor.
 It requires less time as compare to machine language to write a program.
 It is also considered as low-level language.
 It requires less amount of memory.

III. Third Generation Language: High level Language.


 The English like construct is used to write a program.
 The program written in HLL is not directly processed by processor.
 It requires an intermediate software called compiler to convert program written in HLL into machine
language.
 The program in machine independent.
 Programmer need not to have knowledge about hardware or architecture of processor.
 It is very easy write and debug the program.
 FORTRAN, C, C++, JAVA are examples of HLL.
 It requires large amount of memory.

IV Fourth Generation Language: Domain Specific languages


 The program written in HLL languages are needing to specify what to do? How to do? And when to
do? But program written in 4GL are needing to specify what to do only.
 It is considered as domain specific language.
 Fox-Pro, Power Builder, SQL are examples of 4GL.

2.8 Programming Design:


Programming design deals with the analysis, design and implementation of programs. There are two ways
of designing a program: (I) Top-down (II) Bottom-Up. These two design approaches play important role in
software development process.

2.8.1 Top-Down Design:


In this design approach a complete program or system described than it breaks downs into various small
program called subsystem or functions. This design methods leads from generalization to specialization.
Top-down design consists of a hierarchical structure as given below

Main Program

Sub Program1 Sub Program2

Sub Program3 Sub Program4

Fig 2.3 Top-Down Programming Approach

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

2.8.2 Bottom-Up design


In this approach, design methodology starts with the description of the lowest level procedure then moves
up towards more and more complex procedures using the procedure using the procedures which are
already defined. In this system all levels of subsystems are connected to form an entire system.
 In this approach we move bottom to top of our program
 Here data, object & class is basic unit
 This approach is used when we have enough idea about

Wrap text in a cell

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.

Wrap text automatically

1. In a worksheet, select the cells that you want to format.


2. On the Home tab, in the Alignment group, click Wrap Text. (On Excel for desktop, you can
also select the cell, and then press Alt + H + W.)

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.

Adjust the row height to make all wrapped text visible

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.

Enter a line break


To start a new line of text at any specific point in a cell:

1. Double-click the cell in which you want to enter a line break.


Tip: You can also select the cell, and then press F2.
2. In the cell, click the location where you want to break the line, and press Alt + Enter.

Algorithm to find Simple Interest:

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

2.9 Programming Paradigm:


Programming paradigms are a way to classify programming languages based on their features and their
style of programming. Languages can be classified into multiple paradigms like imperative, Functional,
declarative, logical, procedural, object-oriented etc.
Separate PPT
2.10 Characteristics of object-oriented programming (OOP)
Following are the characteristics of object-oriented programming’s
1. Object 2. class 3. Encapsulation 4. Abstraction 5. Inheritance 6. Polymorphism
Object
This is the basic unit of object-oriented programming. That is both data and function that operate on data
are bundled as a unit called as an object.
Class
When you define a class, you define a blueprint for an object. This doesn't define any data value, but it
defines class name, data type and operations can be performed on such an object.
Abstraction
Data abstraction refers to, providing only essential information to the outside world and hiding their
background details, i.e., to represent the needed information in program without presenting the details.
For example, a database system hides certain details of how data is stored and created and maintained.
Similar way, C++ classes provide different methods to the outside world without giving internal detail
about those methods and data.
Encapsulation
Encapsulation is placing the data and the functions that work on that data in the same place. While
working with procedural languages, it is not always clear which functions work on which variables but
object-oriented programming provides you a framework to place the data and the relevant functions
together in the same object.
Inheritance
One of the most useful aspects of object-oriented programming is code reusability. As the name suggests
Inheritance is the process of forming a new class from an existing class that is from the existing class called
as a base class, a new class is formed called as derived class.
This is a very important concept of object-oriented programming since this feature helps to reduce the
code size.
Polymorphism
The ability to use an operator or function in different ways in other words giving different meaning or
functions to the operators or functions is called polymorphism. Poly refers to many. That is a single
function or an operator functioning in many ways different upon the usage is called polymorphism.

2.11 Procedure oriented programming v/s Object Oriented programming

Procedure oriented programming


1. Emphasis is on doing things (algorithms)
2. Large Programs are divided into smaller programs known as functions.
3. Most of the functions share global data. Therefore, data are not secured.
4. Data move openly around the system from function to function.
5. Functions transform data from one form to another.
6. Employs top-down approach in program design.
7. This approach is not suitable for real world software design, but suitable for scientific and system side
programming.
8. It does not support abstract data type.

Object Oriented Programming


1. Emphasis is on data rather than on procedure
2. Programs are divided into objects
3. Data structures are designed in such a way that it characterizes the object
4. Functions that operate on data are ties together in a data structure called class
5. Data is hidden and cannot be accesses by external functions.
6.Objects may communicate to each other with the help of functions.
7.New data and functions can be easily added whenever necessary
8. Follows bottom-up approach.
9. This approach is suitable for real world software design.
10.It may carry al features of POP.

//This is Procedure Oriented Programming


#include<iostream>
void average(int, int, int);
using namespace std;
int main()
{ cout<<"This is Object Oriented Programming"<<endl;
cout<<"Average of three integer numbers "<<endl;
int a,b,c;
cout<<"Enter three integer numbers:";
cin>>a>>b>>c;
average(a,b,c);
cout<<"************ End of Program ************"<<endl;
return 0;
}
void average(int a, int b, int c)
{
float avg;
avg = (a+b+c)/3.0;
cout << " Calculated average is :"<<avg<<endl;
}

//This is Object Oriented Programming

#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.12 C++ Character Set


The character set is a set of valid characters that a language can recognize.
Letters A-Z, a-z
Digits 0-9
Special Space + - * / ^ \ () [] {} = != <> ‘ “ $ , ; : % ! & ? _ # <=
Characters >=

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

Asm Auto Break case catch


Char Class Const continue default
Delete Do Double else enum
Extern inline Int float for
friend Goto If long new
operator private Protected public register
return Short Signed sizeof static
struct switch Template this Try
typedef union Unsigned virtual void
volatile While

2.15 Identifier: It refer to names of variable, function, array class etc. Identifier are created by programmer
in the program.

Rules to create identifier


1. An identifier can consist of alphabets, digits and/or underscores.
2. It cannot start with a digit.
3. C++ is case sensitive that is upper case and lower-case letters are considered different from each
other.
4. Keywords cannot be used as an identifier.
Example: int a; // a is variable identifier.
Void getValue() // getValue is function name identifier.

2.16 Literals or Constants:


Literals (often referred to as constants) are data items that never change their value using the execution of
the program. The following types of literals are available in C++.
1. Integer-Constants
2. Character-constants
3. Floating-constants
4. Strings-constants

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.

Hexadecimal integer constant: It consists of a sequence of digits preceded by ox or OX.

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

2.19 Associativity and Precedence operator


Precedence and Associativity are two characteristics of operators that determine the evaluation order of
subexpressions in absence of brackets.

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.

2.23 Control Statements


Control structure includes following statements
a. Selection or Conditional Statements b. Iterative or Looping Statements

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.

2.24 Structure of C++ Program


Procedure Oriented
# preprocessor directives
Global variable declaration if any;
//function declaration
int main()
{ local variable declaration;
statement;
}
//function definition

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.25 Input Operation


Input operation is used to read the value and assign it to variable at run time of program. This is done by
writing following input statement in C++ program.
cin>>var;
Here var is variable and >> is operator called input operator or extraction operator.

2.26 output Operation


Output operation is used to display the result or message on the output device i.e., monitor. This is done by
writing following output statement in C++ program.
cout<<var;
Here var is variable. Value of this variable will be displayed
and << is operator called output operator or insertion operator.

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

Three terms are associated with C++ functions


1. Function definition
2. Function prototype
3.Function call

1.Syntax of function definition


It contains the code which specify the task to be performed by the function. It is called body of function.
Other than body it has function header and return value
return-typefunction_name(parameter list) // Function header
{ local variable if any;
statements; // Body
return value; // Return value
}
2.Function Prototype
Function prototyping describe the signature of function i.e., Return type, function type and data types of
parameters. It is used to declare the function before it is called in the function. Function prototype does not
have function body.
Syntax
return-type function name (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.

There two types of arrays


1. One-Dimensional array
2. Multidimensional array

1. One-Dimensional array: It is a collection of similar data items. It is in also called list.

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.

*********

You might also like