## Chapter 1: Introduction

## Chapter Goals

- To understand the activity of programming
- To learn about the architecture of computers
- To learn about machine languages and higher-level programming languages
- To become familiar with your compiler
- To compile and run your first C++ program
- To recognize syntax and logic errors

## What is a Computer?

- Computers can handle repetitive chores without becoming bored or exhausted.
- A computer is programmable to handle different tasks:
  - Balancing checkbooks
  - Processing words
  - Playing Games
- Computer actions are composed of huge numbers of extremely primitive operations
- The computer gives the illusion of smooth interaction because it executes these operations a great speed.
- Computers are flexible: they can handle a wide range of tasks.

## What is Programming?

- A program tells the computer the sequence of steps needed to fulfill a task.
- A programmer designs and implements these programs.
- Most computer users are not computer programmers.
- Programming is an essential skill for a computer scientist.
- Programming is not the only skill required to be a successful computer scientist.
- Early programming experiences are mundane and will not rival sophisticated software that you are familiar with.
- Once certain skills are developed, even simple programs can be thrilling.

## The Anatomy of a Computer (CPU, Memory, and bus)

- CPU (Central Processing Unit):
  - Plastic, metal and mostly silicon.
  - Composed of several million transistors.
  - Enormously complicated wiring.
  - Performs program control, arithmetic, and data movement.
  - Locates and executes program instructions.
- Memory:
  - RAM (Random Access Memory): read-write memory.
  - ROM (Read Only Memory): contains certain programs that must always be present.
  - Secondary storage (e.g. a hard drive) provides persistent storage.
- The bus is a set of electrical lines that connect the CPU, RAM, and other devices.

## The Anatomy of a Computer (Motherboard, Peripherals, and Networks)

- The motherboard holds the CPU, memory, and bus, as well as card slots which connect peripherals to the computer.
- Peripherals allow the user and computer to interact:
  - Printer
  - Mouse
  - Keyboard
  - Modem
- Some computers are connected to one another through networks.

## The Anatomy of a Computer (Schematic)

## Translating Human-Readable Programs to Machine Code (Machine Instructions)

- Machine instructions:
  - Extremely primitive:
  - Encoded as numbers:

        161 40000 45 100 127 11280

  - Tedious and error prone to look up numeric codes and enter them manually.
  - Each processor has its own set of machine instructions.

## Translating Human-Readable Programs to Machine Code (Assembler)

- Assembler
  - Assigns short names to commands:

        mov 40000, %eax
        sub 100, %eax
        jg 11280

  - Makes reading easier for humans.
  - Translated into machine instructions by another program.
  - Still processor dependent

## Translating Human-Readable Programs to Machine Code (Higher-level Languages)

- Higher-level languages:
  - Independent of the underlying hardware.
  - Easiest for humans to read and write.

        if (int_rate > 100) message_box("Interest rate error");

  - Translated by compilers into machine instructions.

## Programming Languages

- Compilers follow strict conventions regarding programming language use.
- Strict rules prevent more disastrous errors that could arise should the compiler "guess."
- There are many (hundreds of) programming languages.
- Differences in programming languages are sometimes slight, but at other times substantial.
  - C++:

        if (int_rate > 100) message_box("Interest rate error");

  - Pascal:

        if int_rate > 100 then message_box('Interest rate error');

## Programming Language Design and Evolution (Specific and General Purpose Languages)

- Many programming languages are created with a specific purpose.
  - database processing.
  - "artificial intelligence."
  - multimedia processing.
- Special-purpose languages are not used much beyond their area of specialization.
- General purpose languages can be used for any task.
  - Pascal: designed as a teaching language (purposefully kept simple).
  - C: developed to be translated efficiently into fast machine code, with a minimum of housekeeping.
  - C++: adds "object oriented programming" aspects to C.

## Programming Language Design and Evolution (Incremental Growth)

- C and C++ are languages that grew in increments; as the users of the language perceived shortcomings, they added features:
  - 1972 - original design of C. Many compilers add different features, sprouting dialects of C.
  - 1985 - Bjarne Stroustrup of AT&T adds object oriented features to C, creating C++.
  - 1989 - ANSI standard of C developed to resolve differences among the several version of C.
  - 1998 - Standards developed for most recent version of C++.
