COMP 2710 Software Construction Midterm Exam 1 - Review

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Chapter 1 C++ Basics. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented.
Chapter 3 Function Basics Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Learning Objectives  Parameters  Call-by-value  Call-by-reference  Mixed parameter-lists  Overloading and Default Arguments  Examples, Rules  Testing.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
CSC 200 Lecture 2 Matt Kayala 1/25/06. Lecture Objectives More detailed intro to C++ Variables, Expressions, and Assignment Statements Console Input/Output.
CSC 200 Lecture 04 2/8/06 Matt Kayala. Learning Objectives  Predefined Functions  Those that return a value and those that don’t  Programmer-defined.
Basic Elements of C++ Chapter 2.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: User-Defined Functions
CPS120: Introduction to Computer Science
Chapter 1 C++ Basics Copyright © 2012 Pearson Addison-Wesley. All rights reserved.
Slide 1 Chapter 3 Function Basics. Slide 2 Learning Objectives  Predefined Functions  Those that return a value and those that don’t  Programmer-defined.
Chapter 2 Flow of Control. Learning Objectives Boolean Expressions – Building, Evaluating & Precedence Rules Branching Mechanisms – if-else – switch –
Slide 1. Slide 2 Chapter 1 C++ Basics Slide 3 Learning Objectives  Introduction to C++  Origins, Object-Oriented Programming, Terms  Variables, Expressions,
Slide 1 Chapter 4 Parameters and Overloading. Slide 2 Learning Objectives  Parameters  Call-by-value  Call-by-reference  Mixed parameter-lists  Overloading.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Chapter 1 C++ Basics Copyright © 2016 Pearson, Inc. All rights reserved.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
COMP 2710 Software Construction C++ Basics 2 and Exercises Dr. Xiao Qin Auburn University These slides.
COMP 2710 Software Construction Flow of Control – switch and loops Programming Exercises Dr. Xiao Qin Auburn University
Bill Tucker Austin Community College COSC 1315
FAQ: Exit Vi Using :q Can NOT exit with “:q” if you have not use “:w” to write changes.
Chapter 1 C++ Basics Copyright © 2016 Pearson, Inc. All rights reserved.
Chapter 1.2 Introduction to C++ Programming
Review 1.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 7: User-Defined Functions II
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
Chapter 1.2 Introduction to C++ Programming
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Programmer-Defined Functions Dr.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Function Basics TBC=15 Dr. Xiao Qin.
Basic Elements of C++.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
COMP 2710 Software Construction File I/O
Basics (Variables, Assignments, I/O)
Copyright © 2003 Pearson Education, Inc.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 2-3 C++ Basic Constructs
Java Programming: From Problem Analysis to Program Design, 4e
User-Defined Functions
Basic Elements of C++ Chapter 2.
Chapter 9 Scope, Lifetime, and More on Functions
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Basics (Variables, Assignments, I/O)
Chapter 7 Additional Control Structures
Parameters and Overloading
Chapter 2: Introduction to C++.
C++ Programming Basics
Programming Languages and Paradigms
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

COMP 2710 Software Construction Midterm Exam 1 - Review Dr. Xiao Qin Auburn University http://www.eng.auburn.edu/~xqin xqin@auburn.edu

Chapters Ch 1, 2, 3 Ch 4, 5 Ch 12.1 and 12.2 Please read Ch 1.1 on page 3 1-2 2

C++ Variables C++ Identifiers Variables Keywords/reserved words vs. Identifiers Must start with a letter or the underscore symbol The rest must be letters, digits, or the underscore symbol Case-sensitivity and validity of identifiers Meaningful names! (see also Page 13) Variables A memory location to store data for a program Must declare all data before use in program 1-3 3

Keywords Reserved Words Predefined meaning in C++ Example: short, long, char, true, false, if, else, for, while, class, private, public, typedef, static, sizeof, new, delete, See Appendix 1 for a complete list of keywords. Yes. C++ is case-sensitive 1-4 4

Declare a Variable A variable must be declared before it is used! Tell the compiler what kind of data (i.e., data type) will be stored in the variable See what will happen when you compile a C++ program where a variable is not declared. Give an example. 1-5 5

Data Types: Display 1.2 Simple Types (1 of 2) Page 9: Short -32767 to 32767 = 65534 = 2^16 -> 16 bits for 2 Bytes -> 8 Bits per Byte double: commonly used floating-pint number Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-6 6

