Exam 2 - PowerPoint PPT Presentation

About This Presentation
Title:

Exam 2

Description:

Exam 2 EXAM 2 Tuesday July 29th!!! 25% of Final Grade Know: loops, switch/case Files (.open(), and – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 23
Provided by: Preferr678
Learn more at: http://www.cs.iit.edu
Category:

less

Transcript and Presenter's Notes

Title: Exam 2


1
Exam 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())

2
To 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
4
Functions
displayVals() cout ltlt cout ltlt
cout ltlt return /back to where
we left off /
int main() displayVals()
displayVals() displayVals()
return(0)
5
Functions
  • 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

6
Function 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

7
Function 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

8
Declaring 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)

9
Functions 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

10
Function 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

11
Another 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

12
What If?
  • Write a program that prompts for the names and
    locations of 1000 employees. Store the
    information in the program for later use.

13
Program 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?

14
Arrays
  • 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

15
Array Example
  • char lastname100
  • lastname0 H
  • lastname1 a
  • lastname2 \0
  • cout ltlt lastname0
  • cout ltlt lastname

16
Array Example
  • int values15
  • values0 150
  • values1 78
  • values2 16
  • cout ltlt values0
  • values3 values0 6

17
Array Example
  • const int ARRAY_SIZE 100
  • int offset
  • int numArrayARRAY_SIZE
  • for(offset 0 offset lt ARRAY_SIZE offset)
  • numArrayoffset 0

18
Array 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
19
Initializing 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

20
Initializing string Arrays
  • include ltstringgt
  • using namespace std
  • int
  • main()
  • string sArray "one", "two", "three"
  • cout ltlt sArray0

21
Two Dimensional Arrays
  • char cArray1020
  • int iArray10050
  • cArray00 a
  • cArray01 b
  • cArray919 x
  • iArray00 99
  • iArray15 135
  • iArray9949 0

22
Exam 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())
Write a Comment
User Comments (0)
About PowerShow.com