CPS120 Introduction to Computer Science Exam Review Lecture 18.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Programming Languages and Paradigms The C Programming Language.
Chapter 7: User-Defined Functions II
CPS120: Introduction to Computer Science Lecture 15 B Data Files.
True or false A variable of type char can hold the value 301. ( F )
Chapter 10.
Chapter 5: Loops and Files.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 8 Arrays and Strings
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
CPS120 Introduction to Computer Science Exam Review Lecture 9.
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
Chapter 8 Data File Basics.
Chapter 8 Arrays and Strings
CPS120: Introduction to Computer Science Decision Making in Programs.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
1 CS161 Introduction to Computer Science Topic #13.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Lecture 3 Monday, 14 July Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CPS120: Introduction to Computer Science Lecture 15A Structures.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Loops and Files. 5.1 The Increment and Decrement Operators.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Data Structures.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
CPS120: Introduction to Computer Science Decision Making in Programs.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
CPS120 Introduction to Computer Science Exam Review.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
7 Arrays.
Introduction to C++ Programming
6 Chapter Functions.
CPS120: Introduction to Computer Science
7 Arrays.
CPS120: Introduction to Computer Science
Arrays Arrays A few types Structures of related data items
Reading from and Writing to Files
Reading from and Writing to Files
Presentation transcript:

CPS120 Introduction to Computer Science Exam Review Lecture 18

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

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

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”; }

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

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

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

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

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

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

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");

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"; }

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.

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

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( ) );

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 iptr … i

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

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

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; };

CPS120: Introduction to Computer Science Lecture 15 Arrays

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

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

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

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

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

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