Introduction to Programming
What is a Computer?
• A computer is an electronic device capable of
      performing commands
•     Commands: The basic commands that a computer
      performs are input (get data), output (display result),
      storage, and performance of arithmetic and logical
      operations
    Instructions
                            A Computer
                                                    Results
                              System
          Data
Introduction
 • Without software, the computer is useless
 • Software developed with programming languages
    • C++ is a programming language
 • C++ suited for a wide variety of programming tasks
 • Before programming, it is useful to understand
   terminology and computer components
                C++ Programming: From Problem Analysis
                                                         3
                    to Program Design, Fifth Edition
The Language of a Computer
• When you press A on your keyboard, the computer
  displays A on the screen. But what is actually stored
  inside the computer’s main memory? What is the
  language of the computer? How does it store
  whatever you type on the keyboard?
                 C++ Programming: From Problem Analysis
                                                          4
                     to Program Design, Fifth Edition
The Language of a Computer
• Electrical signals are used inside the computer to
  process information. There are two types of
  electrical signals: analog and digital.
   • Analog signals: are continuous wave forms used to
     represent such things as sound. Audio tapes, for
     example, store data in analog signals.
   • Digital signals: represent information with a sequence of
     0s and 1s. A 0 represents a low voltage, and a 1
     represents a high voltage
                  C++ Programming: From Problem Analysis
                                                             5
                      to Program Design, Fifth Edition
The Language of a Computer
• Digital signals: sequences of 0s and 1s
• Machine language: language of a computer
• Binary digit (bit):
   • The digit is called binary digit or bit 0 or 1
• Binary code:
   • A sequence of 0s and 1s
• Byte:
   • A sequence of eight bits is called a byte
                    C++ Programming: From Problem Analysis
                                                             6
                        to Program Design, Fifth Edition
The Language of a Computer (cont’d.)
            C++ Programming: From Problem Analysis
                                                     7
                to Program Design, Fifth Edition
