Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming Belaid Moa Phone: 556-1852 Office : ECS 115 Spring 2008.

Similar presentations


Presentation on theme: "Computer Programming Belaid Moa Phone: 556-1852 Office : ECS 115 Spring 2008."— Presentation transcript:

1 Computer Programming Belaid Moa Email: Belaid.Moa@qu.edu.qaBelaid.Moa@qu.edu.qa Phone: 556-1852 Office : ECS 115 Spring 2008

2 Computer Programming 2 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/

3 Computer Programming 3 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.

4 Computer Programming 4 Your participation Midterm15% Quizzes and class participation 10% Assignments (2)15% Lab30% Final Exam30% Total100%

5 Computer Programming 5 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

6 Computer Programming 6 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

7 Computer Programming 7 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

8 Computer Programming 8 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.

9 Computer Programming 9 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

10 Computer Programming 10 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

11 Computer Programming 11 Overview Computers have two kinds of components: Hardware – physical devices such as – CPU – memory – storage devices Software – programs such as – Operating system – applications – utilities

12 Computer Programming 12 Hardware

13 Computer Programming 13 Hardware

14 Computer Programming 14 Hardware: CPU Central Processing Unit (CPU): – the “brain” of the machine – Circuitry that performs arithmetic and logical ML statements CPU measurement – Speed in gigahertz (10 9 clock-ticks per second) Examples – Intel Pentium, AMD K6, Motorola PowerPC, Sun SPARC,

15 Computer Programming 15 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. – 2 10 bytes =1024 bytes is called a kilobyte (1K) – 2 20 bytes is called a megabyte (1M).

16 Computer Programming 16 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 (2 10 bytes)) – hard disk (measured in gigabytes (2 30 bytes)) – CD-ROM (measured in megabytes (2 20 bytes)),...

17 Computer Programming 17 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

18 Computer Programming 18 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

19 Computer Programming 19 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.

20 Computer Programming 20 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.

21 Computer Programming 21 Hardware: Summary Putting the pieces together: CPU Bus Main Memory Secondary Memory cache Program storage – Long-term in secondary memory – Loaded into main memory to run – From which the CPU retrieves and executes their statements.

22 Computer Programming 22 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. Disk RAMCPU Cache Bus OS

23 Computer Programming 23 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,...

24 Computer Programming 24 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)

25 Computer Programming 25 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?

26 Computer Programming 26 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?

27 Computer Programming 27 What kinds of statements do 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,...

28 Computer Programming 28 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

29 Computer Programming 29 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!

30 Computer Programming 30 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, R1 MOVER1,1200 CMPRR1, R2 1001110011 1001011010 0110110111 Assembler Assembly language statements (mnemonics) Machine language statements

31 Computer Programming 31 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

32 Computer Programming 32 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!

33 Computer Programming 33 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.

34 Computer Programming 34 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

35 Computer Programming 35 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

36 Computer Programming 36 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

37 Computer Programming 37 Summary of "Levels" of Computer Languages Low level ML in binary language Medium Level Assembly language High Level as in C++ Hard to read, not portable Mnemonics, easier to read, still not portable Reads like English and algebra, portable

38 Computer Programming 38 Putting It All Together Programs and applications that are not running are stored on disk. App Disk RAMCPU Cache Bus OS

39 Computer Programming 39 Putting It All Together When you launch a program – OS controls the CPU – Loads the program from disk to RAM. App Disk RAMCPU Cache Bus OS App

40 Computer Programming 40 The OS then relinquishes the CPU to the program, Application program begins to run. App Disk RAMCPU Cache Bus OS App Putting It All Together

41 Computer Programming 41 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 Disk RAMCPU Cache Bus OS App

42 Computer Programming 42 First program: Hello World! #include using namespace std; int main() { cout << “Hello World!”<<endl; return 0; }

43 Computer Programming 43 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

44 Computer Programming 44 First Interesting Problem Write a program that computes the volume of a sphere with a radius specified by the user. To do in class!


Download ppt "Computer Programming Belaid Moa Phone: 556-1852 Office : ECS 115 Spring 2008."

Similar presentations


Ads by Google