csc-201 - computer science i lecture #10: chapter 10...
TRANSCRIPT
-
1/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
CSC-201 - Computer Science ILecture #10: Chapter 10 concluded, Chapter 11
started
Dr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck Cartledge
October 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pmOctober 24, 2016 at 11:01pm
-
2/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
Table of contents I
1 Schedule
2 Miscellanea
3 Chap. 10
4 Chapter 11
5 Break
6 Hands on
7 Q & A
8 Conclusion
9 References
10 Files
-
3/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
Schedule for the semester:
Wk. Date Topic Wk. Date Topic1 08/22 Chaps. 1 2 X 9 10/17 Chap. 10 X2 08/29 Chaps. 3 4 X 10 10/24 Chaps. 10 113 09/05 Chap. 5 X 11 10/31 Chaps. 10 114 09/12 Test Chap. 6 X 12 11/07 Test Chap. 145 09/19 Chap. 7 X1 13 11/14 Chap. 146 09/26 Chap. 8 X 14 11/28 Chap. 187 10/03 Chap. 9 X 15 12/05 Chap. 188 10/10 Test Chap. 10 X 16 12/12 Exam
Notes:
1 Class canceled due to electrical fire in instructors home
-
4/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
Programming assignment #004
File Based Address Book
Objectives are fairly straightforward:
Accept two new commands fromthe user
Commands are:
+ to add a new contact- to remove all contacts that
match a search string
Update the data file with new, orremoved contacts
Submit your source code.
This is a single person effort (not ateam effort).
Due by start of class 26 Oct. 2016.
-
5/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
Programming assignment #005
Internal Array Based Address Book
Objectives are fairly straightforward:
Maintain all the capability fromprogramming assignment #004
Output matching search contactsalphabetized by last name
Support up to 1,000 contacts
Use an array of structures to dothe work
This is a single person effort (not ateam effort).
Due by start of class 9 Nov. 2016.
-
6/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
Programming assignment #005
Q & A time.
Q: How was Thomas J. Watsonburied?A: 9 edge down.
Now is the time to ask questions.
-
7/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
Corrections and additions since last lecture.
Wrote up programmingassignment #005
Took life easy.
-
8/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
A review
A quick review
1 There are complex, userdefined types (UDT) calledstructs
2 A struct is a way to referto multiple variables by thesame name
3 A struct can contain otherstructs
4 You access a structmember using the dotoperator
5 A struct has aggregate andmember operations
-
9/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
A review
Continuing onto other data types
Today looking at:
1 union where multipleidentifiers point to the samememory location
2 pointer and referencewhere we can look atmemory directly
Image from [1].
-
10/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
Memory
Some basics about memory
It all comes down toidentifiers and how to createthem, how to use them, andhow they can be misused.
Well see some of this duringHands On.There are lots of things you cando with identifiers.
-
11/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
Memory
The basics about pointers
An identifier can:
Contain the thing/variabledirectly, or
Contain the memory addressof the thing (called apointer), or
Contain the memory addressof the memory address ofthe . . .
Well play with these in the addresses.cpp program.
-
12/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
Memory
Some pointer details. You can:
Declare a variableint i;
Declare a pointer to a type ofvariableint* intPtr;
Assign a value to a variablei = 12;
Assign the location of a variableto a pointerintPtr = &i;
Change the contents of a memorylocation*intPtr = 13;
Change where to make changes inmemoryintPtr ++;
-
13/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
Memory
Types of expressions
Arithmetic expressions are made up of variables, constants,operators, and parenthesis.
Pointer expressions are composed of pointer variables,pointer constants, pointer operators, and parenthesis.
-
14/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
Memory
Pointers can point to ANYTHING.
Pointers can point to:
Variables (well see this in the hands on)
Structures (structs are just a collection of variables)
Functions (well see this in the hands on)
-
15/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
Memory
Pointers and structs
To access a member of a struct, we use the dot operator (.)To access a variable using a pointer, we use the dereferenceoperator (*)To access a member of a struct, we have two choices:
1 Combine the dereference operator and the dot operator(*patientPtr).weight = 160;
2 Combine the arrow operator with the dot operatorpatientPtr >weight = 160;
-
16/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
Memory
Variations on a theme
Pointers and reference type variables:
Pointer type variables contain the address of a variableint* intPtr;
Reference type variables contain the address of a variableint& intPtr;
Both contain the address of a variable
Differences:
Pointers require the dereference operator (*) and the addressoperator (&) to get the address of the variableReferences do this automatically (i.e., the compiler knowsyou are working with a reference type and inserts operators asnecessary)
-
17/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
One dimensional arrays
Continuing onto other data types (again)
Now diving into areas where arethe variables are the same type.
1 Declaring and using onedimensional arrays
2 Passing arrays to functions
Image from [1].
One dimesional arrays are sometimes called vectors.
-
18/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
One dimensional arrays
Common thread through the structured data types
1 Structured data type stores a collection of individualcomponents (sometimes called members) as part of a singlevariable name
2 Individual components (or members) can be accessed, stored,and retrieved by their position in the structure
3 An array is a structured data type made up of arraycomponents
By defintion; all the components of an array are the same datatype, the array is given a single name, array components are storedin adjacent memory locations.
-
19/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
One dimensional arrays
Some additional common things
Arrays are created like: Type name[number ofcomponents];
int bp[1000];
The number of components is a constant integralexpression
The name of the array is also the base address of the array
An index in square brackets indicates the position of thecomponent in the arrayr = bp[13];
In general, the only aggregate operation defined for arrays isassignment (strings are slightly different)
An index that is out of range may cause strange things to happen:r = bp[-1];
-
20/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
One dimensional arrays
Passing arrays to functions
Arrays are always passed by reference to a function.
The called function is responsible for staying within legalvalues for accessing array components.
There are two different ways to pass an array to a function:
By explicitly declaring an array is being passed:void Obtain(int [], int);
By declaring the address of the array being passed:void Obtain(int* , int);
Which one to choose depends on how the data is used by thefunction.
-
21/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
Break time.
Take about 10 minutes.
-
22/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
Practice exercises
Programs to load into CodeBlocks and get running:
1 cars.cpp working with structs
2 unions.cpp seeing how unions can work for and against you
3 addresses.cpp seeing how addresses can move you around
4 arrays.cpp playing with arrays (creating, assigning values,and printing values)
Others, if you have time.
-
23/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
Q & A time.
Q: What do monsters eat?A: Things.Q: What do monsters drink?A: Coke. (Because Things gobetter with Coke.)
-
24/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
What have we covered?
Finished Chapter 10 with unionsand addressesStarted Chapter 11 discussing onedimensional arrays (vectors)Looked at how to access arrayelements
Next time: continue Chapter 11
-
25/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
References I
[1] Sylvia Sorkin, Programming and problem solving with c++,Teacher Resource Jones and Barlett Learning, 2014.
-
26/26
Schedule Miscellanea Chap. 10 Chapter 11 Break Hands on Q & A Conclusion References Files
Files of interest
1 unions.cpp Playing with unions
2 addresses.cpp Playing with
addresses
3 cars.cpp Reading and working with
structs
4 cars.dat Cars.cpp data file
5 arrays.cpp Creating arrays and
looking at addresses
unions.cpp#include
usingnamespacestd;
unionu_UnionType{
intanInt;
floataFloat;
doubleaDouble;
};
#defineWhereData(a)cout