Tải bản đầy đủ (.pdf) (6 trang)

INTRODUCTION TO COMPUTER SCIENCE - PART 1 ppsx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (151.88 KB, 6 trang )

INTRODUCTION TO COMPUTER SCIENCE
HANDOUT #1. REVIEWS
K5 & K6, Computer Science Department, Vaên Lang University
Second semester Feb, 2002
Instructor: Traàn Ñöùc Quang
Major themes:
1. Introduction to Computer Science
2. Three Columns: Data Models, Data Structures, and Algorithms
3. C Essentials
4. Arrays and Linked Lists
Reading: Sections 1.1, 1.3, 1.4, and 6.4 (textbook), C Reference (Microsoft Studio
6.0 Books Online)
1.1 INTRODUCTION TO COMPUTER SCIENCE
A science of abstraction: creating the right model of a real world.
Example: In Windows, we can use a small program called Paint for drawing
simple pictures. Paint has tools such as Pencil, Brush, Airbrush, Eraser, etc.
They are not real tools; rather they are the models of real tools.
How to use a computer to solve a problem?
1. Choose the important features of the problem ==> analysis
2. Build a model to reflect those features ==> abstraction
3. Write a program to solve it ==> mechanization
How to write a good computer program?
1. Design data structures
2. Design good algorithms
3. Choose an appropriate programming language to implement the program
Example: You are hired to implement a computer system for a travel agency. Your
system must determine the best route for a traveler to get from location A to location
B ("best" means shortest distance traveled). How would you do it?
6 INTRODUCTION TO COMPUTER SCIENCE: HANDOUT #1. REVIEWS
1. Specify the locations that the system can handle.
2. Create a database of distances between all the locations. Such a database must


have a structure that allows for easy updates and quick searches.
3. Create algorithms that will operate on the database.
4. Create an algorithm that finds the shortest route between two locations.
5. Implement a program that takes as input location A and B and outputs the
shortest route between the two.
There are several examples of abstraction in this example:
• representing the locations and distances as names and numbers
• placing this data in some kind of abstract data structure in a computer
• creating abstract operations on this data structure such as "Find" and "Find
Shortest Distance"
1.2 DATA MODELS, DATA STRUCTURES, AND ALGORITHMS
Three basic concepts in CS: data models, data structures, and algorithms.
1. Data models: The picture of data in an abstract form. In the previous example,
we could use a graph as a model to represent locations and distances: a node
with a name for each location and a line with a number for a distance of two
locations.
2. Data structures: Organizations in memory to hold data. For example, to repre-
sent locations in a computer memory, we need small regions to hold names and
some other information about locations. The distance between two locations can
be stored in a table structure with three fields: the first two for the names and
the last for the distance.
3. Algorithms: A sequence of steps to solve a problem, often as a guide for a com-
puter to do.
Here is Euclide’s algorithm from Knuth’s book (TAOCP, Volume 1, pp. 2): Given
two positive integers m and n, find their greatest common divisor, that is, the
largest positive integer that evenly divides both m and n.
E1. [Find remainder.]. Divide m by n and let r be the remainder. (We will have
0 ≤ r < n.)
E2. [Is it zero?] If r = 0, the algorithm terminates; n is the answer.
E3. [Reduce.] Set m ← n, n ← r, and go back to step E1.

A
B
15 (km)
1.3 C ESSENTIALS 7
1.3 C ESSENTIALS
The two main parts of the programming language C: Data and Statements
1. Data. During the execution of a program, the data must be stored in a region of
the memory that is known as a data object or data item. Data objects have the
following attributes:
• Data types: A type is a set of values and a set of operations on those values.
For examples, the values of an integer type consists of integers in some spec-
ified range, and the operations on those values consist of addition, substrac-
tion, inequality tests, and so forth. In C, there are two broad categories of
types that form the type system: basic or built-in types (char, int, long,
float, etc.), and user-defined types (pointers, arrays, structures, unions).
• Sizes: A number of bits used to represent the object. It depends on the type.
For example, an object of type char is represented in 8 bits; of type int can
be represented either 16 or 32 bits depending on the implementation.
• Values: An object could hold a value that is in the set of possible values called
its domain. A value is either changeable or unchangeable.
If the value is changeable, the object is a variable; otherwise, it is a constant.
• Addresses: In a byte-addressable computer, an object has its own address;
that is the address of the first byte of bytes allocated for the object.
• Names: An object may have a name that can be used to refer to it. Some data
without a name can be given in the source code. They are called literals.
The above figure represents two data objects in memory. The numbers on the left
are addresses. The object named i is of type int and holds the value of 25. It is
102
102
25

