Title: Exam 2
1Exam 2
- EXAM 2 Tuesday July 28th!!!
- 25 of Final Grade
- Know
- loops, switch/case
- Files (.open(), gtgt and ltlt, .close() )
- Input Failure (cin, cin.clear(), cin.ignore())
2To Read or Write from a File
- Declare the File Stream Object Name
- Associate a File Name with the File Stream Object
by Opening the File - Read or Write to the File using ltlt or gtgt
Operators - Close the File
3- include ltiostreamgt
- include ltfstreamgt
- include ltstringgt
- using namespace std
- int
- main()
-
- ifstream input
- int loops, integer, i
- float decimal
- string name
- input.open("mydata.txt")
- input gtgt loops
- for(i 0 i lt loops i)
-
- input gtgt integer
- input gtgt decimal
mydata.txt file 5 8 9.3 Jon 6 14.335 Bill 0
35.67e9 Mary -23 -4.55 Smith -3 -4e3 xyz
Output
8 9.3 Jon 6 14.335 Bill 0 3.567e010 Mary -23
-4.55 Smith -3 -4000 xyz
4Functions
displayVals() cout ltlt cout ltlt
cout ltlt return /back to where
we left off /
int main() displayVals()
displayVals() displayVals()
return(0)
5Functions
- Function A Discrete Piece of Code that Performs
a Specific Operation or Task - Named with a Descriptive Identifier
- Called from main() or Another Function
- When Called, Program Control (Execution) Is
Transferred to the Function - Function Performs Required Tasks, and then
Possibly Returns a Value - After Return from Function, Control Returns to
the Statement Following the Function Call
6Function Attributes
- Function Name Identifier Used to Call Function
- Function Parameter(s) or Argument(s) Value(s)
Passed into Function for Use by Function Code - Function Return Value Value Returned by Function
Back to Calling Function
7Function Parameters (Arguments)
- May Pass as Many Parameters as Necessary to
Function - A Copy of the Value of the Parameter Is Passed to
the Function - Changing the Value of the Parameter in the
Function Does Not Affect the Value of the
Original Variable - This Is Called Pass-by-Value
8Declaring a Function
- Function Prototype Declaring a Function and How
It Is Called Syntactically - Used by the Compiler to Signal Syntax Errors
- Pseudocode Example
- return_type function_name(parameter_type
parameter_name) - Example
- double calculate_avg(int totOfItems, int
numItems)
9Functions Return Values
- Functions Are Typed According to Their Return
Values void, int, double, etc. - Functions Are Declared by Function Prototypes
(Declarations) Found Above main() - Function Returns a Value to Calling Function via
return Statement - Standard Functions (e.g., Those in STL) Have
Function Prototypes in Header Files
10Function Attributes
- include ltiostreamgt
- using namespace std
- void printNum(int) // function prototype
- main()
-
- int myNumber 7
- printNum(myNumber) // function call
- printNum(9)
-
- // begin function definition
- void
- printNum(int numToPrint)
-
- cout ltlt numToPrint
11Another Function Example
- include ltiostreamgt
- using namespace std
- double calcAverage(int total, int numItems)
- main()
-
- int allGrades 974, numStudents 10
- double avgGrade 0.0
- avgGrade calcAverage(allGrades,numStudents)
- cout ltlt avgGrade ltlt endl
-
- double
- calcAverage(int total, int numItems)
-
- return (double) total / numItems
12What If?
- Write a program that prompts for the names and
locations of 1000 employees. Store the
information in the program for later use.
13Program for Previous
- string empName1, empName2, empName3,
- string empLocation1, empLocation2,
- cout ltlt Enter employee name 1
- cin gtgt empName1
- cout ltlt Enter Employee name 2
- cin gtgt empName2
-
- //Can we use a loop?
14Arrays
- Syntax
- type variableNamesize
- Memory Is Set Aside for size Items of type
- Each Variable Location in Array Is Accessed by
Offsetting into the Array with an Integer
Expression - Legitimate Offsets Are 0 to size-1
- Example lastname0 H
15Array Example
- char lastname100
- lastname0 H
- lastname1 a
- lastname2 \0
- cout ltlt lastname0
- cout ltlt lastname
16Array Example
- int values15
- values0 150
- values1 78
- values2 16
- cout ltlt values0
- values3 values0 6
17Array Example
- const int ARRAY_SIZE 100
- int offset
- int numArrayARRAY_SIZE
- for(offset 0 offset lt ARRAY_SIZE offset)
-
- numArrayoffset 0
18Array Example
const int ARRAY_SIZE 10 int offset, sum 0,
average int numArrayARRAY_SIZE for(offset
0 offset lt ARRAY_SIZE offset) cout ltlt
Enter Score ltlt offset ltlt cin gtgt
numArrayoffset for(offset 0 offset lt
ARRAY_SIZE offset) sum sum
numArrayoffset average sum / ARRAY_SIZE
19Initializing Arrays
- int
- main()
-
- char cArray35 'a', 'b', 'c'
- int iArray 1, 2, 3, 4
- int iArray210 10, 13, 15, 17
- double dArray 3.4, 5.67e4
- double dArray15 6.7, 7.8, 9.5
20Initializing string Arrays
- include ltstringgt
- using namespace std
- int
- main()
-
- string sArray "one", "two", "three"
- cout ltlt sArray0
21Two Dimensional Arrays
- char cArray1020
- int iArray10050
- cArray00 a
- cArray01 b
- cArray919 x
- iArray00 99
- iArray15 135
- iArray9949 0
22Exam 2
- EXAM 2 Tuesday July 28th!!!
- 25 of Final Grade
- Know
- loops, switch/case
- Files (.open(), gtgt and ltlt, .close() )
- Input Failure (cin, cin.clear(), cin.ignore())