44
Computer Programming Belaid Moa Email: [email protected] Phone: 556-1852 Office : ECS 115 Spring 2008

Computer Programming Belaid Moa Email: [email protected]@qu.edu.qa Phone: 556-1852 Office : ECS 115 Spring 2008

  • View
    223

  • Download
    3

Embed Size (px)

Citation preview

Computer Programming

Belaid MoaEmail: [email protected]: 556-1852Office : ECS 115Spring 2008

Computer Programming2

Course materials

Required textbook– C++: Introduction to Computing, 3rd Edition, by Adams &

Nyhoff , Prentice-Hall 2003 Other References textbooks

– B. Stroustrup: The C++ Programming Language (3rd edition). Addison Wesley Longman, 1997.

– The C++ reference site: http://www.cplusplus.com/

Computer Programming3

Course objectives

Learn fundamentals of Procedural Programming (control structures and modularity), applicable to most languages: Fortran, Basic, Perl, Assembler, etc.

Learn to implement algorithms to solve problems by writing programs in the programming languages: C and C++.

Introduce the concepts required for Object-Oriented Programming.

Computer Programming4

Your participation

Midterm 15%

Quizzes and class participation

10%

Assignments (2) 15%

Lab 30%

Final Exam 30%

Total 100%

Computer Programming5

Your feedback

Teaching/learning is interactive– two-way communications

Let me know– what you think about lectures, projects, labs, exams, topics,

…– What you want to know or probe further

You can reach me– in class, during office hours, by email/phone

Computer Programming6

Agenda of the course

Overview of computer science field Basic Programming Structure Operators and build in functions Control flow (if, switch, while, for) User defined functions Arrays & strings Pointers Object-Oriented Programming Files and other I/O operations

Computer Programming7

First things First: How to Study

There are no such things as– “I am stupid. I can never understand this material”– “The others are smarter than me. I am just a loser.”– “My capacity is limited, and my brain can’t handle it.”

Everyone of us is capable of achieving anything given– Enthusiasm– Perseverance, especially in CG– Patience, especially in CG– Time organization– Suitable style of learning

Computer Programming8

First things First: How to Study

The killer of them all: Procrastination– Excuses for not studying and doing your homework:

“I can’t study now because I have to get a haircut.” “I can’t do the project because I have other projects.” “I can’t review the CS373 lectures because I have to review only

for this coming exam.” “It is Okay to leave things until the day of exam.”

– The cure: “Do not leave the work of today until tomorrow for tomorrow has its

own work.” Reward yourself: “After I am done with this, I should get a nice

haircut.” Start with things that you feel good at.

Computer Programming9

First things First: How to Study

How to be a good student– People think and learn differently:

Eyes: Visual learner Ears: Auditory learner Order: sequential learner Images: global learner Doing: kinesthetic learner

– Find your own style of learning Write sequential notes Use mind-map technique Draw images Explain loudly the lecture to yourself The read the lecture while walking Write and run the code to see how things work

Computer Programming10

How to be a good student

Find your optimal style of learning Do not miss a single lecture Be active during the lecture:

– Write notes– Ask questions

Study your notes– Just after the lecture– At least three times during the week of the lecture– Once after two weeks– Once after three weeks and so on

Organize your time– Do not over-study for a course at the expense of the other courses– Try to give each course an amount of time every day– Do not leave the review and the project until one day before the deadline– Do not procrastinate!

Use office hours as much as possible Take time to sharpen your axe

Computer Programming11

Overview

Computers have two kinds of components: Hardware – physical devices such as

– CPU– memory– storage devices

Software – programs such as– Operating system– applications– utilities

Computer Programming12

Hardware

Computer Programming13

Hardware

Computer Programming14

Hardware: CPU

Central Processing Unit (CPU):– the “brain” of the machine– Circuitry that performs arithmetic and logical ML

statements

CPU measurement – Speed in gigahertz (109 clock-ticks per second)

Examples– Intel Pentium, AMD K6, Motorola PowerPC, Sun

SPARC,

Computer Programming15

Storage

Random Access Memory (RAM)– “Main” memory, which is fast, but volatile...– Analogous to a person’s short-term memory.– Many tiny “on-off” switches

“on” is represented by 1, “off” by 0.

Each switch is called a binary digit, or bit.– 8 bits is called a byte.– 210 bytes =1024 bytes is called a kilobyte (1K)– 220 bytes is called a megabyte (1M).

Computer Programming16

Storage

Secondary Memory (Disk):– Stable storage using magnetic or optical media.– Analogous to a person’s long-term memory.– Slower to access than RAM.

Examples: – floppy disk (measured in kilobytes (210 bytes))– hard disk (measured in gigabytes (230 bytes))– CD-ROM (measured in megabytes (220 bytes)), ...

Computer Programming17

Input and Output

Input devices– Instructions and data must be encoded in binary

form and transmitted to the CPU

Examples:– keyboard– mouse, trackball, joystick– scanner– audio, video capture boards

Computer Programming18

Input and Output

Output devices– Convert data from binary to another form

Examples– monitors, printers– sound, video– robotics control

Communication between CPU and peripheral devices is through ports– Ports communicate via the system bus

Computer Programming19

Hardware: the Bus

The Bus:– Connects CPU to other hardware devices.– Analogous to a person’s spinal cord.

Speed measured in megahertz (millions of clock-ticks per second)– Typically much slower than the CPU...– The bottleneck in most of today’s PCs.

Computer Programming20

Hardware: Cache

Access speed of RAM – Faster than accessing secondary memory– Still quite slow, relative to the rate at which the CPU runs.

Solution:– Add a fast cache memory to the CPU– Store recently used instructions and data

Assumption:– These instructions/data were needed recently– They will be needed again in the near future.