Data Types: Display 1.2 Simple Types (2 of 2) Char may be treated as an integer type to save space. Unsigned Version: unsigned int, unsigned long Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-7 7

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Assigning Data Initializing data in declaration statement Results "undefined" if you don’t! int myValue = 0; int count = 0, distance = 15, factor = 1; Assigning data during execution Lvalues (left-side) & Rvalues (right-side) Lvalues must be variables Rvalues can be any expression Example: distance = rate * time; Lvalue: "distance" Rvalue: "rate * time" Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-8 8

Display 1.3 Some Escape Sequences (1 of 2) Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-9 9

Constants Naming your constants Use named constants instead Literal constants are "OK", but provide little meaning e.g., seeing 24 in a pgm, tells nothing about what it represents Use named constants instead Meaningful name to represent data const int NUMBER_OF_STUDENTS = 24; const int BRANCH_COUNT = 10, WINDOW_COUNT = 10; //not recommended, a separate line is clearer. Called a "declared constant" or "named constant" Now use it’s name wherever needed in program Added benefit: changes to value result in one fix 1-10 10

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Type Casting Two types Implicit—also called "Automatic" Done FOR you, automatically 17 / 5.5 This expression causes an "implicit type cast" to take place, casting the 17  17.0 Explicit type conversion Programmer specifies conversion with cast operator (double)17 / 5.5 Same expression as above, using explicit cast (double)myInt / myDouble More typical use; cast operator on variable Precision of Calculations VERY important consideration! Expressions in C++ might not evaluate as you’d "expect"! "Highest-order operand" determines type of arithmetic "precision" performed Common pitfall! Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-11 11

Shorthand Operators: Two Options Post-Increment intVar++ Uses current value of variable, THEN increments it Pre-Increment ++intVar Increments variable first, THEN uses new value "Use" is defined as whatever "context" variable is currently in No difference if "alone" in statement: intVar++; and ++intVar;  identical result Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-12 12

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Console Input/Output I/O objects cin, cout, cerr Defined in the C++ library called <iostream> Must have these lines (called pre- processor directives) near start of file: #include <iostream> using namespace std; Tells C++ to use appropriate library so we can use the I/O objects cin, cout, cerr Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-13 13

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Formatting Numbers "Magic Formula" to force decimal sizes: cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); These stmts force all future cout’ed values: To have exactly two digits after the decimal place Example: cout << "The price is $" << price << endl; Now results in the following: The price is $78.50 Can modify precision "as you go" as well! Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1-14 14

Boolean Expressions: Display 2.1 Comparison Operators Logical Operators Logical AND (&&) Logical OR (||) Equality and Relational Operators Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 2-15 15

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Branching Mechanisms if-else statements Choice of two alternate statements based on condition expression Example: if (hrs > 40) grossPay = rate*40 + 1.5*rate*(hrs-40); else grossPay = rate*hrs; Like if-else statements in Java Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 2-16 16

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. The switch Statement A new stmt for controlling multiple branches Uses controlling expression which returns bool data type (true or false) Syntax: Display page 62 next slide Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 2-17 17

The break and continue Statements Flow of Control Recall how loops provide "graceful" and clear flow of control in and out In RARE instances, can alter natural flow break; Forces loop to exit immediately. continue; Skips rest of loop body These statements violate natural flow Only used when absolutely necessary! Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 2-18 18

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Loops 3 Types of loops in C++ while Most flexible No "restrictions" do-while Least flexible Always executes loop body at least once for Natural "counting" loop Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 2-19 19

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. File I/O Stream must be declared like any other class variable: ifstream inStream; ofstream outStream; Must then "connect" to file: inStream.open("infile.txt"); Called "opening the file" Uses member function open Can specify complete pathname Declare; Open; and Use. Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-20 20

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Streams Usage Once declared  use normally! int oneNumber, anotherNumber; inStream >> oneNumber >> anotherNumber; Output stream similar: ofstream outStream; outStream.open("outfile.txt"); outStream << "oneNumber = " << oneNumber << " anotherNumber = " << anotherNumber; Sends items to output file Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-21 21

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Closing Files Files should be closed When program completed getting input or sending output Disconnects stream from file In action: inStream.close(); outStream.close(); Note no arguments Files automatically close when program ends Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 12-22 22

