Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPS120 Introduction to Computer Science Exam Review Lecture 18.

Similar presentations


Presentation on theme: "CPS120 Introduction to Computer Science Exam Review Lecture 18."— Presentation transcript:

1 CPS120 Introduction to Computer Science Exam Review Lecture 18

2 General Form if (test condition 1) { // true-block1 statements if (test condition 2) { true-block2 statements; } else { false-block2 statements; } else { false-block1 statements; }

3 Switch Structure The switch structure is a multiple-selection structure that allows even more complicated decision statements than a two-way if/else structure allows. It chooses one of the "cases" depending on the result of the control expression. Only variables with the INT or CHAR data types may be used in the control expressions (i.e. parentheses) of switch statements. –Single quotes must be used around CHAR variables –Single quotes are NOT used around the integer values

4 Sample Switch Structure switch (character_entered) { case ‘A’ : cout << “The character entered was A.\n”; break; case ‘B’: cout << “The character entered was B.\n”; default: cout << “Illegal entry\n”; }

5 Switch Structure The break statement must be used within each case if you do not want following cases to evaluate once one case is found. When the break statement is executed within a switch, C++ will execute the next statement outside of the switch statement. The default case, if present, will result if none of the prior cases apply

6 An Example of A Function #include void printMyMessage(int numOfTimes); // PROTOTYPE and NAME int main( ) { int userInput = 0; cout << "Enter a number between 1 and 10 (0 to Exit): " ; cin >> userInput; if (userInput != 0) { printMyMessage (userInput); // CALL STATEMENT WITH ACTUAL PARAMETER } else cout << "Thanks, Bye!"; return 0; } // end of main void printMyMessage(int numOfTimes)// FUNCTION HEADER WITH RETURN TYPE AND ACTUAL PARAMETER { int i=0; // LOCAL VARIABLE WITHIN THE FUNCTION for (i=0; i<= numOfTimes; i++)// BODY {cout << "Let's Go State!!" << endl;}// OF THE } //end of printMyMessage// FUNCTION

7 Scope of Variables The scope of a variable is the area in which it can be legally referenced –Variables are either global or local in nature –Global variables are ones that are declared outside and above the main function –They can be used in any function throughout the program. It is not wise to use global variables any more than you have to. –Local variables are ones that are declared inside of a function, including main. They cannot be used or referred to in other functions

8 Passing Data Data is passed to functions as arguments When a function is "called" by the main function one or more arguments are passed to the function On the receiving end, the function accepts these arguments The variable names of the arguments from the "calling" function do not have to be the same as the names in the "called" function. –The data types of the arguments and the parameters should match exactly

9 Required Compiler Directives Any program that uses file pointers must include the fstream.h header file with the compiler directive, #include at the top of the program

10 Preparing to Use Files Opening a sequential-access file ofstream outfile; –ofstream is a C++ keyword indicating the type of pointer that you created –outfile is simply the programmer's chosen name for the file pointer (and can be any valid name) Open the file "mydata.txt" that is stored on your PC's hard drive outfile.open("mydata.txt", ios::out); or the shorter version outfile.open("mydata.txt"); –output (out) is the default type of access for ofstream objects

11 Reading From a File Declare a file pointer as an ifstream object with: ifstream infile; –ifstream is a keyword and infile is the name for the file pointer. Open the actual file for reading with: infile.open("mydata.txt", ios::in); or the shorter version infile.open("mydata.txt");

12 Preparing to Write Output It is wise to check to make sure that there wasn't an error actually opening the data file One can use an if statement like the following to protect the program from crashing. if (outfile) // same as if (outfile != 0) { outfile << "John Doe" << endl; } else { cout << "An error occurred while opening the file.\n"; }

13 Writing Output To write data to a sequential-access data file you would use a statement like: outfile << "John Doe" << endl; to print that name to the next line in the data file pointed to by the file pointer, outfile.

14 Appending Data Adding data to the end of a sequential-access data file is called appending Open the file using the ios::app stream operation mode as in: outfile.open("myfile.txt", ios::app); where the app is short for append. If you accidentally open the file with the ios::out mode, you will end up overwriting data in the file because C++ will write the first piece of outputted data at the beginning of the sequential-access data file

15 Detecting the End of a File. Use the eof function to determine whether the end of a sequential-access file has been reached. This function returns a 1 (true) if an attempt has been made to read past the end of the file. do { infile >> x; if ( !infile.eof( ) ) { cout << x << endl; } } while ( !infile.eof( ) );

16 Pointer Use in C++. A pointer is a variable or constant that holds a memory address –a) Hexadecimal numbers are used for representing memory locations 216793 216794216801iptr … 2168013i 216802

17 Intializing Pointers Declare pointers before use, as with other variables. Each variable being declared as a pointer must be preceded by an asterisk (*). Initialize pointers before use to a 0, NULL or an address to prevent unexpected results

18 Pointer Operators & is the address operator which returns the address of its operand * is the indirection operator or dereferencing operator and returns the value to which the operand (pointer) points. sizeof - used to determine the size of an array during program compiliation

19 Structures Structures group variables together in order to make one's programming task more efficient. –Any combination of variables can be combined into one structure. –This is a useful and efficient way to store data. struct Student { string socSecNum; string lastName; string firstName; int pointsEarned; double gpa; };

20 CPS120: Introduction to Computer Science Lecture 15 Arrays

21 Arrays: A Definition A list of variables accessed using a single identifier –May be of any data type Can be single or multi-dimensioned Vector classifications are object-oriented representations of arrays

22 Declaring an Array To declare an array before it is used in the body of your program, you must use a statement like: int scores[10]; This would declare an array of integers, named "scores". In this case, scores can store up to 10 different integer values. –The positions of the array are identified by their index positions which run from 0 to 9 (not 1 to 10.) Each one of the 10 variables in scores is called an element

23 Initializing an Array If you wish to initialize each element of an array to a specific value, you can use the statement, int scores[] = {65, 76, 45, 83, 99}; You don't even have to specify a size of the array in this case since the initialization statement would cause the compiler to declare an array of size 5 since there are five values in the set of curly braces

24 Loops and Arrays Use loops in conjunction with arrays to allow the user to input data into an array Use loops with arrays to display data stored in an array

25 Declaring a Multi-dimensional Array To declare an array of integers called studentGrades to be a 2-dimensional array with 3 rows and 4 columns, you would use the statement: int studentGrades[3] [4]; where the first integer value is used to specify the number of rows in the array and the second value specifies the number of columns Think of remote control

26 Initializing a Multi-dimensional Array You can initialize the 2-dimensional array when you declare it by using commas and braces appropriately int studentGrades[3] [4] = { { 1, 2, 3, 4}, { 5, 6, 7, 8}, { 9, 10, 11, 12} }; Be careful though when assigning values to a specific position within a 2-dimensional array. –Just like one-dimensional arrays, the subscript positions with regard to the rows and the columns begin at 0, not –In the example above the value of the variable studentGrades[0] [2] is 3


Download ppt "CPS120 Introduction to Computer Science Exam Review Lecture 18."

Similar presentations


Ads by Google