Title: Structured Programming Instructor: Prof. K. T. Tsang
1Structured Programming Instructor Prof. K. T.
Tsang
2Binary tree
node1
- A structure similar to a linked-list
- Each node links up to 2 nodes below it
- struct bnode
- char data20
- bnode left
- bnode right
-
node2
node3
3The tree structure
- A node on the binary tree may have no more than 2
nodes descend from it - The node on top is called the root node
- The bottom nodes are leaf nodes
Root node
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Leaf nodes
4Example - parents
- struct bnode me, dad, mom
- strcpy (me.data, John)
- strcpy (mom.data, Mary)
- strcpy (dad.data, Lew)
- me.left dad
- me.right mom
- mom.left 0
- mom.right 0
- dad.left 0
- dad.right 0
me
dad
mom
0
0
0
0
5Example - grandparents
- struct bnode grandmom, grandpa, gdmom, gdpa
- strcpy (grandmom.data, Jane)
- strcpy (grandpa.data, Bill)
- mom.left grandpa
- mom.right grandmom
- grandmom.left 0
- grandmom.right 0
- grandpa.left 0
- grandpa.right 0
6Recursive tree-printing
- void print_tree (struct bnode top)
- if (top NULL) return //nothing
- print_tree (top-gtleft) //print left
- printf ( s\n, top-gtdata) //print this node
- print_tree (top-gtright) //print right
-
7Generate random number
- int rand ( void )
- Returns a pseudo-random integral number in the
range 0 to RAND_MAX, a constant defined in
stdlib.h - This number is generated by an algorithm that
returns a sequence of apparently non-related
numbers each time it is called. This algorithm
uses a seed to generate the series, which should
be initialized to some distinctive value using
srand.
8A typical way to generate pseudo-random numbers
in a determined range using rand is to use the
modulo of the returned value by the range span
and add the initial value of the range( value
100 ) is in the range 0 to 99( value 100 1 )
is in the range 1 to 100( value 30 1985 ) is
in the range 1985 to 2014 int num
(100rand())/RAND_MAX In the following example,
the random seed is initialized to a value
representing the second in which the program is
executed (time is defined in the header time.h).
This way to initialize the seed is generally a
good enough option for most random needs.
9/ rand example guess the number / include
ltstdio.hgt include ltstdlib.hgt include
lttime.hgt int main () int iSecret, iGuess
/ initialize random seed / srand ( time(0)
) / generate secret number / iSecret
rand() 10 1 do printf ("Guess the
number (1 to 10) ") scanf ("d",iGuess)
if (iSecretltiGuess) puts ("The secret number is
lower") else if (iSecretgtiGuess) puts ("The
secret number is higher") while
(iSecret!iGuess) puts ("Congratulations!")
return 0
10Structured Programming Instructor Prof. K. T.
Tsang
- Lecture 12 Miscellaneous
- C Preprocessor
- and Debugging
11The C Preprocessor
- Part of the C compilation process that analyzes
some statements before analysis of the C program
itself. - Preprocessor statements begin with a pound
sign, . - Some of these statements we have already learned
- include ltstdio.hgt
- define PI 3.14159
- define YES 1
- define NO 0
12define statement
- define PI 3.141592654
- define YES 1
- define TWO_PI 2.0PI
- A defined name is not a variable. You cannot
assign a value to it. You use it like a constant. - double area (double r)
- return PI r r
-
- double circum (double r)
- return TWO_PI r
-
13More complicated define - Macros
- define LEAP_YEAR(y) y4 0 y100 ! 0
y400 0 - define SQUARE(x) x x
- if (LEAP_YEAR(year))
- //replace by if (year4 0 year100 ! 0
year400 0) - if (LEAP_YEAR(next_year))
- Y SQUARE ( n1 ) //replace by n1 n1
- Y SQUARE ( u1) //replace by (u1)(u1)
14include statement
- include ltstdio.hgt //system include
- include metric.h //file provide by you
- main ()
- float x
- printf (enter distance in miles\n)
- scanf(f, x)
- printf (f miles f km\n, x, MILE_KM x)
15metric.h
- define INCH_PER_CM 0.394
- define CM_PER_INCH 1.0/ INCH_PER_CM
- define MILE_KM 5280.012.0 CM_PER_INCH /
100000.0
16Debugging with define
- include ltstdio.hgt
- define DEBUG //same as define DEBUG 1
- main()
- int i, j, k, nread
- nread scanf (d d d, I, j, k)
- ifdef DEBUG
- printf (number of integers readi\n, nread)
- printf (ii ji ki\n, i, j, k)
- endif
-
-
-
- //scanf returns the number of values successfully
read and assigned.
17Define preprocessor name in the compiler
command line
- Most compiler allows you to define a name to the
preprocessor when the program is compiled by
using a special option to the compiler command - gcc D DEBUG myProgram.cpp
18Example use ifdef to debug
Use pointer to write a function with the
prototype char strConcat(char s1, char
s2) to concatenate(??) 2 strings s1 s2
together and return a pointer points to the
resulting string. Use this function in a program
to show that it works.
19include ltstdio.hgt define DEBUG char
strConcat(char s1, char s2) int i 0,
c0 char s, s399 while (s1 ! '\0')
//check the content is non-null (s3i)
s1 ifdef DEBUG printf ("c", (s3i)) endif
s1 i //pointer arithmetic to scan the
array s1 //end first while loop
20while (s2 ! '\0') //check the content is
non-null (s3i) s2 ifdef DEBUG printf
("c", (s3i)) endif s2 i //pointer
arithmetic to scan the array s2 //end second
while loop s3i '\0' s s3 //set pointer
to beginning of character array ifdef
DEBUG printf ("\ns\ntotal number of characters
d\n", s, i) endif return s
21int main() char str199, str299, s0, s1,
s2 //declare pointers printf ("Enter 2
strings") scanf ("s s", str1,
str2) //points to the beginning of an character
array s1 str1 s2 str2 //pointers in the
arguments and return s0 strConcat (s1,
s2) printf ("After strConcat s\n",
s0) return 0
22Summary
- Preprocessor statements begin with a pound
sign, . - define statement
- Macros more complicated use of define
- include statement
- Conditional compilation with ifdefendif
- Debugging with ifdef
23More on Algorithm 1. Write a C function that
takes two positive integers as input, then
compute and return their Greatest Common Divisor
(GCD). This problem can be solved by Euclidean
algorithm, an efficient method for computing the
GCD and based on the fact that the greatest
common divisor of two numbers does not change if
the smaller number is subtracted from the larger
number. For example, 4 is the GCD of 28 and 12
since 28 - 12 16, the GCD of 12 and 16 is also
4. Since the larger of the two numbers is
reduced, repeating this process gives
successively smaller numbers until one of them is
zero.
242. Euler's totient function f(n) of a positive
integer n is defined as the number of positive
integers less than n that are coprime (or
relatively prime, i.e. having no common factor
other than 1) to n. In particular f(1) 1 since
1 is coprime to itself (1 is the only natural
number with this property, and 1 is counted as
being relatively prime to all natural numbers).
For example, f(9) 6 since the six numbers 1, 2,
4, 5, 7 and 8 are coprime to 9. There are eight
coprimes of 24 that are less than 24 (1, 5, 7,
11, 13, 17, 19, 23), so f(24)8. For a prime
number p, f(p)p-1, since all numbers less than p
are relatively prime to p. Use the gcd function
developed in the previous problem to implement a
function phi(n) to compute and return the Euler's
totient function f(n) of an input positive
integer n.
253. Write a C-program to find the relative
frequency distribution of the English alphabets
by reading a long input text file that contains
an English article and count the number of
occurrence of each alphabet (disregard whether it
is lower or upper cases), then divide it by the
total number of the alphabets. Your program
should be able to read an input English text file
and perform the statistical analysis. Print the
result out to a file and the standard output.
Check your answer by comparing it with the
histogram shown below.
26(No Transcript)
27- include ltstdio.hgt
- void main()
-
- FILE f1, f2 char c int i, fq26, n10,
nt0 float freq26 - //initialize fq to 0
- f1 fopen (input.txt, r)
- f2 fopen (output.txt, w)
- if (f1 NULL) printf (input.txt cannot be
opened) exit(1) - if (f2 NULL) printf (output.txt cannot be
opened) exit(1) - printf (input.txt output.txt opened
successfully) - while ((cgetc(f1)) ! EOF)
- putc(c, f2) printf( c, c) nt
- if(ca cA) fq0 //
-
- printf (\n\nTotal characters read d\n\n,
nt) - fprintf(f2, \n\nTotal characters read d\n\n,
nt) - for (i0 ilt26 i) n1n1fqi
- for (i0 ilt26 i) freqifloat(fqi)/float(n
1) - for (i0 ilt26 i) fprintf(f2, relative freq
f\n, freqi)
28Caesar Cipher - mono-alphabetic cipher
- replace each letter of message by a letter a
fixed distance away (e.g. use the 3rd letter on) - reputedly used by Julius Caesar
- e.g.
- L FDPH L VDZ L FRQTXHUHG
- I CAME I SAW I CONQUERED
29The mapping is ABCDEFGHIJKLMNOPQRSTUVWXYZ DEFGHI
JKLMNOPQRSTUVWXYZABC
304. Write a program that can encrypt and decrypt
using the general Caesar cipher, also known as an
additive cipher. 5. In cryptography, the Cæsar
Cipher can be broken by statistical frequency
analysis. By knowing the expected distribution of
the letters in the original language of the
plaintext, we can easily spot the value of the
shift by looking at the displacement of
particular features of the graph. This is known
as frequency analysis. For example in the English
language the plaintext frequencies of the letters
E, T, (usually most frequent), and Q, Z
(typically least frequent) are particularly
distinctive. The histogram shown previously
exhibits the expected relative frequency
distribution of the English alphabets.
31The simplest numerical integration of a
one-dimensional function, f(x), in the range, a,
b, samples function values at regular intervals,
f(xn a (n - 1/2) ?x) (n 1, ..., M ?x (b
- a)/M)
The Monte Carlo method instead generates a
sequence of random numbers, rn, in the range, a,
b, and approximates the integral as (the only
difference being random instead of regular points)
32(No Transcript)
33Random-number generator in the standard library
The Unix standard library includes a
random-number generator, int rand(). Note the
header file, include ltstdlib.hgt. Successive
calls to this routine generate a sequence of
random numbers in the range, 0, 231 - 1
2,147,483,647.
34(No Transcript)
35(No Transcript)
36RAND_MAX is the maximum random number, 2 31 - 1
2,147,483,647, defined in /usr/include/stdlib.h.
The expression, rand()/(float)RAND_MAX, generates
a random number in the range, 0,1. void srand(
(unsigned) time((int )0) ) To initialize the
sequence of random numbers, use void
srand(unsigned seed), where seed is a random
starting point of a sequence. In the above, the
seed depends on the time the program is run. The
int time(int tloc) system call returns the time
since 000000 GMT, January 1, 1970, measured in
seconds. If tloc is nonzero, the return value is
also stored in the place to which tloc points.