68
0
pt
i
RAM
8 INTRODUCTION TO COMPUTER SCIENCE: HANDOUT #1. REVIEWS
located at address 102. This address is the value of the object pt, an object of
type "pointer to int". The address of the pointer pt is 68.
2. Statements. We use statements to direct a computer to do some task. A state-
ment may indicate the flow of control or an operation to be performed in pro-
grams. In C,
• Flow of control statements: for, while, do-while (loops), if-else, switch-
case (selections), and others (break, continue, return).
• Operators: assignments, additions, multiplications, ect.
1.4 BASIC DATA STRUCTURES
In memory, there are only two physical data structures: arrays and linked lists.
1. Arrays: To hold a set of objects of the same type, we need a contiguous region
large enough to accommodate those objects. From an intuitive point of view, it is
best to think of an array as a sequence of boxes, one box for each data value in
the array. Each value in an array is called an element. Each element of the array
is identified with a numeric value called it index. Array indices begin at 0, so
that the nth element of an array had an index of n−1.
Arrays have two properties which vary depending on the specific array:
• element type: also referred to as base type, this specifies the data type of the
element
• size: the maximum number of elements the array can hold. The size must be
determined at declaration time.
In the figure, num is an array of type int with size 4. In general, the type of
objects is any type in the type system of C.
1002

5
0
num
8
3
10
0
1
2
3
1.5 GLOSSARY 9
2. Linked Lists. An array allocates memory for all its elements lumped together as
one block of memory. In contrast, a linked list allocates space for each element
separately when needed in its own block of memory called a "linked list element"
or "node". The list gets its overall structure by using pointers to connect all its
nodes together like the links in a chain.
• Each node contains two fields: a "data" field (info) to store whatever element
type the list holds for its client, and a "next" field which is a pointer used to
link one node to the next node. This kind of linked list is called singly linked
list.
• Each node may also contain other fields, such as a "previous" field to hold a
pointer to the previous node, forming a so-called doubly linked list.
We only need to keep track of the first node of the list using a pointer called a
header.
Any other data structures are implemented by either arrays or linked lists. I will
explain in class how operations on individual structures can be used when we reach to
the topics.
1.5 GLOSSARY
Abstraction: Trừu tượng hóa. The process of creating a description of a real-world
problem by extracting important characteristics to be represented in a computer.

to abstract: trừu xuất.
Problem: Bài toán, vấn đề. A question proposed for solution or consideration.
Solution: Lời giải (nghiệm), giải pháp. An answer to a problem.
to solve: giải (một bài toán, vấn đề).
Model: Mô hình. An abstract description used in capturing the important characteris-
tics of a real-world problem to be represented and manipulated in a computer.
to model: mô hình hóa.
Data Model: Mô hình dữ liệu. A way of describing and representing data.
Element 1 Element 2 Element 3 Element 4
Info1 Info2 Info3 Info4

10 INTRODUCTION TO COMPUTER SCIENCE: HANDOUT #1. REVIEWS
Implementation: Sự cài đặt, lắp đặt; bản cài đặt. (1) The process of installing a com-
puter system; (2) The process of building a sotfware product from its design; (3) a
software running on a computer.
to implement, to install: cài đặt, hiện thực.
Operation: Phép toán, thao tác; Sự hoạt động, điều hành. (1) An action specified by a
single computer instruction or high-level statement (2) Any defined action.
to operate: thao tác, hoạt động.
operator: người điều hành, toán tử.
operand: toán hạng.
Execution: Thực thi, chạy. The running of a program on a computer.
to execute: to run.
Data Type: Kiểu dữ liệu. See the definition in text.
built-in type: kiểu cài sẵn.
user-defined type: kiểu do người dùng đònh nghóa, kiểu tự tạo.
Variable: Biến. See the definition in text.
Constant: Hằng. See the definition in text.
RAM: Bộ nhớ chính. Random Access Memory, main memory.
Pointer: Con trỏ. An data object (variable or constant) holding the address of another

object.
Statement: Câu lệnh. An sentence to make a computer to perform an action, usually in
a high-level language.
Instruction: Chỉ thò. An coded operation to be performed in a computer, usually in
binary form (machine language) or mnemonic form (assembly).
Index: Bản chỉ dẫn; Chỉ mục. (1) A structure to quickly search for a subject or name in
a book or the like; (2) A number indicating the location of an element in an array.
Array: Mảng. See the definition in text.
List: Danh sách. See the definition in text.
Linked List: Danh sách liên kết. See the definition in text.
Header: Con trỏ đầu. See the definition in text.

×