Algorithm
-
Upload
taimoor-khan -
Category
Documents
-
view
71 -
download
4
Transcript of Algorithm
![Page 1: Algorithm](https://reader035.fdocuments.us/reader035/viewer/2022062513/55620a83d8b42a7d028b4a5d/html5/thumbnails/1.jpg)
AlgorithmBy Taimoor Abdullah Khan
BSCS - 1st
![Page 2: Algorithm](https://reader035.fdocuments.us/reader035/viewer/2022062513/55620a83d8b42a7d028b4a5d/html5/thumbnails/2.jpg)
Definition“Set of instructions used to perform a certain task.”
![Page 3: Algorithm](https://reader035.fdocuments.us/reader035/viewer/2022062513/55620a83d8b42a7d028b4a5d/html5/thumbnails/3.jpg)
Characteristics of an Algorithm• Well-ordered Collection.• Unambiguous.• Effective.• Computable Operations.• Executable.• Result giving.• Halts in Finite amount of time.
![Page 4: Algorithm](https://reader035.fdocuments.us/reader035/viewer/2022062513/55620a83d8b42a7d028b4a5d/html5/thumbnails/4.jpg)
Elements of Algorithm1. Constants.2. Variables.3. Arrays.4. Operators.5. Loops.6. Conditional Statements.7. Comments
![Page 5: Algorithm](https://reader035.fdocuments.us/reader035/viewer/2022062513/55620a83d8b42a7d028b4a5d/html5/thumbnails/5.jpg)
1. Constants• Numbers as same as in mathematics.• Helps Structuring
Example: y = 5*ci = i + 1
![Page 6: Algorithm](https://reader035.fdocuments.us/reader035/viewer/2022062513/55620a83d8b42a7d028b4a5d/html5/thumbnails/6.jpg)
2. Variables• For Storing Values.• Can be given any name, but given such to
remind of its purpose.Sum, Temp etc.
Value
Variable
![Page 7: Algorithm](https://reader035.fdocuments.us/reader035/viewer/2022062513/55620a83d8b42a7d028b4a5d/html5/thumbnails/7.jpg)
3. Arrays• Two or more Variables with same name.• Addressed individually by their index.
myArray[7]• Variable or constant both used to address the index.• Each location can store a different value.• Usually Starting at index 0.
myA
rray
0 myA
rray
1 myA
rray
2
ValueValueValue
![Page 8: Algorithm](https://reader035.fdocuments.us/reader035/viewer/2022062513/55620a83d8b42a7d028b4a5d/html5/thumbnails/8.jpg)
4. Operators• Used to perform Operations on Variables or
Constants.• Mathematical Operators: +, -, *, / and %.• Assignment Operator: =• Comparison Operator: ==• Increment , Decrement Operator: ++, --
![Page 9: Algorithm](https://reader035.fdocuments.us/reader035/viewer/2022062513/55620a83d8b42a7d028b4a5d/html5/thumbnails/9.jpg)
5. Iteration• Used to repeat a certain code or lines.• Useful for replacing repetition.• Can use Index variables for Arrays.• Major two types of loops: for loop
While loop• Can be stopped from inside off loop body by using
break; statement.
![Page 10: Algorithm](https://reader035.fdocuments.us/reader035/viewer/2022062513/55620a83d8b42a7d028b4a5d/html5/thumbnails/10.jpg)
While Loop• Repeats if a certain Condition is true.• Stops when Condition becomes false.• Can repeat Infinite times.• Can be given value ‘1’ to represent Boolean value for ‘True’ so the loop remains
true.
• Syntax: while(Condition) { Loop body; }
![Page 11: Algorithm](https://reader035.fdocuments.us/reader035/viewer/2022062513/55620a83d8b42a7d028b4a5d/html5/thumbnails/11.jpg)
For Loop• Given a Variable, for which the condition is set.• Variable is Incremented or Decremented.• Repeats for fixed amount of times.• Syntax: for( i = 0; i <= n; i++) //n is any number
{Loop body;
}
![Page 12: Algorithm](https://reader035.fdocuments.us/reader035/viewer/2022062513/55620a83d8b42a7d028b4a5d/html5/thumbnails/12.jpg)
6. Conditional Statement• Used to take decision.• Checks a Condition.• Runs a statement body if Condition is true.• Otherwise runs statment body for “Else” statement if any is given.• Syntax: If(Condition)
{Loop body;
}Else {
Loop body; }
![Page 13: Algorithm](https://reader035.fdocuments.us/reader035/viewer/2022062513/55620a83d8b42a7d028b4a5d/html5/thumbnails/13.jpg)
7. Comments• Useful in long and complex programs.• Can be written where required
explanation.• Mostly written after ‘//’.
![Page 14: Algorithm](https://reader035.fdocuments.us/reader035/viewer/2022062513/55620a83d8b42a7d028b4a5d/html5/thumbnails/14.jpg)
The EndWhat Are You Staring At?? Its Done!
![Page 15: Algorithm](https://reader035.fdocuments.us/reader035/viewer/2022062513/55620a83d8b42a7d028b4a5d/html5/thumbnails/15.jpg)
Thank you.