- Grown languages tend to be messy; different features are added by people with different tastes.
- Grown languages are modified in a hurry to solve a specific need, without considering the consequences.
- Grown languages accumulate a patchwork of features that do not necessarily interact well together.
- Grown languages have an edge in the marketplace (C++ allows programmers to use new features without throwing away old C code).

## Programming Language Design and Evolution (Planned Languages)

- Planned languages (such as Pascal and Ada) are carefully designed to anticipate the needs of future users.
- More thought goes into planned languages.
- Features of planned languages tend to be more related to one another.
- Designers pay more attention to readability and consistency.
- Well designed languages are easier to use and learn.

## Becoming Familiar with Your Computer

- Log In
- Locate the C++ compiler
- Understand Files and Folders
- Write a Simple Program
- Save Your Work

## Compiling a Simple Program ("Hello, World!")

## Compiling a Simple Program

- C++ is case sensitive
- C++ has freeform layout
  - Good taste dictates that you use readable program layout.

## Compiling a Simple Program (Syntax)

Syntax 1.1 : Simple Program
    header files
    using namespace std;
    int main()
    {
       statements
       return 0;
    }
Example: |
    #include <iostream>
    using namespace std;
    int main()
    {
       cout << "Hello, World!\n";
       return 0;
    }
Purpose: | A simple program, with all program instructions in a `main` function.

## Compiling a Simple Program

- `#include<iostream>` - read the file `iostream` that contains the definition for the stream input/output package.
- `using namespace std;` - all names in the program belong to the "standard namespace"
- `int main () { ... }` defines a function called `main`. A function is a collection of instructions that carry out a particular task.
- The statements (instructions) inside the body (inside the curly braces `{ ... }`) of the main function are executed one by one.
- Each statement ends with a semicolon ;
- The sequence of characters enclosed in quotations marks (`"Hello, World\n"`) is called a string.
  - Escape sequence`\n` indicates a newline.
  - Use `\"` to display quotes.
- `return` denotes the end of the main function. The zero value is a signal that the program ran successfully.

## Errors (Syntax and Logic Errors)

- Syntax error or compile-time error:
  - Something violates the language rules.
  - Compiler finds the error.

        cot << "Hello, World!\n";
        cout << "Hello, World!\";

- Logic error or run-time error:
  - Program doesn't do what it's supposed to do.
  - Program author must test and find the error.

        cout << "Hell, World\n";

## Errors

- Structuring programs so that an error in place does not trigger a disastrous response is called defensive programming.
- A program error is commonly called a bug; special software tools called debuggers help you locate bugs.
- Overflow and roundoff errors can occur when the result of a calculation is outside a computer's numeric range (i.e. either very big or very small).

## The Compilation Process (Diagram)

## The Compilation Process (Definitions)

- The C++ source code is your program.
- The compiler is a program that translate your C++ code into object code.
- Object code consists of machine instructions and information on how to load the program into memory.
- A linker program takes the object code from your program and the code from the various libraries and builds them into an executable file.
- Libraries contain the (already translated) code used by your program (such as `iostream`).

## The Compilation Process (Edit - Compile - Debug Loop)

## Algorithms

- A planning process must precede implementing a computer program.
- The computer needs a systematic approach for finding solutions: a series of steps that involves no guesswork.
- An algorithm is a solution technique that is unambiguous, executable, and terminating.
  - Unambiguous - no room for guesswork or creativity.
  - Executable - each step can be carried out in practice.
  - Terminating - the process will eventually come to an end.

## Algorithms (Example)

> You put $10,000 into a bank account that earns 5% interest per year. How many years does it take for the account balance to be double the original?

- Step 1: Start with the table

  After Year | Balance
  0 | $10,000

- Step 2: Repeat steps 2a-2c while balance < $20,000

  - Step 2a. Add a new row to the table

  - Step 2b. In column 1 of the new row, put one more than the preceding year's value

  - Step 2c. In column 2, place the value of the preceding balance value, multiplied by 1.05.

- Step 3: Report the last number in the year column as the number of years required to double the investment