Chapter 1 Notes: An Overview of Computers and Programming Languages

Introduction to Computers and Programming Languages

  • Without software, a computer is useless; software is developed with programming languages.
  • C++ is a programming language; C++ is suited for a wide variety of programming tasks.

The Computer: Hardware and Software

  • Elements of a computer system:
    • Hardware
    • Software
  • Two main components of a computer system: hardware and software (the software enables hardware to perform tasks).

The Language of a Computer

  • Signals types:
    • Analog signals: continuously varying wave forms.
    • Digital signals: sequences of 0s and 1s.
  • Machine language: language of a computer consisting of 0s and 1s.
  • Key terms:
    • Bit: the binary digit, 0 or 1.
    • Binary code: a sequence of 0s and 1s.
    • Byte: a sequence of eight bits.
  • Byte and character encoding:
    • ASCII (American Standard Code for Information Interchange) encodes 128 characters.
    • Unicode is a coding scheme for more characters; 65,536 characters are supported, stored with two bytes (16 bits) per character.
  • Example encodings:
    • A is encoded as 1000001 (66th character) [as stated in the transcript].
    • The character 3 is encoded as 0110011.
  • Number systems:
    • The decimal system (base 10) is used in daily life.
    • The computer uses the binary (base 2) number system.
  • Binary units (from small to large):
    • 1 Byte = 8 bits.
    • 1 Kilobyte (KB) = 2^{10} bytes.
    • 1 Megabyte (MB) = 2^{20} bytes.
    • 1 Gigabyte (GB) = 2^{30} bytes.
    • 1 Terabyte (TB) = 2^{40} bytes.
    • 1 Petabyte (PB) = 2^{50} bytes.
    • 1 Exabyte (EB) = 2^{60} bytes.
    • 1 Zettabyte (ZB) = 2^{70} bytes.
  • Quick numerical reference (as presented):
    • KB = 2^{10} bytes
    • MB = 2^{20} bytes
    • GB = 2^{30} bytes
    • TB = 2^{40} bytes
    • PB = 2^{50} bytes
    • EB = 2^{60} bytes
    • ZB = 2^{70} bytes

The Evolution of Programming Languages

  • Early computers programmed in machine language using sequences of binary instructions (e.g., wages calculation shown as binary sequences).
    • Example (machine language): //Load 100100 010001; //Multiply 100110 010010; //Store 100010 010011
  • Assembly language:
    • Mnemonic form of instructions.
    • An assembler translates assembly language to machine language.
    • Example: wages = rate * hours can be written as:
    • LOAD rate
    • MULT hours
    • STOR wages
  • High-level languages include: Basic, FORTRAN, COBOL, C, C++, C#, Java, Python.
  • A compiler translates a program written in a high-level language into machine language.
  • In C++, the equation for weekly wages can be written as:
    • extwages=extrateimesexthoursext{wages} = ext{rate} imes ext{hours}
  • The shift from low-level to high-level languages increased portability and productivity.

