Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB : 1024 * 1024 * 1024 bytes 2 10 * 2 10 * 2 10 (2 30 ) bytes 1
Data Types char int (short, long) float (double) bool string (C++ string) array (2-D Array) C String (array of char) Enum (typedef) Classes 2
Data Types Storage Values Operations 3
Input cin >> mostVal; cin.get(charVal); inFile >> mostVal; inFile.get(charVal); Output Arithmetic operations float division integer division (remainder) Assignment Comparison Function parameter Function return value 4
Classes Construct or class Student: string id, name; float gpa; class Section: Student students[26]; int numStudents; class UWPClass Section sections[MAX_SIZE]; int numSections; class Department UWPClass allClasses[100]; int numClasses; 5
Classes class Student { private: string id, name; float gpa; public: string getName() { return name; }... } 6
Classes class Student { public: string id, name; float gpa; public: // Not needed string getName() { return name; }... } 7
Char and Int Sometimes char is treated as int (its ASCII code value). char ch = ‘A’; ch ++; cout << ch; cin >> ch; if (ch >= ‘0’ and ch <= ‘9’) cout << “The value is ” << (ch – ‘0’); 8
Important Topics If If – Elseif - Else While and For Loops Tracing function call Pass by value Pass by reference Activation Record Linear search Selection Sort File I/O 9
Review The following declaration is illegal. TRUEFALSE int size = 100; StudentType allStudents[size]; The following declaration is valid. TRUEFALSE int size = 100; float allSscores[size]; 10
Review const int MAX_SIZE = 5000; float scores[MAX_SIZE]; The following operation is invalid. (Assuming values have been entered.) TRUEFALSE for (int i = 0; i <= MAX_SIZE; i ++) cout << setw(9) << scores[i]; // Uninitialized values // Run time Error The above operation is valid. (Assuming values have been entered.) TRUEFALSE 11
Review Find Max Linear (Sequential) Search for (int i = 0; i < size; i ++) // Comparing Not sorted Sorted The last (first) element
Review Find a Number Linear (Sequential) Search for (int i = 0; i < size; i ++) // Comparing Not sorted Sorted Binary Search
Review Functions void SomeFunction(int x, float& y); // Pass by Value or by Reference int xVal = 100, noVal; float yVal = 135.0; SomeFunction(120, noVal); SomeFunction(noVal, yVal); SomeFunction(xVal, yVal + 10); SomeFunction(xVal, xVal); SomeFunction(“120”, yVal); 14
Review Syntax and Style if (index == -1) cout << “Not found”; if (index == -1) cout << “Not found”; // Syntax is OK 15
Review Programming Design Coding Compiling Running (Debugging) Errors Cannot compile Run time error (Uninitialized values) Infinite loop Incorrect results Correct results Good Programs 16
Example int count = 15; while (count > 0); { //do something count --; } How many times will the body be executed? A. 0 B. 1 C. 15 D. Infinity loop E. None of above 17
Example int count; cout >> “Enter count: ”; cin << count; while (count > 0) { //do something count --; } The above code segment A. Cannot compile. B. Will generate a Run time error. C. Will not terminate (Infinity Loop). D. Will generate incorrect result. E. NONE of Above. 18
Example int count = 15; float value, total = 0; while (count > 0) { cin >> value; value += total; count --; } cout << “Total: ” << total; The above code segment A. Cannot compile. B. Will generate a Run time error. C. Will not terminate (Infinity Loop). D. Will generate incorrect result. E. NONE of Above. 19
Final Exam 7:00 – 8:52 PM, Thursday, May 19 Doudna Points 50 points: T/F, Multiple Choices 50 Points: Programming Statements Entire Program Tracing Functions #2 pencil Do it as given in class! Manage your time! 20
Final Exam 21