300 likes | 401 Views
EECS230 Lectures Series. EECS230 Course Introduction and a First Program. Ying Wu Electrical Engineering and Computer Science Northwestern University yingwu@ece.northwestern.edu. Course Personnel. Instructor: Ying Wu TA: Ming Yang (m-yang4@northwestern.edu)
E N D
EECS230 Lectures Series EECS230 Course Introduction and a First Program Ying Wu Electrical Engineering and Computer Science Northwestern University yingwu@ece.northwestern.edu
Course Personnel • Instructor: Ying Wu • TA: Ming Yang (m-yang4@northwestern.edu) • TA: David Choffnes (drchoffnes@northwestern.edu)
Course Information • Lectures • M,T,W,F 1-1:50pm, Tech. LR4 • Tuesday meetings: TA sessions • Text • C++ How to Program, 4rd (or later) Edition, by H.M. Deitel and P.J. Deitel, Prentice Hall. • Course Web • http://www.courses.northwestern.edu/ using your ID • Grading • Machine Problems (MPs): 30% • Midterm exam: 30% • Final exam: 40%
Why should we study programming? • Think about your new role in 5 years • A graduate student • An engineer • A manager • … • Programming practice can • Implement your ideas • Motivate you to re-think your ideas • Make you solid, sharp and deep-thinking • Make you more cooperative
Eager to Learn C/C++? • We shall cover • C/C++ control structures • Function • Pointers and Array • Stream I/O and string operations • Dynamic memory allocation • Classes • Classes with pointer data members • Linked list • Sort and search See the course syllabus for a detailed schedule
We’ll focus on • Core C/C++ concepts • Such as: • Pointers and references • Call-by-value v.s. call-by-reference • Class • Copy constructor • … • Program design • Debugging skills
The best way in learning C/C++ • Deep understanding the concepts, especially • Pointers/references • Functions • Classes • Coding experiences • Do spend time in coding • Reading examples • Debugging • Tools • Skills
The GOAL for Week-1/2 • Understand all C/C++ data types • Understand basic computer architecture and addressing mechanism • Use cin/cout to input/output • Grasp six C/C++ control structures • Understand array • Design by using flowcharts • Get a sense of the compiling process • DEBUG_1: debugging when you are coding • Do the MP#1 (basic control structures)
What to learn today? • How does the computer do with my code? • What are “int”, “float”, “double”, “bool”? • What is a variable? • What will happen if you declare a variable? • Arithmetic expressions and calculation? • Logical expressions and decision making? • How to get inputs and output results?
What does the Computer do with my program? Preprocessor Linker Editor Disk Disk Disk Disk Disk CPU . . . . . . . . . . . . Program is created in the editor and stored on disk. • Phases of C++ Programs: • Edit • Preprocess • Compile • Link • Load • Execute Preprocessor program processes the code. Compiler creates object code and stores it on disk. Compiler Linker links the object code with the libraries, creates a.out and stores it on disk Primary Memory Loader Loader puts program in memory. Primary Memory CPU takes each instruction and executes it, possibly storing new data values as the program executes. Let’s see an example then.
Basic Data Types • int • integer (4 bytes = 32 bits) • float • real decimal number (4 bytes = 32 bits) • double • double precision real decimal number (8 bytes = 64 bits) • char • character (1 byte = 8 bits) • bool • true/false (1 byte), or 0/1
ASCII char set • American Standard Code for Information Interchange • A character has a numerical representation (a decimal integer) in computer, e.g., ‘a’ – 97, ‘A’ – 65, ‘1’ – 49, ‘+’ – 43, etc • Check the appendix B of D&D for the table Question: how many characters can we have?
Conversion among Data Types • Priority order • bool and char < int < float < double • The compiler can automatically convert low priority data to higher ones • But it can not do the inverse automatically • Why? • You will lose precisions! • So, you have to do it yourself forcefully • e.g., (int) 3.1415 3
What is a variable? • Three Properties • Type • determines the number of bytes of memory that are allocated for this variable by O/S • Name • is used to address (or retrieve) this variable in system memory stacks • Value • is the content and is used for calculation
Memory Concepts • Variable names • Correspond to locations in the computer's memory • Every variable has a name, a type, and a value • Whenever a new value is placed into a variable, it replaces the previous value • Reading variables from memory does not change them • A visual representation integer1 45
Addressing? • Physically, each byte of physical memory is a set of registers, each of which corresponds to 0/1. • Each byte of memory has a unique “address”. Generally, we use a hexadecimal number to describe its address, e.g., 0x0012FD8C (32 bits) • Question: how many bytes can a 32-bit machine manage? • In hardware, addressing is realized by sending such a hexadecimal number to the BUS to get the value of the memory blocks. • In C/C++ language, we do not need deal with such hexadecimal numbers, instead, we deal with the names or pointers of variables.
What if you declare a variable? • O/S will allocate (reserve) a memory block to hold the value of the variable. • The value can be changed by “assignment” • The value can be retrieved by “addressing” • No other variables can use such a memory block until you let the O/S to recycle it. (how?)
Examples a = 5; a int a; a 5 float b; b a a 5 3 a = 3; a 5 b = a+1; b 6
Arithmetic Expressions • Arithmetic calculations • Use * for multiplication and / for division • Integer division truncates remainder • 7 / 5 evaluates to 1 (isn’t it interesting?) • Modulus operator returns the remainder • 7 % 5 evaluates to 2 • Operator precedence • Some arithmetic operators act before others (i.e., multiplication before addition) • Be sure to use parenthesis when needed • Example: Find the average of three variables a, b and c • Do not use: a + b + c / 3 • Use: (a + b + c ) / 3
Arithmetic Operators Operator(s) Operation(s) Order of evaluation (precedence) () Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level” (i.e., not nes ted), they are evaluated left to right. , , Multiplication Division Evaluated second. If there are several, they re or * / % Modulus evaluated left to right. Addition Evaluated last. If there are several, they are or + - Subtraction evaluated left to ri ght. • Arithmetic operators: • Rules of operator precedence:
What does a logical exp. evaluate? • It evaluates a boolean value, i.e., true/false • Certainly, a boolean variable can be automatically converted to other data types • An interesting example bool c; char a = ‘Z’; c = (a = = 90); Question: what will be the value of c?
“Hello, World!” • std::cout • Standard output stream object • “Connected” to the screen • std:: specifies the "namespace" which cout belongs to • std:: can be removed through the use of using statements • << • Stream insertion operator • Value to the right of the operator (right operand) inserted into output stream (which is connected to the screen) • std::cout << “Hello, World!\n”; • \ • Escape character • Indicates that a “special” character is to be output
1 // Fig. 1.4: fig01_04.cpp 2 // Printing a line with multiple statements 3 #include <iostream> 4 5 int main() 6 { 7 std::cout << "Welcome "; 8 std::cout << "to C++!\n"; Unless new line '\n' is specified, the text continues on the same line. 9 10 return 0; // indicate that program ended successfully 11 } 1. Load <iostream> 2. main 2.1 Print "Welcome" 2.2 Print "to C++!" 2.3 newline 2.4 exit (return 0) Program Output Welcome to C++!
Wanna get an input? • >> (stream extraction operator) • When used with std::cin, waits for the user to input a value and stores the value in the variable to the right of the operator • The user types a value, then presses the Enter (Return) key to send the data to the computer • Example: int myVariable; std::cin >> myVariable; • Waits for user input, then stores input in myVariable
1 // Fig. 1.6: fig01_06.cpp 2 // Addition program 3 #include <iostream> 4 Notice how std::cin is used to get user input. 5 int main() 6 { 7 int integer1, integer2, sum; // declaration std::endl flushes the buffer and prints a newline. 8 Variables can be output using std::cout << variableName. 9 std::cout << "Enter first integer\n"; // prompt 10 std::cin >> integer1; // read an integer 11 std::cout << "Enter second integer\n"; // prompt 12 std::cin >> integer2; // read an integer 13 sum = integer1 + integer2; // assignment of sum 14 std::cout << "Sum is " << sum << std::endl; // print sum 15 16 return 0; // indicate that program ended successfully 17 } Load <iostream> 2. main 2.1 Initialize variables integer1, integer2, and sum 2.2 Print "Enter first integer" 2.2.1 Get input 2.3 Print "Enter second integer" 2.3.1 Get input 2.4 Add variables and put result into sum 2.5 Print "Sum is" 2.5.1 Output sum 2.6 exit (return 0) Program Output Enter first integer 45 Enter second integer 72 Sum is 117
Save your typing … • using statements • Eliminate the need to use the std:: prefix • Allow us to write cout instead of std::cout • To use the following functions without the std:: prefix, write the following at the top of the program using std::cout; using std::cin; using std::endl;
1 // Fig. 1.14: fig01_14.cpp 2 // Using if statements, relational 3 // operators, and equality operators Notice the using statements. 4 #include <iostream> 5 6 using std::cout; // program uses cout 7 using std::cin; // program uses cin The if statements test the truth of the condition. If it is true, body of if statement is executed. If not, body is skipped. To include multiple statements in a body, delineate them with braces {}. 8 using std::endl; // program uses endl 9 10 int main() 11 { 12 int num1, num2; 13 14 cout << "Enter two integers, and I will tell you\n" 15 << "the relationships they satisfy: "; 16 cin >> num1 >> num2; // read two integers 17 18 if ( num1 == num2 ) 19 cout << num1 << " is equal to " << num2 << endl; 20 21 if ( num1 != num2 ) 22 cout << num1 << " is not equal to " << num2 << endl; 23 24 if ( num1 < num2 ) 25 cout << num1 << " is less than " << num2 << endl; 26 27 if ( num1 > num2 ) 28 cout << num1 << " is greater than " << num2 << endl; 29 30 if ( num1 <= num2 ) 31 cout << num1 << " is less than or equal to " 32 << num2 << endl; 33 1. Load <iostream> 2. main 2.1 Initialize num1 and num2 2.1.1 Input data 2.2 if statements Enter two integers, and I will tell you the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7
34 if ( num1 >= num2 ) 35 cout << num1 << " is greater than or equal to " 36 << num2 << endl; 37 38 return 0; // indicate that program ended successfully 39 } Enter two integers, and I will tell you the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7 2.3 exit (return 0) Program Output Enter two integers, and I will tell you the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than 12 22 is greater than or equal to 12 Enter two integers, and I will tell you the relationships they satisfy: 7 7 7 is equal to 7 7 is less than or equal to 7 7 is greater than or equal to 7
Summary • What we have learnt: • Basic C++ environment • Basic data types • Variables and addressing • Arithmetic/logical expressions • Basic I/O • Questions remained: • How can we get a “line of characters” as the input? • Compare to a variable, how will such a line be stored in memory? • What can we do about that entire line of input?