Processing a C++ Program

  • Steps to process a C++ program (1 of 4):
    1. Use a text editor to create the source code (source program) in C++.
    2. Include preprocessor directives (begin with the symbol #) processed by the preprocessor.
    3. Use the compiler to: check language compliance and translate to machine language (object program).
    4. Use an Integrated Development Environment (IDE) to develop programs in a high-level language; IDE provides libraries and prewritten code.
    5. A linker combines the object program with library resources to create executable code.
    6. The loader loads the executable program into main memory.
    7. The last step is to execute the program.
  • Processing a C++ program (2 of 4) and IDEs:
    • IDEs are user-friendly.
    • The compiler identifies syntax errors and suggests corrections.
    • Build or Rebuild is a simple command that links the object code with resources used from the IDE.
  • Processing a C++ program (4 of 4) references:
    • FIGURE 1-2 shows Processing a C++ program.

Problem-Solving Cycle: Analysis–Coding–Execution

  • Programming is a process of problem solving.
  • An algorithm is a step-by-step problem-solving process.
  • A solution is achieved in a finite amount of time.
  • FIGURE 1-3 illustrates the Problem analysis–coding–execution cycle.
The Problem Analysis–Coding–Execution Cycle (1 of 5)
  • Step 1: Analyze the problem.
    • Outline the problem and its requirements.
    • Design steps (algorithm) to solve the problem.
  • Step 2: Implement the algorithm.
    • Implement the algorithm in code.
    • Verify that the algorithm works.
  • Step 3: Maintain the program.
    • Use and modify the program if the problem domain changes.
The Problem Analysis–Coding–Execution Cycle (2 of 5)
  • Analyze the problem using these steps:
    • Step 1: Thoroughly understand the problem and all requirements.
    • Step 2: Understand the problem requirements:
    • Does the program require user interaction?
    • Does the program manipulate data?
    • What is the output?
    • Step 3: If complex, divide the problem into subproblems.
    • Analyze and design algorithms for each subproblem.
    • Check the correctness of the algorithm.
    • Test the algorithm with sample data.
    • Some mathematical analysis might be required.
The Problem Analysis–Coding–Execution Cycle (3 of 5)
  • Once the algorithm is designed and correctness is verified:
    • Write the equivalent code in a high-level language.
    • Enter the program using a text editor.
The Problem Analysis–Coding–Execution Cycle (4 of 5)
  • Run code through the compiler.
  • If the compiler generates errors:
    • Look at the code and fix errors.
    • Run code again through the compiler.
  • If there are no syntax errors:
    • The compiler generates equivalent machine code.
    • Link machine code with the system’s resources (performed by the linker).
The Problem Analysis–Coding–Execution Cycle (5 of 5)
  • Once compiled and linked, the loader places the program into main memory for execution.
  • The final step is to execute the program.
  • The compiler guarantees that the program follows the rules of the language; it does not guarantee that the program will run correctly.

Examples

  • Example 1-1 (1 of 2): Design an algorithm to find the perimeter and area of a rectangle.

    • Perimeter formula: extperimeter=2imes(extlength+extwidth)ext{perimeter} = 2 imes ( ext{length} + ext{width})
    • Area formula: extarea=extlengthimesextwidthext{area} = ext{length} imes ext{width}
  • Example 1-1 (2 of 2): Algorithm steps:

    • Get the length of the rectangle.
    • Get the width of the rectangle.
    • Compute perimeter using the perimeter formula.
    • Compute area using the area formula.
  • Example 1-5 (1 of 4): Calculate each student’s grade.

    • There are 10 students in a class.
    • Each student has taken five tests; each test is worth 100 points.
    • Design algorithms to: calculate the grade for each student and class average, find the average test score, determine the grade, and use the provided data (students’ names and test scores).
  • Example 1-5 (2 of 4): Algorithm to determine the average test score:

    • Get the five test scores.
    • Sum the five test scores: the sum is represented by sum.
    • Suppose average stands for the average test score: extaverage=extsum5.ext{average} = \frac{ ext{sum}}{5}.
  • Example 1-5 (3 of 4): Algorithm to determine the grade:

    • If average ≥ 90, grade = A
    • Else if average ≥ 80 and < 90, grade = B
    • Else if average ≥ 70 and < 80, grade = C
    • Else if average ≥ 60 and < 70, grade = D
    • Else, grade = F
  • Example 1-5 (4 of 4): Main algorithm (pseudocode):

    1. totalAverage = 0;
    2. Repeat for each student:
    • Get student’s name
    • Use the average algorithm to find the student’s average test score
    • Use the grade algorithm to determine the student’s grade
    1. Update totalAverage by adding the current student’s average test score
    2. Determine the class average as follows: extclassAverage=exttotalAverage10.ext{classAverage} = \frac{ ext{totalAverage}}{10}.

Programming Methodologies

  • Two popular approaches to programming design:
    • Structured programming
    • Object-oriented programming (OOP)

Structured Programming

  • Structured design involves dividing a problem into smaller subproblems.
  • Structured programming involves implementing a structured design.
  • The structured design approach is also called:
    • Top-down (or bottom-up) design
    • Stepwise refinement
    • Modular programming

Object-Oriented Programming (OOD) (1 of 3)

  • Object-oriented design identifies components called objects and determines how objects interact with each other.
  • Specify relevant data and possible operations to be performed on that data.
  • Each object consists of data and operations on that data.

Object-Oriented Programming (OOD) (2 of 3)

  • An object combines data and operations on the data into a single unit.
  • A programming language that implements OOD is called an object-oriented programming (OOP) language.
  • To design and use objects, you must learn how to:
    • Represent data in computer memory
    • Manipulate data
    • Implement operations

Object-Oriented Programming (OOD) (3 of 3)

  • To create operations:
    • Write algorithms and implement them in a programming language
    • Use functions to implement algorithms
  • Learn how to combine data and operations on the data into a single unit called a class.
  • C++ was designed to implement OOD; OOD is used with structured design.

ANSI/ISO Standard C++

  • C++ evolved from C.
  • C++ designed by Bjarne Stroustrup at Bell Laboratories in the early 1980s.
  • Many different C++ compilers were available; programs were not always portable from one compiler to another.
  • In mid-1998, ANSI/ISO C++ language standards were approved.
  • The second standard, called C++11, was approved in 2011.

Quick Review (1 of 3)

  • A computer is an electronic device that can perform arithmetic and logical operations.
  • A computer system has hardware and software components.
  • The central processing unit (CPU) is the brain.
  • Primary storage (MM) is volatile; secondary storage (e.g., disk) is permanent.
  • The operating system monitors the overall activity of the computer and provides services.
  • There are various kinds of languages.

Quick Review (2 of 3)

  • A compiler translates a high-level language into machine code.
  • Algorithm: a step-by-step problem-solving process that arrives at a solution in a finite amount of time.
  • Problem-solving process:
    1. Analyze the problem and design an algorithm.
    2. Implement the algorithm in code.
    3. Maintain the program.

Quick Review (3 of 3)

  • Structured design: the problem is divided into smaller subproblems; each subproblem is solved; then solutions are combined.
  • Object-oriented design (OOD) program: a collection of interacting objects.
  • Object: data and operations on those data.

Note on Figures and Examples

  • FIGURE 1-1 illustrates Hardware components of a computer and main memory.
  • FIGURE 1-2 illustrates the processing of a C++ program.
  • FIGURE 1-3 shows the Problem analysis–coding–execution cycle.
  • Example data and procedures are presented to illustrate the problem-solving workflow (rectangle perimeter/area, grade calculation, etc.).

Important Formulas and Notations (summary)

  • Perimeter of rectangle: extperimeter=2imes(extlength+extwidth)ext{perimeter} = 2 imes ( ext{length} + ext{width})
  • Area of rectangle: extarea=extlengthimesextwidthext{area} = ext{length} imes ext{width}
  • Class average calculation (example): extclassAverage=exttotalAverage10ext{classAverage} = \frac{ ext{totalAverage}}{10}
  • Average of five tests: extaverage=extsum5ext{average} = \frac{ ext{sum}}{5}
  • Wages example in high-level language: extwages=extrateimesexthoursext{wages} = ext{rate} imes ext{hours}
  • Memory size references (binary units):
    • 1extKB=210extbytes1 ext{ KB} = 2^{10} ext{ bytes}
    • 1extMB=220extbytes1 ext{ MB} = 2^{20} ext{ bytes}
    • 1extGB=230extbytes1 ext{ GB} = 2^{30} ext{ bytes}
    • 1extTB=240extbytes1 ext{ TB} = 2^{40} ext{ bytes}
    • 1extPB=250extbytes1 ext{ PB} = 2^{50} ext{ bytes}
    • 1extEB=260extbytes1 ext{ EB} = 2^{60} ext{ bytes}
    • 1extZB=270extbytes1 ext{ ZB} = 2^{70} ext{ bytes}
  • Unicode storage: 65{,}536 characters; two bytes (16 bits) per character.
  • ASCII encoding: 128 characters.