460 likes | 547 Views
Computer Programming. Belaid Moa Email: Belaid.Moa@qu.edu.qa Phone: 556-1852 Office : ECS 115 Spring 2008. Course materials. Required textbook C++: Introduction to Computing, 3rd Edition, by Adams & Nyhoff , Prentice-Hall 2003 Other References textbooks
E N D
Computer Programming Belaid Moa Email: Belaid.Moa@qu.edu.qa Phone: 556-1852 Office : ECS 115 Spring 2008
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 Programming
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 Programming
Your participation Computer Programming
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 Programming
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 Programming
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 Programming
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 Programming
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 Programming
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 Programming
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 Programming
Hardware Computer Programming
Hardware Computer Programming
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 Programming
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 Programming
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 Programming
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 Programming
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 Programming
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 Programming
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 Programming
Hardware: Summary Putting the pieces together: CPU Main Memory Secondary Memory cache Bus • Program storage • Long-term in secondary memory • Loaded into main memory to run • From which the CPU retrieves and executes their statements. Computer Programming
Disk CPU RAM Cache OS Bus 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. Computer Programming
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 Programming
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 Programming
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 Programming
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 Programming
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 Programming
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 Programming
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 Programming
Assembly languagestatements (mnemonics) Machine language statements A Bright Idea • Devise a set of abbreviations (mnemonics) corresponding to the ML statements • Create a program (the assembler) to translate them into ML. 100111001110010110100110110111 ADD 34, R1MOVE R1,1200CMPR R1, R2 Assembler Computer Programming
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 Advantages and Disadvantages Computer Programming
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 Programming
Compiler Versus Interpreter • A Compiler translates a file written in a high-level language • into a machine language file that can be executed on the computer. Compiler Machine language Source code • 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 Programming
Assembler translates one mnemonic into one ML statement Compiler translates one HL statement into several ML statements Contrast Assembler and HLL Compiler 1010110011110101 0000000000010000 0010111010110101 0000000000010010 0010111011111101 0000000000010100 Compiler z = x + y; Computer Programming
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 Programming
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 Objectives in ProgrammingA program should solve a problem: Computer Programming
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 Summary of "Levels" of Computer Languages Computer Programming
App Disk CPU RAM Cache OS Bus Putting It All Together Programs and applications that are not running are stored on disk. Computer Programming
App Disk CPU RAM Cache OS Bus Putting It All Together • When you launch a program • OS controls the CPU • Loads the program from disk to RAM. App Computer Programming
App App Disk CPU RAM Cache OS Bus Putting It All Together • The OS then relinquishes the CPU to the program, • Application program begins to run. App Computer Programming
App Disk CPU RAM Cache OS Bus The Fetch-Execute Cycle • CPU repeatedly fetches the next instruction (from memory / cache), • Executes it • Stores any results back to memory. App App • That’s all a computer does: fetch-execute-store, millions of times each second! Computer Programming
First program: Hello World! #include <iostream> using namespace std; int main() { cout << “Hello World!”<<endl; return 0; } Computer Programming
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 Programming
First Interesting Problem Write a program that computes the volume of a sphere with a radius specified by the user. To do in class! Computer Programming