Algorithms - technolic

Download Report

Transcript Algorithms - technolic

Algorithms
• An algorithm is a finite set of sequential
instructions to accomplish a task, where
instruction are written in the natural languages
like English. Is also called as a step by step
solution of the problem.
• Algorithm has four properties, that are:
• Finiteness: Total number of steps used in algorithm
should be finite.
• Definiteness: Each step of algorithm must be clear
& an unambiguous.
• Effectiveness: Every step must be basic & essential.
• Input & Output: The algorithm must accept zero
or more input & must produce at least one output.
• Advantages:
– It is very easy to write.
– Easy technique to understand logic.
• Disadvantages:
– Time consuming
– Difficult to show branching & looping
– Big mistakes are difficult to put in algorithm.
• Simple flow problem: These are very simple
problems & the procedure flow is sequential
manner, means after first step, it automatically
goes to second step.
Write an algorithm to calculate area of
rectangle.
• Step 1: Accept the value of length i.e l.
• Step 2: Accept the value of Breadth i.e b.
• Step 3 :Calculate area i.e a=l*b
• Step 4 :Print Area i.e Print a
• Step 5:Stop procedure.
Write an algorithm to calculate percentage of
student.
• Step 1: Accept the name & marks in 6
subjects, i.e n,m1,m2,m3,m4,m5,m6
• Step 2: Total = m1+m2+m3+m4+m5+m6
• Step 3 :Calculate percentage i.e
per=Total/600*100
• Step 4 :Print Percentage i.e Print per
• Step 5:Stop procedure.
• Branching Flow problems: These sentence are not
simple. It consist of condition, it test that
condition, according to it transfers the flow. Here
flow is not sequential that means after branching
step, it automatically goes to step mentioned in
the statement.
Write an algorithm to find out positive or negative
number.
• Step 1: Accept the number i.e n
• Step 2: check if n<0? If yes go to Step 3 otherwise
go to Step 5
• Step 3: Print n is negative
• Step 4: Stop procedure
• Step 5: Print n is positive
• Step 6:Stop procedure
• Write an algorithm to find out smallest
number out of two number.
– Step 1.Accept two numbers i.e a, b
– Step 2: check if a<b? If yes go to Step 3 otherwise
go to Step 5
– Step 3: Print a Smallest number
– Step 4: Stop procedure
– Step 5: Print b Smallest number
– Step 6: Stop procedure
• Loops: There are 2 types of loops: Finite loop &
infinite loops.
• When number of steps are repeated many
number of times using same sequence is called as
a loop & set of repeated steps are called as a
body of loop
• Finite loop: When body of loop is executed finite
number of times called as finite loop.
– Step 1: Initialize running variable with initial value I=1;
– Step 2: Increment running variable by step value I=I+2
– Step 3: Check & transfer control to step 2 or in
between step 1 & step 2. If I<10 ten step 2.
• Write an algorithm to print first 50 odd
numbers.
• There are two possible algorithms:
• Algorithm 1:
Algorithm 2
– Step 1: I=1
– Step 2: ODD =1
– Step 3: print ODD
– Step 4: ODD=ODD+2
Step 1: I=1
Step 2: Print I
Step 3: I=I+2
Step 4: If I<=99 go to
step 2
– Step 5: I=I+1
Step 5:Stop procedure.
– Step 6: If I<=50 go to step 3
– Step 7: Stop procedure.
• Write an algorithm to find out factorial of
given number.
– Step 1: Accept n
– Step 2: I=1
– Step 3: Fact=1
– Step 4: Fact= Fact* I
– Step 5: I=I+1
– Step 6 : If I<=n go to step 4
– Step 7: Print Fact
– Step 8: Stop procedure.
• Infinite Loop: When the body of loop is
executed infinite number of times called as
infinite loop.
• Write an algorithm to print “Tech Max
Publication” infinite times.
– Step 1: Print “Tech Max Publication”
– Step 2: Go to step 1
– Step 3: Stop procedure
Here step 3 never executes, so it is called as dead
step.
• Write an algorithm & draw flowchart for
finding sum digits of a number
• Algorithm:
– Step 1: Accept n
– Step 2: N1=N, Sum =0
– Step 3: D=N%10
– Step 4: Sum=Sum+D
– Step 5: N=N/10
– Step 6: If N!=0 go to step 3
– Step 7: print Sum
– Step 8: Stop procedure
• Write a program which prints Fibonacci series
• Algorithm:
– Step 1: Accept n
– Step 2: A=0, B=1
– Step 3: Print A, B
– Step 4: C=A+B
– Step 5: Print C
– Step 6: A=B, B=C
– Step 7: If C<=n Then step 4
– Step 8: Stop Procedure.
• Write an algorithm to check the input year is
leap year or not.
• Algorithm
– Step 1: Accept year
– Step 2: If year %4==0 step 3 else step 5
– Step 3: print Leap year
– Step 4: Stop Procedure
– Step 5: It is not a leap procedure
– Step 6: Stop Procedure.
• Write an algorithm to print all prime numbers in
the given range.
• Algorithm
–
–
–
–
–
–
–
–
–
–
–
–
Step 1: Accept r1,r2
Step 2: n=r1
Step 3: F=1
Step 4: d=2
Step 5: If n%d==0 then F=0 Goto step 8
Step 6: d=d+1
Step 7: If d<n Goto step 5 else step 8
Step 8: If F=0 then Step 10
Step 9: Print n
Step 10: n=n+1
Step 11: If n<=r2 Goto step 4
Step 12: Stop procedure
C++ Programming
• C++ programming contains following sections:
• Header Files
– For using library function use header file at the top of
the program. This is done with the #include statement.
– The way to include I/O header file of c++ is as follows
– #include<iostream.h>
– The above header file contains function prototype for
the standard input & output functions with various input
output stream
• main() function
– The c++ program is also collection of functions.
– The execution of any program in c++ starts from main().
– Every c++ program must have main function.
Comments: Comments are used in c++ for user
understanding. It is not related to compiling &
execution. There are two types to specify comment:
• Single Line Comment:
– It starts with double slash symbol (//) and terminate at
the end of the line.
– A comment may start anywhere in the line & whatever
follows till the end of the line is ignored at the time of
program compiling
– There is no closing symbol. Eg: //This is a fruit.
• Multiple line comment:
– This is also called as block comment. The c++ comments
are written inside symbols ‘/*’ (for starting) and (for
terminating) ’*/’ are still valid & more suitable for
multiple comments.
• Output handing stream:
– To display the message on screen, we used keyword cout
and <<.
– This is pronounced as ‘c out’. << operator is called as
insertion operator.
– Eg: cout<<“This is new feature”;
– We can display more outputs in single cout called
Cascaded outputs.
– The operator << is an insertion operator or put to
operator. It inserts content of variable to the object on
its left.
– This operator use fro 2 purpose ‘bit shifting & printing
message with cout’.
• Input handing stream:
– For reading the data from the keyboard, wee use input
handing stream as cin and >> together. ‘ >>’ operator is
called as Extraction operator. This is pronounced as “c
in”.
– Eg: int num; cin>>num;
– We can also take more than two inputs in single cin as
cin>>num1>>num2;
– This is called as cascaded inputs.
– The operator >> is extraction operator. It extracts the
data from keyword or user and assigns it to the variable.
It is also known as “Get from operator”.
Escape sequence:
• \b- backspace
• \n- newline
• \t- horizontal
• \”- Display double quote
• \’ – Display single quote
• \0- null
• \\ - backslash
• \?- Display question mark
Data Types
• Data type representation are as follows:
Type
Size
Range
char
1
-128 to 127
int
2
float
4
-31768 to
32767
3.4E-38 to
3.4E+38
double
8
Unsigned char 1
1.7E-308 to
1.7E+308
0 to 255
Unsigned int
0 to 65535
2
Operators in c++
• Arithmetic operators:
• These methods are used for mathematical
calculation. This mathematical calculation is in
between variable, constant or combination of both.
• For arithmetic operation, the operator requires two
operands & hence all the arithmetic operators are
binary operators.
Name of operator
Operator
Addition
+
Subtraction
-
Multiplication
*
Remainder operator
%
Division Operator
/
• Relational operator
• These operators are used to compare two values.
• The comparison is in between two operands & these are binary
operators.
• These operator check the condition is true or false. If the condition is
true it returns 1 otherwise 0.
Name of operator
Operator
Less than
<
Grater Than
>
Less than equal to
<=
Greater Than equal to
>=
Equal to equal to
==
Not equal to
!=
• Logical operators:
• Logical operators are nothing but to combine
results of one or more expression & resultant
expression is called as “logical expression”.
• The output of the logical operator is either
true(1) or false(0).
Name of the
operator
AND
OR
NOT
Operator
Operator type
&&
||
!
Binary
Binary
Binary
• Assignment operator
• Operator ‘=’ causes the value of the right hand
operand to be assigned to the left hand operand.
• The left hand operand, sometime called as ‘lvalue’. This is always referring always to a
memory location.
• Unary Operator:
• The unary operator, which takes only one
operand & predict output.
• Unary operators work on the concept of prefix
postfix.
• Prefix: Operator used before the variable ++a,--a.
• Postfix: Operator used after the variable a--,a++.
Name of the operator
Unary minus
Unary plus
Increment
Decrement
Logical Negation
One’s Complement
Pointer reference
Address
Size of an object
Type cast
Operator
+
++
-!
~
*
&
Size of
(type)
Conditional operator:
• This is one & only one ternary operator in c++.
• Syntax: expression?expression1:expression2.
Scope Resolution operator
• If we declare the variable inside the function
definition is called local variable.
• And outside the function definition , declared
variable is called Global variable.
• The global variable are accessed by any function
of class. The scope of local is only with in the
block.
• C++ solved this problem by scope resolution
operator (::). This can be used to uncover a
hidden variable.
• Manipulators:
• To format the data that are to be displayed on
screen we use these types of operators called
as manipulators.
• The most commonly used manipulators are
setw & endl.
• endl manipulator: It moves the cursor to the
next line.
• setw manipulator: Sometimes we want the
output in a right align.
• To use setw we must include <iomanip.h>
header file in our program.
• Type cast Operators.
• Type cast is used to convert a value from one type
to another.
• There is no automatic type convert of one form into
another. Like as int to float, float into double etc.