CS 241 Computer Programming Introduction Teacher Assistant Hadeel Al-Ateeq 1
2 Course URL: http://241cs.wordpress.com/ Hadeel Al-Ateeq
3 Textbook HOW TO PROGRAM BY C++ DEITEL AND DEITEL, Seventh edition.
4 Grades Evaluation Type Midterms (Midterm 1) (Week # 7) (Midterm 2) (Week # 12) Grade 10% 15% Lab works (HW-Evaluation Quiz) 15% Final Lab 20% Final 40% Total 100% MIGHT Change Hadeel Al-Ateeq
5 Topics Topics to be covered Introduction Data types, variables, values, assignment Sequence, iteration, branching statements Functions Arrays Pointers and Strings Recursion Hadeel Al-Ateeq
6 Outline Introduction. Computer languages: Machine Languages. Assembly Languages. High-Level Languages. C++ System. C++ Standard Library. Basics of a Typical C++ Environment. Structure of C++ program. Input/Output. Program #1: Printing a Line of Text. Program #2: Adding Two Integers. Memory Concepts.
7 Introduction (1-3) Computer Device capable of performing computations and making logical decisions Software Computer programs. Instructions to command computer to perform actions and make decisions. Programs that run on computer
8 Introduction (2-3) Hardware Computer hardware is any physical device (e.g. the computer monitor), something that you are able to touch. Various devices comprising computer Keyboard, screen, mouse, disks, memory, CD-ROM, processing units,
9 Computer Languages Used to write instructions to computer. Some are directly understandable by computers. Others require intermediate translation steps. Types of Computer Languages Machine Languages Assembly Languages High-Level Languages
10 Machine Language (1-2) Only language that computer directly understands. Natural language of computer. Generally consist of strings of numbers (Ultimately 0s and 1s). Instruct computers to perform elementary operations one at a time. Cumbersome for humans. Too slow, tedious and error prone for most programmers.
11 Machine Language (2-2) Example: Adding overtime pay to base pay and storing the result in gross pay +11001101 +00000110 +11110111
12 Assembly Languages (1-2) English-like abbreviations representing elementary computer operations Clearer to humans Incomprehensible (ambiguous) to computers Assemblers: Translator programs. Convert to machine language.
13 Assembly Languages (2-2) Example: Adding overtime pay to base pay and storing the result in gross pay LOAD ADD STORE BASEPAY OVERPAY GROSSPAY
14 High-level Languages (1-2) Similar to everyday English, use common mathematical notations. Single statements accomplish substantial tasks. Assembly language requires many instructions to accomplish simple tasks Compilers: Translator programs. Convert to machine language. Take a considerable amount of time.
15 High-Level Languages (2-2) Example: Adding overtime pay to base pay and storing the result in gross pay grosspay = basepay + overpay Most Used high-level languages: C, C++, VB.NET, etc.
C++ System Program development environment Language C++ Standard Library Hadeel Al-Ateeq
17 C++ Standard Library C++ standard library Rich collections of existing classes and functions Common math calculations e.g. sqrt, sin, cos. Input/output Date/Time Building block approach to creating programs Software reuse.
Basics of a Typical C++ Environment (1-2) Phases of C++ Program: 18 Edit: Programmer types a C++ program (source code.cpp ). Preprocess: obeys commands called preprocessor directives. Indicate that certain manipulations are to be performed on the program before compilation. e.g. include other text files to be compiled. Compile: translates the C++ program into machine language code.
Basics of a Typical C++ Environment (2-2) Phases of C++ Program: 19 Link: links the code with the code for the missing functions to produce an executable program with no missing pieces. Load: load the executable code on memory. Execute: CPU executes the program one instruction at time.
Editor Disk 20 Program is created in the editor and stored on disk. Preprocessor Compiler Linker Loader Disk Disk Disk Disk Primary Memory... Preprocessor program processes the code. Compiler creates object code and stores it on disk. Linker links the object code with the libraries, creates a.out and stores it on disk Loader puts program in memory. CPU Primary Memory... CPU takes each instruction and executes it, possibly storing new data values as the program executes.
21 Structure of a C++ program (1-2) #include<iostream> using namespace std; int main() { return 0; } ; also is important at end of each statement
22 Structure of a C++ program (2-2) #include<iostream> using namespace std; void main() { } ; also is important at end of each statement
Input/ Output cin cout cerr Hadeel Al-Ateeq
24 cin Command Description Standard input stream. Normally keyboard. Used to get input form the user. Syntax cin >> ;
25 cout Command Description Standard output stream Normally computer screen Used to print a specified message on the output screen. Syntax cout << ;
26 Comments Description Document programs. Improve program readability. Ignored by compiler. Used to describe the code segments briefly. Single-line comment begins with //. Multi-line comments begins with /* and ends with */ Syntax // Single line comment /* Multil-ine Comment */ Hadeel Al-Ateeq
27 Preprocessor directives Description Processed by preprocessor before compiling Begin with # Syntax #
Program #1 28 Printing Line of Text (1-6) Question Write a program that prints Welcome to C++. Hadeel Al-Ateeq
Program #1 29 Printing Line of Text (2-6) Preprocessor directive to include input/output stream header file Left brace { begins function body. <iostream>. A keyword indicates that the main returns integer value Function main appears exactly once in every C++ program.. Name cout belongs to namespace std. Corresponding right brace } ends function body. Statements end with a Keyword return semicolon is one of ;. several means to exit function; value 0 indicates program terminated successfully.
Program #1 30 Printing Line of Text (3-6)
Program #1 31 Printing Line of Text (4-6) Standard output stream object std::cout Connected to screen << Stream insertion operator. Value to right (right operand) inserted into output stream. Namespace std:: specifies using name that belongs to namespace std. std:: removed through use of using statements.
Program #1 32 Printing Line of Text (5-6) Escape characters \ Indicates special character output.
Program #1 33 Printing Line of Text (6-6) Escape characters Escape sequence \n \t \r Description Newline: Position the screen cursor to the beginning of the next line Horizontal tab: Move the screen cursor to the next tab stop. Carriage Return: Position the screen cursor to the beginning of the current line; do not advance to the next line. \a Alert: Sound the system bell. \\ Backslash: Used to print a backslash character. \ Double Quote: Used to print a double quote character.
34 Modifying Printing Line of Text (1-4) Multiple stream insertion statements produce one line of output.
Modifying Printing Line of Text (2-4) 35
36 Modifying Printing Line of Text (3-4) Using newline characters to print on multiple lines.
Modifying Printing Line of Text (4-4) 37