The Language of a Computer
(cont'd.)
• ASCII (American Standard Code for Information
  Interchange) The most commonly used encoding
  scheme on personal computers is the seven-bit
  American Standard Code for Information
  Interchange (ASCII).
  • Consists of 128 characters: 0-127
  • A is encoded as the binary number 1000001 (66th
    character in ASCII)
  • 3 is encoded as 0110011
                C++ Programming: From Problem Analysis
                                                         8
                    to Program Design, Fifth Edition
The Language of a Computer
(cont'd.)
• Inside the computer, every character is represented
  as a sequence of eight-bits, that is, as a byte.
ASCII is a seven-bit code. Therefore, to represent
each ASCII character inside the computer, you must
convert the seven-bit binary representation of an
ASCII character to an eight-bit binary representation.
   • This is accomplished by adding 0 to the left of the seven-
     bit ASCII encoding of a character.
   • Example: the character A is represented as 01000001,
     and the character 3 is represented as 00110011 by
     adding 0 to the left.
                   C++ Programming: From Problem Analysis
                                                              9
                       to Program Design, Fifth Edition
The Evolution of Programming
Languages
• Early computers were programmed in machine
  language
• To calculate wages = rates * hours in
  machine language:
 100100 010001                    //Load
 100110 010010                    //Multiply
 100010 010011                    //Store
               C++ Programming: From Problem Analysis
                                                        10
                   to Program Design, Fifth Edition
The Evolution of Programming
Languages (cont'd.)
 • Assembly language instructions are mnemonic
 • Assembler: translates a program written in assembly
   language into machine language
               C++ Programming: From Problem Analysis
                                                        11
                   to Program Design, Fifth Edition
The Evolution of Programming
Languages (cont'd.)
• Using assembly language instructions, wages =
  rates • hours can be written as:
    LOAD rate
    MULT hour
    STOR wages
               C++ Programming: From Problem Analysis
                                                        12
                   to Program Design, Fifth Edition
The Evolution of Programming
Languages (cont'd.)
• The next step toward making programming more
  easier (high-level languages)
               C++ Programming: From Problem Analysis
                                                        13
                   to Program Design, Fifth Edition
Programming Languages
• Classification of programming languages:
  1. Machine language (0s and 1s)
  2. Low-level languages (Assembly)
  3. High-level languages (natural languages
     such as English, French, German, and
     Spanish. Basic, FORTRAN, COBOL, Pascal, C,
     C++, C#, Java)
1. Machine level languages
• A computer understands only sequence of bits or 1’s
   and 0’s (the smallest piece of information)
• A computer program can be written using machine
   languages (01001101010010010….)
     • Very fast execution
     • Very difficult to write and debug
     • Machine specific (different codes on different
        machines)
2. Low level languages
• English encrypted words instead of codes (1’s and
  0’s)
• More understandable (for humans)
• Example: Assembly language
• Requires: “Translation” from Assembly code to
  Machine code
   Assembly Code                          Machine Code
compare:                                1001010101001101
  cmpl #oxa,n                           1110010110010100
  cgt                   Assembler       0101010111010010
   end_of_loop                          0110100110111011
  acddl #0x1,n                          1101100101010101
end_of_loop:
3. High level languages
• Mostly machine independent
• Close to natural language (English like language
    keywords)
•   Easy to write and understand programs
•   Easy to debug and maintain code
•   Requires compilers to translate to machine code
•   Slower than low-level languages
3. High level languages examples
• Some Popular High-Level languages
  •   COBOL (COmmon Business Oriented Language)
  •   FORTRAN (FORmula TRANslation)
  •   BASIC (Beginner All-purpose Symbolic Instructional Code)
  •   Pascal (named for Blaise Pascal)
  •   Ada (named for Ada Lovelace)
  •   C (whose developer designed B first)
  •   Visual Basic (Basic-like visual language by Microsoft)
  •   C++ (an object-oriented language, based on C)
  •   Java
High-Level Language C++
• The instruction written in C++ is much easier to
  understand and is self-explanatory.
• The computer cannot directly execute instructions
  written in a high-level language. To run on a computer,
  these C++ instructions first need to be translated into
  machine language.
• A program called a compiler translates instructions
  written in high level languages into machine code.
• Compiler: A program that translates instructions written
  in a high-level language into the machine language
Processing a C++ Program
#include <iostream>
using namespace std;
int main()
{
    cout << "My first C++ program." << endl;
  return 0;
}
Sample Run (OUTPUT):
My first C++ program.
             C++ Programming: From Problem Analysis
                                                      20
                 to Program Design, Fifth Edition
Terminology
• Source Code: A program written in high-level language.
• Object program: The machine language version of the
  high-level language program.
• Compiling/Compiler: The action of turning the source
  code into a format the computer can use (object code).
• Linking: Combines object program with other programs
  (Libraries) provided by the SDK to create executable code
• Executable: The result of compiling and linking a source
  program; the “.exe” file that the computer can run
• Loader: A program that loads an executable program into
  main memory.
• The last step is to execute the program
                                                     21
Processing a C++ Program
(cont'd.)
         C++ Programming: From Problem Analysis
                                                  22
             to Program Design, Fifth Edition
Another C++ Example
Consider the following C++ program:
#include <iostream>
using namespace std;
int main()
{
   cout<<"My first C++ program."<< endl;
   cout<<"The sum of 2 and 3 ="<< 5 << endl;
   cout<<"7 + 8=“ << 7 + 8 <<endl;
   return 0;
}
OUTPUT: (When you compile and execute this program, the following
three lines are displayed on the screen.)
   My first C++ program.
   The sum of 2 and 3 = 5
   7+8= 15
   C++ example
• #include <iostream> preprocessor directives
• main: every c++ program consist of main function
• Anything in double quotes is a string. For example, "My first
  C++ program." and "7+8=" are strings.
• 7+8 is an arithmetic expression (without quotes)
• endl: causes the insertion point to move to the beginning of
  the next line
• cout<<: to print statements to the console.
• using namespace std; allows you to use cout and endl
  without the prefix std::
• The last statement, that is, return 0; returns the value 0 to
  the operating system when the program terminates.
Problem Solving Steps – Programming Process
 Programming is a process of problem
 solving
 1. Understand the problem
 2. Plan the logic
 3. Code the program
 4. Test the program
 5. Deploy the program into production
1. Understanding the Problem
 • Problems are often described in natural language
   like English.
 • Users may not be able to specify needs well, and
   the needs may changing frequently
• Identify the requirements
   1. Inputs or given data-items
   2. Required output(s) or desired results
   3. Indirect inputs (may not be given directly, you
      have to calculate or assume)
1. Understanding the Problem
 • Example: Calculate the area of a circle
   having the radius of 3 cm
    • Inputs:
       •   Radius=3
    • Output:
       •   Area
    • Indirect Inputs:
       •   Pi=3.14
       •   Area = 3.14 * (3*3) = 28.27
2. Plan the Logic
 • Identify/Outline small steps in sequence, to
   achieve the goal (or desired results)
 • Tools such as flowcharts and pseudocode can
   be used:
   1. Flowchart: a pictorial representation of the logic
      steps
   2. Pseudocode: English-like representation of the logic
 • Walk through the logic before coding
3. Code the Program
• Code the program:
  • Select the programming language
  • Write the program instructions in the selected
    programming language
  • Use the compiler software to translate the
    program into machine understandable code or
    executable file
  • Syntax errors (Error in program instructions) are
    identified by the compiler during compilation
    and can be corrected.
4. Test the Program
• Testing the program
   • Execute it with sample data and check the
     results
  • Identify logic errors if any (undesired results
    or output) and correct them
  • Choose test data carefully to exercise all
    branches of the logic (Important)
5. Deploy the Program
• Putting the program into production
  • Do this after testing is complete and all known
    errors have been corrected
Introduction to Pseudocode
• One of the popular representation based on
  natural language
• Widely used
   • Easy to read and write
   • Allow the programmer to concentrate on the
     logic of the problem
• Structured in English language (Syntax/grammar)
What is Pseudocode (continued...)
• English like statements
• Each instruction is written on a separate line
• Keywords and indentation are used to signify
  particular control structures.
• Written from top to bottom, with only one entry
  and one exit
• Groups of statements may be formed into
  modules
What is Pseudocode (continued...)
 Example: Calculate the area of a circle having
 the radius of 3 cm
 Pseudocode:
 • Get radius of a circle
 • Calculate Area
 • Display Area
                     Flowcharts
• “A graphic representation of a sequence of operations to
  represent a computer program”
• Flowcharts show the sequence of instructions in a single
  program or subroutine.
                  A Flowchart
• A Flowchart
  • Shows logic of an algorithm or problem
  • Shows individual steps and their interconnections
  • E.g., control flow from one action to the next
A Flowchart
     START
        Input
      Radius (r)
  Calculate Area =
    3.14 * r * r
     Display
      Area
     END
                 Flowchart Symbols
    Name            Symbol              Description
    Oval                     Beginning or End of the Program
Parallelogram                Input / Output Operations
  Rectangle                  Processing for example, Addition,
                             Multiplication, Division, etc.
                             Denotes a Decision (or branching)
  Diamond                    for example IF-Then-Else
                             Denotes the Direction of logic
   Arrow                     flow
Relational / Logical Operators
                 Relational Operators
     Operator                      Description
        >          Greater than
        <          Less than
        =          Equal to
                  Greater than or equal to
                  Less than or equal to
                  Not equal to
QUIZ