Dr. Sajid M.
Sheikh
                         Senior Lecturer
                Department of Electrical Engineering
                      University of Botswana
                      Office No: 248/106
                           Ext: 4956
                 Email: sheikhsm@ub.ac.bw
EEB 334: Computer Programming
           CHAPTER 1
 INTRODUCTION TO PROGRAMMING
Objectives
    • Be able to define and understand the following:
            - Software and hardware
            - bits and bytes
            - high-level languages
            - compilers
            - source code
            - object code
            - compilers
            - linking
            - algorithm
    • Understand the Program design process
                      Introduction to computers and C++
                                 Programming
1.1     Computer Systems
• A set of instructions for a computer to follow is called a program.
• The collection of programs used by a computer is referred to as the software for
  that computer.
• The actual physical machines that make up a computer installation are referred to
  as hardware
1.1.1 Hardware
• There are three classes of computers: PCs, workstations and
  mainframes.
• A PC (Personal computer) is a relative small computer designed
  to be used by one person at a time
• A workstation is a larger and more powerful PC.
• A mainframe is even larger computer that requires some
  support staff and generally is shared by more than one user.
• A network consists of a number of computers connected, so
  they may share resources such as printers, and may share
  information.
The hardware for most computer systems is organised as
shown below
                         CPU
 Input devices       Main Memory            Output devices
                       Secondary
                       memory
• The computer has five main components
   •   Input devices
   •   Output devices
   •   Central processing unit (CPU)
   •   Main memory
   •   Secondary memory
• The CPU and main memory form the heart of a computer and
  can be thought of as an integrated unit.
• An input device is any device that allows a person to
  communicate information to the computer e.g. Keyboard and
  mouse
• An output device is anything that allows the computer to
  communicate information to you. E.g. Video display unit
• The computer has two forms of memory called the main memory and
  secondary memory.