Computer Programming21

Hardware: Summary

Putting the pieces together:

CPU

Bus

MainMemory

SecondaryMemory

cache

Program storage– Long-term in secondary memory– Loaded into main memory to run– From which the CPU retrieves and executes their statements.

Computer Programming22

Software: OS

Operating system (OS)– Loaded from secondary memory into main memory

when the computer is turned on,– Remains in memory until the computer is turned off.

DiskRAMCPUCache

Bus

OS

Computer Programming23

Software: OS

OS acts as the “manager” of the system, – Making sure that each hardware device interacts

smoothly with the others.

Provides an interface – Enables user to interact with the computer, – Awaits user input if no application is running.

Examples: MacOS, Windows-95, Windows-NT, UNIX, Linux, Solaris, ...

Computer Programming24

Software: Applications

Applications are non-OS programs – Perform some useful task– Including word processors, spreadsheets,

databases, web browsers, C++ compilers

Example C++ compilers/environments: – CodeWarrior (MacOS, Win95, WinNT, Solaris) – GNU C++ (UNIX, Linux)– Turbo/Borland C++ (Win95, WinNT)– Visual C++ (Win95, WinNT)

Computer Programming25

Software: User Programs

Programs that are neither OS programs nor applications are called user programs.

User programs are what you’ll be writing in this course.

A user program is supposed to solve a problem!– Is the problem solvable?– Is the problem efficiently solvable?

Computer Programming26

What is Programming?

Computer Program– A sequence of statements that instruct a computer in how to

solve a problem. Programming

– The act of designing, writing, and maintaining a program Programmers

– People who write programs Hackers

– People who enjoy exploring the details of computers and how to stretch their capabilities

– Do you want to be one?

Computer Programming27

What kinds of statementsdo computers understand?

A computer only understands machine language statements.

Characteristics of machine language– A sequence of ones and zeros – Cause the computer to perform a particular action,

such as add, subtract, multiply, ...

Computer Programming28

Machine Language (ML)

ML statements – Stored in a computer’s memory

Computer memory is a sequence of switches. – an “on” switch is represented by 1, – an “off” switch is represented by 0.

ML thus appears to be binary (base-2):

0010111010110101

Computer Programming29

Early Computers

... required a programmer to write in ML... Problem

– Easy to make mistakes!– Such mistakes are hard to find!– Not portable -- only runs on one kind of machine!

Programming was very difficult!

Computer Programming30

A Bright Idea

Devise a set of abbreviations (mnemonics) corresponding to the ML statements

Create a program (the assembler) to translate them into ML.

ADD 34, R1MOVE R1,1200CMPR R1, R2

100111001110010110100110110111

Assembler

Assembly languagestatements (mnemonics)

Machine language statements

Computer Programming31

Advantages and Disadvantages

More natural than binary.

Much easier to read programs

Much easier to find and fix mistakes

Still not portable to different machines

Each machine had its own set of mnemonics

Each had its own assembler

Computer Programming32

High Level Languages

Improvement on assembly language– Devise a set of statements that are close to human

language (if, while, do, ...)– Create a program to translate them into ML.

The set of statements is called a high-level language (HLL)

The translation program is called a compiler. A linker is sometimes needed! Sometimes we use interpreters instead of

compilers!

Computer Programming33

Compiler Versus Interpreter

Source code Compiler Machine language

A Compiler translates a file written in a high-level language into a machine language file that can be executed on the computer.

An interpreter executes each high-level statement directly. On fly, the interpreter translates the statement to the corresponding machine language instructions and executes/runs them.

Computer Programming34

Contrast Assembler and HLL Compiler

Assembler translates one mnemonic into one ML statement

Compiler translates one HL statement into several ML statements

1010110011110101

0000000000010000

0010111010110101

0000000000010010

0010111011111101

0000000000010100

z = x + y; Compiler

Computer Programming35

Advantage of HLLs

Programs are easier to read Errors are easier to find Programs are portable from one computer to

another– Assumes the language is standard– Just create a new compiler which does the

translation into the correct ML

Computer Programming36

Objectives in Programming A program should solve a problem:

Correctly Efficiently Readably In user-friendly

fashion

It actually solves the problem Without wasting time or space Understandable by another

person In a way that is easy for its user

to use

Computer Programming37

Summary of "Levels" of Computer Languages

Low levelML in binary language

Medium Level Assembly language

High Levelas in C++

Hard to read, not portable

Mnemonics, easier to read, still not portable

Reads like English and algebra, portable

Computer Programming38

Putting It All Together

Programs and applications that are not running are stored on disk.

App

DiskRAMCPUCache

Bus

OS

Computer Programming39

Putting It All Together

When you launch a program– OS controls the CPU – Loads the program from disk to RAM.

App

DiskRAMCPUCache

Bus

OS

App

Computer Programming40

The OS then relinquishes the CPU to the program, Application program begins to run.

App

DiskRAMCPUCache

Bus

OS

AppApp

Putting It All Together

Computer Programming41

The Fetch-Execute Cycle

CPU repeatedly fetches the next instruction (from memory / cache), Executes it Stores any results back to memory.

That’s all a computer does: fetch-execute-store, millions of times each second!

App

DiskRAMCPUCache

Bus

OS

AppApp

Computer Programming42

First program: Hello World!

#include <iostream>

using namespace std;

int main() {

cout << “Hello World!”<<endl;

return 0;

}

Computer Programming43

First program: Hello World!

Save the previous program as hello.cpp Compile it:

– g++ -o hello hello.cpp– Build button

Run it:– hello– Run button

Computer Programming44

First Interesting Problem

Write a program that computes the volume of a sphere with a radius specified by the user.

To do in class!