03/07 8am CISC181 lecture notes (0.5) Daily calendar... is the the only way to get to the lecture notes and code? NO!!!! Links are just there for convenience. (0) NOTE: on things you turn in, put your SECTION NUMBER, full name, and DATE DUE, not the date you complete the work. We have to manage 4000 pieces of paper, several hundred at at time! (We need ways to sort out the papers when they get mixed up.) (0.5) Note about "cin" with multiple variables cin >> x, y, z; // WRONG!!! cin >> x >> y >> z; // CORRECT !!! Notes on this: The comma has (nearly?) the lowest precedence among the the operators in C++. If you've got a comma on the line, almost every other operator will get done first, then last, the comma gets done. So, cin >> x, y, z; is the same as (cin >> x), y, z; The y and z have no effect whatsoever. You can put x; by itself on a line of code, and it will compile! You can put 3 + 4 + a; by itself, and it will compile. Neither does any useful work. Why does the comma operator exist at all? for (i=0; i<10; i++) cout << i << endl; // that's a for loop that prints out 0 through 9 for (i=0, p=1; i<10; i++, p*=2) cout << "i= " << i << " p= " << p << endl; // what does this do? // I can do that, but should I??? Isn't this clearer: p=1; for (i=0; i<10; i++) { cout << "i= " << i << " p= " << p << endl; // what does this do? p*=2; } (1) Go over some stuff needed for lab05: Reading from a file using an ifstream object Reading until eof See "readUsernamesFromFile.cpp" and "usernames.txt" (2) More on reading from files Show reading a file of integers into an array. This is a BASIC SKILL that should be in your "toolbox" of things you know how to do in C++ (or in any programming language that you learn.) Once you have this, then you can practice other basic skills such as writing functions to (a) find the min, max, average, count of an array. (b) sort the elements in an array smallest to largest, or vice-versa (c) count the number of occurences of element x in an array (d) count the number of elements smaller than x (e) indicate whether x is in the array or not (3) Why does main return int? Why do we "return 0"? Programs don't always return 0. The "grep" program has several different return values (show the output of "man grep" ) Check out the example shell scripts in the code directory. See the subdirectory called "shellScripts". Show the various return values from the "curl" command (found in ~pconrad/local/bin/curl) In the shell, we use $? to examine the value returned by the last program that executed. (Note: If we use "echo" to examine $?, we then lose the value, since the new value of $? is the value returned by the "echo" command!) (4) Review of Recursion from previous lecture Again Factorial: 0! = 1 1! = 1 2! = 2 * 1 3! = 3 * 2 * 1 5! = 5 * 4 * 3 * 2 * 1 4! = 4 * 3 * 2 * 1 5! = 5 * 4 ! n! = n * (n-1)! when n >= 1 1 when n is 0 Recursion is defining a function in terms of itself. A recursive definition always needs: (1) a base case (2) a recursive call that moves us towards a base case. Usually only one base case, but can be more than one. (5) Now consider some other recursive functions: (5a) Adding 1 + 2 + 3 + 4 + 5 .... (show different ways of doing it with recursion, iteration, and closed form) (5b) Adding 1 + 4 + 9 + 16 + 25 ... (show different ways of doing it with recursion, iteration, etc. ) (5c) Computing x to the yth power, (where x and y are integers) (show different ways of doing it with recursion, iteration, etc. ) (5d) Printing an array (show array initializers first...) show iterative printing of an array show recursive printing of an array