• The program that is being executed is kept in the main memory (often
  referred to as RAM or random access memory.
• The main memory consists of a long list of numbered locations called
  memory locations. The number of memory locations varies from
  computer to computer.
• The memory locations in most computers contain eight bits. An eight-
  bit portion of memory is called a byte.
• The number that identifies a byte is called its address.
• If the computer needs to deal with a data item (such as a large number)
  that is too large to fit in a single byte, it will use several adjacent bytes
  to hold the data item. In this case the entire chunk of memory that
  holds the data item is still a memory location.
• Memory locations and bytes
               Byte 1
                               3 byte location with
               Byte 2          address 1
               Byte 3
                               2 byte location with
                               address 4
                               1 byte location with address 6
                               3 byte location with
               Byte 8
                               address 7
               Byte 9
• Secondary memory is the memory that is used for keeping a permanent record of
  information after (and before) the computer is used.
• Information in secondary storage is kept in units called files.
• A program is stored in a file in secondary storage and copied into main memory
  when the program is run.
• Several different kinds of secondary memory may be attached to a single
  computer. The most common forms of secondary memory are hard disks,
  diskettes, tapes and CD-ROM.
• The central processing unit (CPU) is the brain of the computer.
• The CPU follows the instructions in a program and performs the calculations
  specified by the program.
• The CPU is however, a very simple brain. All it can do is follow a set of simple
  instructions provided by the programmer.
1.1.2 Software
• You do not normally talk directly to the computer, but
  communicate with it through an operating system.
• The operating system allocates the computer's resources to the
  different tasks that the computer must accomplish.
• The operating system is actually a program. Think of the
  operating system as your chief servant.
• If you want to run a program, you tell the operating system the
  name of the file that contains it, and the operating system runs
  the program.
• The names of some common operating systems are
    • UNIX, DOS, OS/2, Windows, Macintosh and VMS.
• A program is a set of instructions for a computer to follow.
• As shown below, the input to a computer can be thought of as consisting of two
  parts, a program and some data.
• The data is what we conceptualise as the input to the program.
• Whatever we give a computer both as program to follow and some data for the
  program, we are said to be running the program on the data and the computer is
  said to execute the program on the data
• Simple view of running a program
                      Program                   Data
                                     Computer
                                      Output
1.1.3 High-Level Languages
• There are many languages for writing programs.
• C++ is a high-level language
• Other high-level languages include
   • C, Pascal, FORTRAN, BASIC, COBOL, Modula-2, Lisp, Scheme
     and Ada
• High-level languages resemble human languages in many ways.
  They are designed to be easy for human beings to write programs
  in and to be easy for human beings to read.
• A high level language contains instructions that are much more
  complicated than the simple instructions a computer is capable
  of following.
• The kind of language a computer can understand is called a low-level
  language.
• The exact details of a low-level language differ from computer to
  computer.
• A typical low-level instruction might be the following:
    • ADD X Y Z
• This instruction might mean “add the number in the memory location
  X to the number in the memory location called Y and place the result
  in the memory location called Z”.
• The above sample instruction is written in what is called assembly
  language. The assembly language must be translated into string of
  zeros and ones before the computer can understand it.
• Programs written in the form of zeros and ones are said to be written
  in Machine language.
1.1.4 Compilers
• A program that translates a high-level language like C++ to a
  machine language is called a compiler.
• A compiler its input or data is some other program and its output
  is yet another program.
• The input program is called source program or code
• The translated version produced by the compiler is called the
  object program or object code.
• The object code for your C++ program must be combined with
  the object code for routines (such as input and output routines)
  that your program uses. This process of combining object code is
  called linking and is done by a program called a linker. For simple
  programs linking may be done for you automatically.
Preparing a C++
Program for         C++ program
running
                       Compiler
                  Object code for C++   Object code for
                       program           other routines
                         Linker
                   Complete machine
                    language code
                      ready to run
1.2 Programming and Problem-Solving
• 1.2.1 Algorithms
• The most difficult part of solving a problem on a computer is
  discovering the method of solution. After you come up with a
  method of solution, it is routine to translate your method into the
  required language.
• A sequence of precise instructions which lead to a solution is
  called an algorithm. Some approximate equivalent words are
  recipe, method, directions, procedure and routine
• The instruction may be expressed in a programming language or
  human language.
• Our algorithms will be expressed in English
• A computer program is simply an algorithm expressed in a
  language that a computer can understand.
An Algorithm
•1     Get the list of names
•2     Get name being checked
•3     Set a counter to zero
•4     Do the following for each name on the list:
   • Compare the name on the list of the name being checked and if the names are the same,
     then add one to the counter
• 5 Announce that the answer is the number indicated by the counter
• The above algorithm determines the number of times a specified name occurs on
  a list of names
1.2.2 Program Design
• Designing a program is often a difficult talk with no complete
  set of rules, no algorithm to tell you how to write programs.
  Program design is a creative process.
• The outline is given in the diagram below. The entire process
  can be divided into two phases: the problem-solving phase
  and the implementation phase.
• The result of the problem-solving phase is an algorithm,
  expressed in English, for solving the problem.
• To produce a program in a programming language, the
  algorithm is translated into the programming language .
  Producing the final program from the algorithm is called the
  implementation phase.
       Program Design Process
         Start             Implementation phase
   Problem definition
       Algorithm                Translating to C++
    Desktop testing                  Testing
Problem-solving phase           Working program
1.2.3 The software Life Cycle
• Designers of large software systems, such as compilers and
  operating systems, divided the software development process
  into six phases known as the software life cycle. The six phases
  of this life cycle are:
• 1. Analysis and specification of the task (problem definition);
• 2. Design of software (algorithm design)
• 3. Implementation (Coding)
• 4. Testing
• 5. Maintenance and evolution of the system
• 6. Obsolescence
1.3 Introduction to C++
• The C programming language was developed by Dennis Ritchie in
  1970. It was first used for writing and maintaining the UNIX operating
  system.
• The C language is peculiar because it is a high-level language with
  many of the features of a low-level language
• C is somewhere in between the two extremes of very high-level and
  low-level language and therein lies both its strengths and its
  weaknesses.
• Like assembly language, C language programs can directly manipulate
  the computer memory. It also has features of a high-level language,
  which makes it easier to read and write than assembly language.
• This makes C an excellent choice for writing systems programs, but for other
  programs C is not as easy to understand as other languages.
• To overcome these and other shortcomings of C, C++ was developed in 1980.
  C++ was designed to better C. Most of C is a subset of C++ and most C programs
  are also C++ programs.
• Unlike C, C++ has facilities to do object-oriented programming, which is a recently
  developed and very powerful programming technique.
1.3.2 A sample C++ program
• The figure below contains a simple C++ program and the screen
  display that might be generated when a user runs and interacts
  with this program.
• The person who runs a program is called the user.
• The person who writes the program is called the programmer.
• The beginning and end of our sample program contain details
  that need not concern us yet.
• #include <iostream.h>
     int main()
 {
         return0;
 }
                                                                  A sample C++ program
#include <iostream.h>
  int main()
  {
               int number_of_pods, peas_per_pod, total_peas;
               cout << “press return after entering a number. \n”;
               cout << “Enter the number of pods: \n”;
               cin >> number_of_pods;
               cout << “Enter the number of peas in a pod: \n”;
               cin >> peas_per_pod;
               total_peas = number_of_pods * peas_per_pod;
               cout << “If you have”;
               cout << number_of_pods;
               cout << “pea pods\n”;
               cout << “and”;
               cout << peas_per_pod;
               cout << “peas in each pod, then\n”;
               cout << “you have “;
               cout << total_peas;
               cout << “ peas in all the pods. \n”;
               return 0;
  }
• Sample Dialogue
                    Press return after entering a number
                    Enter the number of pods
                    10
                    Enter the number of peas in a pod
                    9
                    If you have 10 pea pods
                    And 9 peas in each pod, then
                    You have 90 peas in all the pods.
Description of the above lines
• int number_of_pods, peas_per_pod, total_peas;
• The above line is called a variable declaration. This variable
  declaration tells the computer that number_of_pods, peas_per_pod
  and total_peas will be used as names of variables.
• The word that starts the line int, is an abbreviation for the word
  integer and it tell the computer that the numbers named by these
  variables will be integers.
• The remaining lines are all instructions that tell the computer to do
  something. These instructions are called statements or executable
  statements.
• Most of the statements begin with either the word cin or cout.
  These statements are input and output statement.
• The statement that begin with cin tell the computer what to do
  when information is entered from the keyboard.
• The word cout is used for output.
• The arrows << or >> tell you the direction that the data is
  moving. The arrows, << and >> are called “insert” and “extract”
  or put to and get from respectively.
•   E.g cout << “Press return after entering a number. \n”;
•   This line may be read, put “ Press … number \n” to cout.
•   cin >> number_of_pods;
•   This line may be read, “get number of pods from cin”.
1.3.3 Layout of a simple C++ Program
          #include <iostream.h>
          int main()
          {
                    Variable_Declarations
                   Statement_1
                   Statement_2
                     …..
                   Statement_Last
                   return 0;
          }
                   #include <iostream.h>
• The first line
• is called an include directive. It tell the computer where to find
  information about certain items that are used in your program.
• In this case iostream is the name of a library that contains the
  definitions of the routines that handle input from the keyboard
  and output to the screen. iostream.h is a file that contains
  some basic information about this library.
• The linker program combines the object code for the library
  iostream and the object code for the program you write.
• The second and third nonblank lines simply say that the main
  function of the program starts here.
      int main()
      {
• The braces { and } mark the beginning and end of the main
  function of the program.
• The next-to-last line
       return 0;
• says to “end the program when you get here”
• Some compilers will allow you to omit this line, others will
  insist that you include it. It is best to get in the habit of
  including it. This line is called a return-statement and it is
  considered to be an executable statement.
1.3.4 Compiling and running a C++ program
• You write a C++ program using a text editor in the same way that you
  write any other document. The program is kept in a file just like any
  other document.
• The way you compile and run a C++ program also depends on the
  particular system you are using
• You will need to learn how to give the commands to compile, link and
  run a C++ program on your system.
• When you give the command to compile your program this will
  produce a machine-language translation of your C++ program. This
  translated version of your program is called the object code for your
  program.
• The object code for your program must be linked with the object code
  for routines (e.g. input and output routines) written for you.
1.4 Testing and Debugging
• A mistake in a program is called a bug and the process of
  eliminating bugs is called debugging.
1.4.1 Kinds of Program Errors
• The compiler will catch certain kinds of mistakes and will write
  out an error message when it finds a mistake. It will detect what
  are called syntax errors since they are, by and large violation of
  the syntax of the programming language, such as omitting a
  semicolon.
• If your program contains something that is a direct violation of
  the syntax rules for your programming language, the compiler
  will give you an error message.
• Sometimes the compiler will give you only a warning message which
  indicates that you have done something that is not technically
  speaking, a violation of the programming language syntax rules, but
  that is unusual enough to indicate a likely mistake.
• There are certain kinds of errors that the computer system can detect
  only when a program is run. These are called run-time errors. Many
  run time errors have to do with numeric calculations.
• If the compiler approved of your program and the program ran once
  with no run-time error messages, this does not guarantee that your
  program is correct.
• Mistakes in the underlying algorithm or in translating the algorithm
  into the C++ language are called logic errors.