MODULE 1
(LABORATORY)
This Photo by Unknown Author is licensed under CC BY-NC-ND
Algorithms
• Algorithm: the step-by-step sequence of
  instructions that describe how the data is to be
  processed to produce the desired output
• Programming = the translation of the selected
  algorithm into a language the computer can use
                                                     2
Algorithms (continued)
• Pseudocode: English-like phrases used to
  describe the algorithm
• Formula: description of a mathematical
  equation
• Flowchart: diagram showing the flow of
  instructions in an algorithm
  – Flowcharts use special symbols
                                             3
 Expressing Algorithms
Algorithms can be expressed in many kinds of notation,
including:
     •   Natural Languages
     •   Pseudocode
     •   Flowcharts
     •   Programming Languages
Pseudocode
•   A program design technique that uses English words.
•   Has no formal syntactical rules.
•   Pseudo means false, thus pseudocode means false code. It looks like
    (imitates) real code but it is NOT real code.
•   Pseudocode cannot be compiled nor executed, and there are no real
    formatting or syntax rules.
•   Pseudocode should not include keywords in any specific computer
    languages.
•   The benefit of pseudocode is that it enables the programmer to concentrate
    on the algorithms without worrying about all the syntactic details of a
    particular programming language.
 How do I write Pseudocode?
• First you may want to make a list of the main tasks that must
  be accomplished on a piece of scratch paper.
• Then, focus on each of those tasks. Generally, you should try
  to break each main task down into very small tasks that can
  each be explained with a short phrase.
• There may eventually be a one-to-one correlation between
  the lines of pseudocode and the lines of the code that you
  write after you have finished pseudocoding.
• It is not necessary in pseudocode to mention the need to
  declare variables. It is wise however to show the initialization
  of variables.
• All statements showing "dependency" are to be indented.
  These include while, do, for, if, switch.
  Some Keywords That Should be Used
   For looping and selection, the keywords that are to be used
   include:
       Do While...EndDo        Call ... with (parameters)
       Do Until...Enddo    Call
       Case...EndCase      Return
       If...Endif       When
As verbs, use the words:
   Generate, Compute, Process
   Set, Reset, Increment, Compute, Test
   Calculate, Add, Sum, Multiply, ...
   Print, Display, Input, Output, Edit
Pseudocode Examples
Original Program Specification:
Write a program that obtains two integer numbers from
the user. It will print out the sum of those numbers.
Pseudocode:
Prompt the user to enter the first integer
Prompt the user to enter a second integer
Compute the sum of the two user inputs
Display an output prompt that explains the answer as the
sum
Display the result
Original Program Specification:
Write a program that will display if the entered student’s
grade is passed of failed. Passing grade is 70.
Pseudocode:
Prompt the user to enter a student grade
If student's grade is greater than or equal to 70
     Print "passed"
else
     Print "failed"
Original Program Specification:
Write a program that will allow the student to input the grade
on his 5 subjects and will compute for his average grade.
Original Program Specification:
Set total to zero
Set grade counter to one
While grade counter is less than or equal to 5
    Input the next grade
    Add the grade into the total
Set the class average to the total divided by 5
Print the class average.
Pseudocode example : If then Else
   If age > 17
        Display a message indicating you can vote.
   Else
        Display a message indicating you can't vote.
   Endif
Pseudocode example : Case
   Case of age
       0 to 17 Display "You can't vote."
      18 to 64 Display "Your in your working years."
      65 +     Display "You should be retired."
   Endcase
Pseudocode example : While loop
   count assigned zero
   While count < 5
       Display "I love computers!"
       Increment count
   Endwhile
Pseudocode example : For loop
   For x starts at 0, x < 5, increment x
         Display "Are we having fun?"
   Endfor
Pseudocode example : Do While loop
       count assigned five
       Do
           Display "Blast off is soon!"
           Decrement count
       While count > zero
Pseudocode example : Repeat Until loop
       count assigned five
       Repeat
             Display "Blast off is soon!"
             Decrement count
       Until count < one
Pseudocode example : Function with no parameter
passing
  Function clear monitor
     Pass In: nothing
     Direct the operating system to clear the monitor
  Pass Out: nothing
  Endfunction
Pseudocode example : Function with parameter passing
  Function delay program so you can see the monitor
     Pass In: integer representing tenths of a second
  Using the operating system delay the program
     Pass Out: nothing
  Endfunction
Pseudocode of : Function main calling the clear monitor
function
   Function main
      Pass In: nothing
      Doing some lines of code
      Call: clear monitor
      Doing some lines of code
      Pass Out: value zero to the operating system
   Endfunction
