Computer Programming
Chapter (7)
              Computer Programming
Computer Programming
                       Objectives
     Program and Programming
     Algorithms & Programs
     Computer Programming Languages
     Flowchart
     Examples
   Program and Programming
Program: مجموعة من األوامر المكتوبة بلغة برمجة يقوم الحاسب بتنفيذها
• A set of instruction written in a programming language that a
  computer can execute so that the machine acts in a
  predetermined way.
• Program solves a problem البرنامج يحل مشكلة معينة
• Before writing a program:
   – Have a thorough understanding of the problem
   – Carefully plan an approach for solving it.
Programming: البرمجة هي عملية كتابة أوامر للحاسب تخبر المعالج بما يجب أن
   يقوم به
• The Process of providing instructions to the computer that tells
  the processor what to do.
     Algorithms and Programs
  الخوارزمي يقدم حال لمشكلة دون االعتماد على أي لغة للبرمجة بينما
            البرنامج عبارة عن خوارزمي مكتوب بلغة معينة
• An Algorithm is a solution to a problem that is
  independent of any programming language.
                           While
• A program is an algorithm expressed using a
  specific set of instructions from any
  programming language.
           Algorithm Example
•    Maximum of two numbers
    Steps:
          1.Read/input two numbers
          2.Compare two numbers
          3.Print the Greater number
•   Average of three numbers
    Steps:
          1.Read/input three numbers
          2.Add three numbers
          3. divide the sum by 3.
          4.Print the result of division
        Computer Languages
- Machine language
- Assembly languages
- High-level languages
6
           Machine Language
• Programs and memory locations are written
  in strings of 0s and 1s.
• Problems with machine languages
    – Programs are difficult to write and debug
• Only option available to early programmers.
7
           Assembly Language
  – For example, A for add, C for compare, etc.
  – Use names rather than binary addresses for
    memory locations
• Require an assembler to translate the program
  into machine language.
  8
        High-Level Languages
• Transformed programming
    – Programs could be written and debugged
      much more quickly
• Requires a compiler to convert the
  statements into machine language
    – Each computer has its own version of a
      compiler for each language.
9
 Major Programming Languages
•    FORTRAN
•    COBOL
•    BASIC
•    Visual Basic
•    C/C++
•    Java
10
Flowcharts
Flowcharts
                              Flowchart
                             Flowchart
   – It is a graphical representation of an algorithm.
   – Components:
      •   Arrows/lines: Flow of control
      •   Parallelogram: Indicates input and output operations
      •   Rectangle symbol (action symbol): Indicates any type of
          action/computational step
      •   Oval symbol: Indicates the start or the end of a program or a
          section of code.
      •   Diamond: Decision.
                                                                          12
Flowcharts
             Flowchart Notations
              Arrows
                                   Diamond
                Parallelogram
                                     Oval
                Rectangle
                                             13
Flowcharts
             Examples
Flowcharts
             Example: Add Two Numbers
                      START
                       READ
                        A,B
                     Sum = A+B
                      PRINT
                       Sum
                       Stop
                                        15
Flowcharts
  Example: Average of Three Numbers
                 START
                   READ
                   A,B,C
                                     Sum= A+B+C
             Average = (A+B+C) / 3
                                     Avg = Sum / 3
                   PRINT
                  Average
                   Stop
                                                     16
Flowcharts
             Example: Area of a Circle
                      START
                      READ
                        R
                 Area = 3.14 * R * R
                      PRINT
                       Area
                      Stop
                                         17
Flowcharts
         Decision Making (Selection)
Flowcharts
       Example: Max. of Two Numbers
                            START
                            READ
                            N1, N2
             PRINT   Yes              No   PRINT
              N1           If N1>N2
                                            N2
                            Stop
                                                   19
Flowcharts
               Example: Pass or Fail
                             START
                             READ
                               D
             PRINT    Yes              No   PRINT
             “Pass”         If D>=60
                                            “Fail”
                             Stop
                                                     20
Flowcharts
               Example: Degree to Grade
                 START
                 READ
                   D
 PRINT   Yes               No              No              No            No PRINT
                If D>=90        If D>=80        If D>=70        If D>=60     “F”
  “A”
                                Yes             Yes             Yes
                                 PRINT            PRINT          PRINT
                                  “B”             “C”             “D”
                     Stop
                                                                          21