Beruflich Dokumente
Kultur Dokumente
Nell Dale
Chapter 2
Data Design and Implementation
1
What do mean by data?
● When talk about the function of a program, we
use words such as “add”, ”read”, ”Multiply”,
”write”…and so on.
● The date are the objects that are manipulated,
The information that is processed by a
computer program.
● This information is just a collection of bits that
can be turned off or on (computer’s view).
● Human thinks of information as numbers ,lists.
● To separate computer’s view from our view
we use data abstraction.
2
Data Abstraction
What are the possible values? How can this be done in C++?
3
Data Encapsulation
● is the separation of the representation of
data from the applications that use the
data at a logical level; a programming
language feature that enforces
information hiding.
APPLICATION REPRESENTATION
int y;
y = 25; 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1
4
Encapsulated C++ Data Type int
TYPE
int
Value range: Representation of
INT_MIN . . INT_MAX
int
Operations:
+ prefix as 16 bits two’s
- prefix (inside) complement
+ infix
- infix +
* infix
/ infix Implementation of
% infix Operations
Relational Operators
infix
5
Abstract Data Type (ADT)
6
Data from 3 different levels
● Application (or user) level: modeling
real-life data in a specific context.
7
Viewing a library from 3
different levels
● Application (or user) level: Library of our
University
● Logical (or ADT) level: domain is a collection
of books; operations include: check book
out, check book in, pay fine, reserve a book.
● Implementation level: representation of the
structure to hold the “books”, and the
coding for operations.
8
C++ Built-In Data Types
Simple
Composite
Integral Floating
array struct union class
pointer reference 9
Composite Data Type
10
Two Forms of
Composite Data Types
UNSTRUCTURED STRUCTURED
EXAMPLES: EXAMPLES:
classes and structs arrays
1111
Records
A record is a composite data type made up
of a finite collection of not necessarily
homogeneous elements called members
or fields. For example . . .
thisCar at Base Address 6000
.year 1999
.price 18678.92
12
struct CarType
struct CarType
{
int year ;
char maker[10];
float price ;
} ;
13
Accessing struct members
EXAMPLES
myCar.year
thisCar.maker[4]
14
Valid struct operations
15
Pass-by-value
sends a copy
of the contents of
the actual parameter
CALLING FUNCTION
BLOCK CALLED
SO,
he actual parameter cannot be changed by the functio
1616
Pass-by-reference
sends the location
(memory address)
of the actual parameter
CALLING
BLOCK FUNCTION
CALLED
SAMPLE CALL
AdjustForInflation(myCar, 0.03);
18
Using struct type
Value Parameter to examine a member
bool LateModel(CarType car, int date)
// Returns true if the car’s model year is later than or
// equal to date; returns false otherwise.
{
return ( car.year >= date ) ;
}
SAMPLE CALL
if ( LateModel(myCar, 1995) )
cout << myCar.price << endl ;
19
One-Dimensional Array at the
Logical Level
A one-dimensional array is a structured composite
data type made up of a finite, fixed size (known at
compile time) collection of homogeneous (all of
the same data type) elements having relative
positions and to which there is direct access
(any element can be accessed immediately).
20
Implementation Example
This ACCESSING FUNCTION gives position of values[Index]
Address(Index) = BaseAddress + Index * SizeOfElement
Indexes 21
One-Dimensional Arrays in C++
22
Another Example
This ACCESSING FUNCTION gives position of name[Index]
Address(Index) = BaseAddress + Index * SizeOfElement
Base Address
6000 6001 6002 6003 6004 6005 6006 6007 6008 6009
23
Exercise
struct CarType
{
int year ;
char maker[10];
float price ;
} ;
CarType thisCar; //CarType variables
CarType Cars[20];
Assume that an int and char require one cell, and float
requires two cells, and base address of thisCar is 2000.
year 1 0 2000
maker 10 1 2001
price 2 11 2011
b) How much space does the compiler set aside for Cares.
13 * 20 = 260
25
Passing Arrays as Parameters
● In C++, arrays are always passed
by reference, and & is not used
with the formal parameter type.
26
const array parameter
FOR EXAMPLE . . .
// prototype
27
float SumValues (const float values[ ],
int numOfValues )
// Pre: values[ 0] through values[numOfValues-1]
// have been assigned
// Returns the sum of values[0] through
// values[numOfValues-1]
{
float sum = 0;
for ( int index = 0; index < numOfValues; index++ )
{
sum += values [ index ] ;
}
return sum;
} 2828
Exercises
Declare one-dimensional array,
studentID, that contains 30 integers.
studentID[4]=4214;
carListType buyCar; 31
Exercises
struct carType
{
int year ;
char maker[10];
float price ;
} ;
Assign the value 2002 to the
year member of the 25
component in buyCar.
buyCar[24].year = 2002; 32
Exercises
struct carType
{
int year ;
char maker[10];
float price ;
} ;
Assume that an int and char require one cell, and
float requires two cells, and base address of
buyCar is 4000. What is the address of the member
assigned in
buyCar[24].year = 2002;
Address(Index) = BaseAddress + Index *
SizeOfElement
33
4000+24*13 = 4312
Two-Dimensional Array at the
Logical Level
A two-dimensional array is a structured composite
data type made up of a finite, fixed size collection
of homogeneous elements having relative
positions and to which there is direct access.
34
EXAMPLE -- To keep monthly high temperatures for
50 states in a two-dimensional
array.
[0]
[ 1 ] 66 64 72 78 85 90 99 115 98 90 88 80
row 2,
col 7 [2]
might be .
Arizona’s . stateHighs [2] [7]
high for .
August
[ 48 ] 35
[ 49 ]
Finding the average high temperature for Arizona
int total = 0 ;
int month ;
float average ;
36
Finding the average high temperature for all the
states:
int total = 0 ;
int month ;
float average ;
for (int state = 0; state <NUM_STATES; state ++)
for ( month = 0 ; month < NUM_MONTHS ; month ++ )
total = total + stateHighs [ state ] [ month ] ;
37
const int NUM_STATES = 50 ;
const int NUM_MONTHS = 12 ;
int stateHighs [ NUM_STATES ] [ NUM_MONTHS ] ;
rows columns
STORAGE
● In memory, C++ stores arrays in row order.
The first row is followed by the second row, etc.
Base Address
8000 8024 8048
...
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
[0]
Alaska ? [1] 43 42 50 55 60 78 79 80 77 72 63 40
Arizona ? [2] 66 64 72 78 85 90 99 115 98 90 88 80
.
.
.
[ 48 ]
[ 49 ] 41
void findAverages ( const int stateHighs [ ] [ NUM_MONTHS] ,
int stateAverages [ ] )
// Pre: stateHighs[ 0..NUM_STATES-1] [ 0..NUM_MONTHS-1]
assigned
// Post: stateAverages[ 0..NUM_STATES-1 ] contains rounded
average
// high temperature for each state
{
int state;
int month;
int total;
for ( state = 0 ; state < NUM_STATES; state++ )
{
total = 0 ;
for ( month = 0 ; month < NUM_MONTHS ; month++ )
total += stateHighs [ state ] [ month ] ;
stateAverages [ state ] = int ( total / 12.0 + 0.5 ) ; 42
Declaring Multidimensional Arrays
EXAMPLE USING TYPEDEF
const int NUM_DEPTS = 5 ; // mens, womens, childrens, electronics, linens
const int NUM_MONTHS = 12 ;
const int NUM_STORES = 3 ; // White Marsh, Owings Mills, Towson
MonthlySalesType monthlySales;
43
const int NUM_DEPTS = 5 ; // mens, womens, childrens, electronics, linens
const int NUM_MONTHS = 12 ;
const int NUM_STORES = 3 ; // White Marsh, Owings Mills, Towson
44
12 MONTHS columns
Exercise
● Consider the following matrix:
11 21 30 4
34 70 57 8
92 10 11 12
Assume the array is stored in raw-wise order
beginning at location 2000. Assume each array
element occupies four storage locations. Answer
the following:
● Give the storage address of the value 10.
● Give the value of the seventh element stored.
● Give the value of the element stored at location
2020.
● Give the program segment required to print the
stored values in column- wise order.
● Solve the above again for column order 45