Basic Concept: All The Programs in This File Are Selected From
Basic Concept: All The Programs in This File Are Selected From
BASIC CONCEPT
CHAPTER 1 1/25
How to create programs
Requirements
Analysis: bottom-up vs. top-down
Design: data objects and operations
Refinement and Coding
Verification
– Program Proving
– Testing
– Debugging
CHAPTER 1 2/25
Algorithm
Definition
An algorithm is a finite set of instructions that
accomplishes a particular task.
Criteria
– input
– output
– definiteness: clear and unambiguous
– finiteness: terminate after a finite number of steps
– effectiveness: instruction is basic enough to be carried out
CHAPTER 1 3/25
Data Abstraction(1/2)
Data Type
A data type is a collection of objects and a set of
operations that act on those objects.
Abstract Data Type
An abstract data type(ADT) is a data type that is
organized in such a way that the specification of
the objects and the operations on the objects is
separated from the representation of the objects
and the implementation of the operations.
CHAPTER 1 4/25
Data Abstraction(2/2)
Basic Data types of C :
– char, int, float, double
– short, long, unsigned
Grouping Data types of C :
– Array
– Structure
CHAPTER 1 5/25
*Structure 1.1:Abstract data type Natural_Number
structure Natural_Number is
objects: an ordered subrange of the integers starting at zero and ending
at the maximum integer (INT_MAX) on the computer
functions:
for all x, y ∈ Nat_Number; TRUE, FALSE ∈ Boolean
and where +, -, <, and == are the usual integer operations.
Nat_No Zero ( ) ::= 0
Boolean Is_Zero(x) ::= if (x) return FALSE
else return TRUE
Nat_No Add(x, y) ::= if ((x+y) <= INT_MAX) return x+y
else return INT_MAX
Boolean Equal(x,y) ::= if (x== y) return TRUE
else return FALSE
Nat_No Successor(x) ::= if (x == INT_MAX) return x
else return x+1
Nat_No Subtract(x,y) ::= if (x<y) return 0
else return x-y
end Natural_Number ::= is defined as
CHAPTER 1 6/25
Measurements
Criteria
– Is it correct?
– Is it readable?
– …
Performance Analysis (machine independent)
– space complexity: storage requirement
– time complexity: computing time
Performance Measurement (machine dependent)
CHAPTER 1 7/25
Space Complexity
S(P)=C+SP(I)
Fixed Space Requirements (C)
Independent of the characteristics of the inputs
and outputs
– instruction space
– space for simple variables, fixed-size structured
variable, constants
Variable Space Requirements (SP(I))
depend on the instance characteristic I
– number, size, values of inputs and outputs associated
with I
– recursive stack space, formal parameters, local
variables, return address
CHAPTER 1 8/25
Pointers
int i, *pi;
float j, *pj;
pi = &i; // i=10 or *pi=10
pj = &j; // j=1.5 or *pj=1.5
if (pi==NULL)
if (!pi)
CHAPTER 1 9/25
Dynamic Memory Allocation
int i, *pi;
float f, *pf;
pi = (int *) malloc(sizeof(int)); request memory
pf = (float *) malloc (sizeof(float));
*pi =1024;
*pf =3.14;
printf(”an integer = %d, a float = %f\n”, *pi, *pf);
free(pi);
free(pf); return memory
CHAPTER 1 10/25
Array
int a[5]={1, 2, 3, 4, 5};
…
fun(a, 5);
…
CHAPTER 1 11/25
float abc(float a, float b, float c)
{
return a + b + b * c + (a + b - c) / (a + b) + 4.00;
}
Sabc(I) = 0
Assumptions:
Type Name Number of bytes
parameter: array pointer list [ ] 2/4
parameter: integer n 2/4
return address:(used internally) 2/4(unless a far address)
TOTAL per recursive call 6/12
CHAPTER 1 13/25
Time Complexity
T(P)=C+TP(I)
Compile time (C)
independent of instance characteristics
run (execution) time TP
Definition TP(n)=caADD(n)+csSUB(n)+clLDA(n)+cstSTA(n)
A program step is a syntactically or semantically
meaningful program segment whose execution
time is independent of the instance characteristics.
Regard as the same unit
Example machine independent
– abc = a + b + b * c + (a + b - c) / (a + b) + 4.0
– abc = a + b + c
CHAPTER 1 14/25
Methods to compute the step count
CHAPTER 1 15/25
Iterative summing of a list of numbers
CHAPTER 1 16/25
float sum(float list[ ], int n)
{
float tempsum = 0;
int i;
for (i = 0; i < n; i++)
count += 2;
2n + 3 steps
count += 3;
return 0;
}
CHAPTER 1 17/25
Recursive summing of a list of numbers
CHAPTER 1 18/25
Matrix addition
CHAPTER 1 19/25
void add(int a[ ][MAX_SIZE], int b[ ][MAX_SIZE],
int c[ ][MAX_SIZE], int row, int cols )
{
int i, j; 2rows * cols + 2 rows + 1
for (i = 0; i < rows; i++){
count++; /* for i for loop */
for (j = 0; j < cols; j++) {
count++; /* for j for loop */
c[i][j] = a[i][j] + b[i][j];
count++; /* for assignment statement */
}
count++; /* last time of j for loop */
}
count++; /* last time of i for loop */
}
CHAPTER 1 20/25
void add(int a[ ][MAX_SIZE], int b [ ][MAX_SIZE],
int c[ ][MAX_SIZE], int rows, int cols)
{
int i, j;
for( i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
count += 2;
count += 2;
}
count++;
}
2rows × cols + 2rows +1
Suggestion: Interchange the loops when rows >> cols
CHAPTER 1 21/25
Asymptotic Notation (O)
Definition
f(n) = O(g(n)) iff there exist positive constants c and
n0 such that f(n) ≤ cg(n) for all n, n ≥ n0.
Examples
– 3n+2=O(n) /* 3n+2≤4n for n≥2 */
– 3n+3=O(n) /* 3n+3≤4n for n≥3 */
– 100n+6=O(n) /* 100n+6≤101n for n≥10 */
– 10n2+4n+2=O(n2) /* 10n2+4n+2≤11n2 for n≥5 */
– 6*2n+n2=O(2n) /* 6*2n+n2 ≤7*2n for n≥4 */
CHAPTER 1 22/25
O(1): Constant
O(log n) : Logarithmic
O(n): Linear
O(n log n): Log Linear
O(n2): Quadratic
O(n3): Cubic
O(2n): Exponential
O(n!): Factorial
CHAPTER 1 23/25
CHAPTER 1 24/25
60 2n
n2
50
40
n log n
↑ 30
f
20
n
10
log n
0
0 1 2 3 4 5 6 7 8 9 10
n →
CHAPTER 1 25/25
CHAPTER 1 26/25