Flowcharts Symbols
Flowcharts Symbols (continued)
Flowcharts (continued)
 Flowchart for calculating the
 average of three numbers.
                                 19
 Guidelines for Drawing a Flowchart
• In drawing a proper flowchart, all necessary requirements
   should be listed out in logical order.
• The flowchart should be clear, neat and easy to follow. There
  should not be any room for ambiguity in understanding the
  flowchart.
• The usual direction of the flow of a procedure or system is
  from left to right or top to bottom.
• Only one flow line should come out from a process
symbol.
• Only one flow line should enter a decision symbol, but two or
  three flow lines, one for each possible answer, should leave the
  decision symbol.
• If the flowchart becomes complex, it is better to use
   connector symbols to reduce the number of flow lines.
   Avoid the intersection of flow lines if you want to make it
   more effective and better way of communication.
• Ensure that the flowchart has a logical start and finish.
• It is useful to test the validity of the flowchart by passing
   through it with a simple test data.
Advantages of using flowcharts
The benefits of flowcharts are as follows:
• Communication: Flowcharts are better way of communicating
  the logic of a system to all concerned.
• Effective analysis: With the help of flowchart, problem can be
  analyzed in more effective way.
• Proper documentation: Program flowcharts serve as a good
  program documentation, which is needed for various purposes.
•    Efficient Coding: The flowcharts act as a guide or blueprint   during the systems
    analysis and program development phase.
• Proper Debugging: The flowchart helps in debugging process.
• Efficient Program Maintenance: The maintenance of operating
  program becomes easy with the help of flowchart. It helps the
  programmer to put efforts more efficiently on that part
Limitations of using flowcharts
• Complex logic: Sometimes, the program logic is quite
  complicated. In that case, flowchart becomes complex and
  clumsy.
• Alterations and Modifications: If alterations are required the
  flowchart may require re-drawing completely.
• Reproduction: As the flowchart symbols cannot be typed,
  reproduction of flowchart becomes a problem.
Flowcharting Examples
Example 1
Draw a flowchart to find
the sum of first 50
natural numbers.
Example 2
Draw a flowchart
to find the largest
of three numbers
A,B, and C.
Example 3
Draw a flowchart for
computing factorial N (N!)
Where N! = 1 ´ 2 ´ 3 ´ …… N
.
Problem Solving Process
 Input     Process        Output
Example Problem #1
Calculate and display the price of a number
of apples if the quantity in kg and price per kg
are given.
 Input           Process                   Output
• Quantity       Price = Quantity * Price_per_kg
• Price_per_kg                                     Price
Pseudocode
     1. Start
     2. Read quantity
     3. Read price_per_kg
     4. price  quantity *
        price_per_kg
     5. Print price
     6. End
Flowchart: Calculate Price of Apples
                     Start
                    Input
                   Quantity
                     Input
                 Price_per_kg
        Price = Quantity * Price_per_kg
                    Output
                     Price
                     End
Source Code
#include<iostream>                            Pre-processor
using namespace std;
int main()
                                               Main function
{
int price = 0, quantity = 0;                   Start
float price_per_kg = 0;                        Variable declarations
cout << "Enter quantity of an apple : " ;
cin >> quantity;                               Input
cout << "Enter price of an apple per kilogram : " ;
cin >> price_per_kg;                           Input
price = quantity * price_per_kg;               computations
cout << "The price of an apple in kilogram is " << price << "\n";
system ("pause");
return 0;
}                                               Output
                                               Stop
Example #2
A car park has the following charges:
The 1st hour costs Php 5.00. The
subsequent hour cost Php 2.00 per hour.
Write an algorithm based on a vehicle’s
entry and exit time.
  Input          Process          Output
  • Entry_time                   Charge
                 ????
  • Exit_time
Flowchart: Calculate Car Park Charge
                             Start
                         Input Entry_time
                          Input Exit_time
                Period = Exit_time – Entry_time
   Charge = 5                  If                 Charge = (Period -1) * 2
                   No      Period > 1   Yes
                          Output
                          Charge
                             End
Laboratory
 Activities
Problem #1:
Given three numbers A,B,C. Draw
a flowchart to compute and print
out the sum, the average, and the
product of these values.
Problem #2:
Create a flowchart from the
given Algorithm below.
Step 1: Read in the values of X and Y.
Step 2: Subtract Y from X.
Step 3. If the difference between the X and Y is negative, compute
R=X+Y; if zero, compute R=2x+2y; and if positive, compute
R=X*Y.
Copy And Paste In Your Browser For
Additional Learnings
Introduction to Algorithms and flowchart in C++
Programming
https://www.youtube.com/watch?v=W8-iX15matg
End of Module 1