7/31/2019 992 DataStruAlgo Arrays
1/23
Data Structures and Algorithms
Data type and Arrays
Instructor: Chien-Yu ChenBIME@NTU
7/31/2019 992 DataStruAlgo Arrays
2/23
Chien-Yu Chen, BIME@NTU
Choice of proper data structure
once the choice has been made, the
necessary algorithms are simple.
For the same data, some datastructures require more or less space
than others; for the same operations
on the data, some data structures
lead to more or less efficientalgorithms than others.
7/31/2019 992 DataStruAlgo Arrays
3/23
Chien-Yu Chen, BIME@NTU
Data structure
A data structure is not a passive object: We
also must consider the operations to be
performed on it (and the algorithms used for
these operations). The data structures that we consider in this
chapter are important building blocks that
we can use in a natural manner in C++ and
many other programming languages.
7/31/2019 992 DataStruAlgo Arrays
4/23
Chien-Yu Chen, BIME@NTU
type and function
Types allow us to specify how we will
use particular sets of bits and
functions allow us to specify the
operations that we will perform on the
data.
7/31/2019 992 DataStruAlgo Arrays
5/23
Chien-Yu Chen, BIME@NTU
Basic type of data
Integers (ints)
int, lont int, short int
Floating-point numbers (floats) float, double
Characters (chars)
7/31/2019 992 DataStruAlgo Arrays
6/23
Chien-Yu Chen, BIME@NTU
Type-dependent operations
7/31/2019 992 DataStruAlgo Arrays
7/23Chien-Yu Chen, BIME@NTU
Program 3-1
7/31/2019 992 DataStruAlgo Arrays
8/23
Chien-Yu Chen, BIME@NTU
Program 3-2
7/31/2019 992 DataStruAlgo Arrays
9/23
Chien-Yu Chen, BIME@NTU
Program 3-2 (cont.)
7/31/2019 992 DataStruAlgo Arrays
10/23
Chien-Yu Chen, BIME@NTU
Changing data type
7/31/2019 992 DataStruAlgo Arrays
11/23
Chien-Yu Chen, BIME@NTU
Using header file
In C++, an alternative approach is to
put the typedef and randNum into a
separate header filecalled, say,
Number.hreplacing them with thedirective
include Number.h
in the code in Program 3.2.
7/31/2019 992 DataStruAlgo Arrays
12/23
Chien-Yu Chen, BIME@NTU
interface
7/31/2019 992 DataStruAlgo Arrays
13/23
Chien-Yu Chen, BIME@NTU
An example in C
/* in the file named Number.h */
typedef int Number;
Number randNum();
/* in the file named int.c */
#include #include Number.h
Number randNum()
{ return rand(); }
/* in the file named avg.c */
#include #include
#include Number.h
...
7/31/2019 992 DataStruAlgo Arrays
14/23
Chien-Yu Chen, BIME@NTU
7/31/2019 992 DataStruAlgo Arrays
15/23
Chien-Yu Chen, BIME@NTU
pointer
Apointeris a reference to an object inmemory (usually implemented as amachine address).
We declare a variable a to be a pointer to
(for example) an integer by writing int *a,and we can refer to the integer itself as *a.
We can declare pointers to any type ofdata. The unary operator& gives the
machine address of an object, and is usefulfor initializing pointers. For example, *&a is the same as a.
7/31/2019 992 DataStruAlgo Arrays
16/23
Chien-Yu Chen, BIME@NTU
New data type
Implementation
Using it
7/31/2019 992 DataStruAlgo Arrays
17/23
Arrays
a[i]
7/31/2019 992 DataStruAlgo Arrays
18/23
Chien-Yu Chen, BIME@NTU
Using pointers
7/31/2019 992 DataStruAlgo Arrays
19/23
Chien-Yu Chen, BIME@NTU
Sieve of Eratosthenes
Eratosthenes (276~194 BC) .Eratosthenes (Sieve). , 100
2~100 2, 2 , 2
3, 3 , 3
5, 5 , 5
..., , ,
2,3,5,7,...,97 100
http://home.educities.edu.tw/kuen/maths/algorithm01.htm
7/31/2019 992 DataStruAlgo Arrays
20/23
Chien-Yu Chen, BIME@NTU
7/31/2019 992 DataStruAlgo Arrays
21/23
Chien-Yu Chen, BIME@NTU
Dynamic memory allocation
7/31/2019 992 DataStruAlgo Arrays
22/23
Chien-Yu Chen, BIME@NTU
7/31/2019 992 DataStruAlgo Arrays
23/23
Chien-Yu Chen, BIME@NTU