Introduction to Functions Building Blocks of Programs Other terminology in other languages: Procedures, subprograms, methods In C++: functions I-P-O Input – Process – Output Basic subparts to any program Use functions for these "pieces" Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3-23 23

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Predefined Functions Libraries full of functions for our use! Two types: Those that return a value Those that do not (void) Must "#include" appropriate library e.g., <cmath>, <cstdlib> (Original "C" libraries) <iostream> (for cout, cin) Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3-24 24

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Even More Math Functions: Display 3.2 Some Predefined Functions (1 of 2) Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3-25 25

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Even More Math Functions: Display 3.2 Some Predefined Functions (2 of 2) Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3-26 26

Random Number Generator Return "randomly chosen" number Used for simulations, games rand() Takes no arguments Returns value between 0 & RAND_MAX Scaling Squeezes random number into smaller range rand() % 6 Returns random value between 0 & 5 Shifting rand() % 6 + 1 Shifts range between 1 & 6 (e.g., die roll) See Example: rand.cpp Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3-27 27

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Random Number Seed Pseudorandom numbers Calls to rand() produce given "sequence" of random numbers Use "seed" to alter sequence srand(seed_value); void function Receives one argument, the "seed" Can use any seed value, including system time: srand(time(0)); time() returns system time as numeric value Library <time> contains time() functions Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3-28 28

Components of Function Use 3 Pieces to using functions: Function Declaration/prototype Information for compiler To properly interpret calls Function Definition Actual implementation/code for what function does Function Call Transfer control to function Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3-29 29

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. main(): "Special" Recall: main() IS a function "Special" in that: One and only one function called main() will exist in a program Who calls main()? Operating system Tradition holds it should have return statement Value returned to "caller"  Here: operating system Should return "int" or "void" Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3-30 30

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Scope Rules Local variables Declared inside body of given function Available only within that function Can have variables with same names declared in different functions Scope is local: "that function is it’s scope" Local variables preferred Maintain individual control over data Need to know basis Functions should declare whatever local data needed to "do their job" Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3-31 31

Call-by-Value Parameters Copy of actual argument passed Considered "local variable" inside function If modified, only "local copy" changes Function has no access to "actual argument" from caller This is the default method Used in all examples thus far Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 4-32 32

Call-By-Reference Parameters Used to provide access to caller’s actual argument Caller’s data can be modified by called function! Typically used for input function To retrieve data for caller Data is then "given" to caller Specified by ampersand, &, after type in formal parameter list Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 4-33 33

Constant Reference Parameters Reference arguments inherently "dangerous" Caller’s data can be changed Often this is desired, sometimes not To "protect" data, & still pass by reference: Use const keyword void sendConstRef( const int &par1, const int &par2); Makes arguments "read-only" by function No changes allowed inside function body Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 4-34 34

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Overloading Same function name Different parameter lists Two separate function definitions Function "signature" Function name & parameter list Must be "unique" for each function definition Allows same task performed on different data Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Overloading Example: Average Function computes average of 2 numbers: double average(double n1, double n2) { return ((n1 + n2) / 2.0); } Now compute average of 3 numbers: double average(double n1, double n2, double n3) { return ((n1 + n2 + n3) / 3.0); } Same name, two functions Copyright © 2008 Pearson Addison-Wesley. All rights reserved.

Introduction to Arrays Array definition: A collection of data of same type First "aggregate" data type Means "grouping" int, float, double, char are simple data types Used for lists of like items Test scores, temperatures, names, etc. Avoids declaring multiple simple variables Can manipulate "list" as one entity Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 5-37 37

Programming with Arrays Plenty of uses Partially-filled arrays Must be declared some "max size" Sorting Searching Which two variables for each partially filled array? Size Number of elements Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 5-38 38

Testing and Debugging Functions Many methods: Lots of cout statements In calls and definitions Used to "trace" execution Compiler Debugger Environment-dependent assert Macro Early termination as needed Stubs and drivers Incremental development Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 4-39 39

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. The assert Macro Assertion: a true or false statement Used to document and check correctness Preconditions & Postconditions Typical assert use: confirm their validity Syntax: assert(<assert_condition>); No return value Evaluates assert_condition Terminates if false, continues if true Predefined in library <cassert> Macros used similarly as functions Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 4-40 40