Title: Presentation Number 7
1Presentation Number 7
2Last Time in Class
- Recursion - a method for defining functions in
which the function being defined is used within
its own definition. - Two things you must remember about recursions
- Change the parameters from call to call!!!
- Make sure you defined a boundary condition!!!
3Merge Sort
- Given two arrays of sorted numbers, how do you
merge the two into one sorted array?
Left array
Right array
Mergerd array
4Merge Sort
- Loop across the array and copy the smaller number
into the merged array.
Is the current left number smaller then the
current right number?
Copy the left number into the merged array
5Merge Sort
- Loop across the array and copy the smaller number
into the merged array.
Is the current left number smaller then the
current right number?
Copy the left number into the merged array
6Merge Sort
- What happens when one of the arrays has reached
the end?
Copy the rest of the other array into the merged
array
7Merge Sort
- Given the merge algorithm that we just saw, how
do we sort an array? - Sort the left half.
- Sort the right half.
- Merge the two halves.
- What is the boundary condition?
Sort the left half
Sort the right half
Merge into one sorted array
8Pointers
9Declaring variables - Reminder
- Before using a variable, one must declare it.
- The declaration introduces the variable type,
then its name. - When a variable is declared, its value is
undefined.
type variable_name
10Declaring arrays - Reminder
- An array is a block of variables of the same
type. - The array variable holds the address in memory of
the beginning of the sequence. - The nth element of an array is specified by
Arrn-1 - We start counting from 0.
- The ith element of an array can be treated as any
other variable.
type variable_nameSIZE
11Declaring a pointer variable
- A pointer is declared by adding a before the
variable name. - A pointer is a variable that contains an address
in memory. - The address should be the address of a variable
or an array that we defined.
type variable_name
12Pointers
- int a 1
- int b 2
- int ptr NULL
ptr
13Referencing
- The unary operator gives the address of a
variable - The statement
- ptr a
- assigns the address of a to the pointer
variable ptr. - The variable pointer ptr now points to variable
a.
14Pointers
- int a 1
- int b 2
- int ptr NULL
- ptr a
ptr
ptr
P is assigned the address of variable a
15Dereferencing
- The unary operator is the dereferencing
operator. - It is applied on pointers.
- It accesses the object the pointer points to.
- Follow the pointer and perform the operation on
the actual memory that the pointer points to.
16Pointers
- int a 1
- int b 2
- int ptr NULL
- ptr a
- ptr b
ptr
ptr
The physical address to which ptr points is
assigned the value of b.
ptr
17Pointers - Illustration
memory
num
int num
p
5
int p
p num
p 5
int arr5
arr
p arr
p 7
0
7
p3 0
18Common errors
- It is impossible to define pointers to constants
or expressions. - i 3
- j (k 5)
- k (a b)
- It is impossible to change a variables address
- It is not ours to change!!!!!!!!!
- a 150
- a b
19Wrong Swap
A swap that gets integers as variables does not
change the value in the original variables.
- void swap(int x, int y)
-
- int tmp x
- x y
- y tmp
-
- int main()
-
- int x 5, y 7
- swap( x, y )
- printf( x d and y d\n, x, y )
- return 0
x 5 and y 7
20How can we fix it?
- void swap(int x, int y)
-
- int temp x
- x y
- y temp
-
- int main()
-
- int x 5, y 7
- swap( x, y )
- printf( "x d and y d\n", x, y )
- return 0
We define the swap function so it gets pointers
to integers instead of integers.
x 7 and y 5
21Scanf
- We can now understand the in scanf(d, x)
- The argument list in scanf is simply passed by
reference, so scanf can change its content.
22Arrays and Pointers
- An array arr holds the address of its first
element arr0. - arr is actually a pointer to arr0.
- char arr10char iptr NULLiptrs
- Both iptr and arr now point to arr0.
23Pointer-array equivalence
- An array is a kind of pointer.
- When an array is defined, a fixed amount of
memory (the size of the array) is allocated. - The array variable is set to point to the
beginning of that memory segment. - When a pointer is declared, it is uninitialized
(like a regular variable) and it points to
somewhere.
The address to which an array variable points
cannot be changed!
24Arrays and Pointers
- for ( p a p lt a N p )
- sum p
-
- is equivalent to
- for ( i 0 i lt N i )
- sum ( a i )
- is equivalent to
- p a
- for ( i 0 ilt N i )
- sum p i
Declarations int p int aN
Illegal expressions
a p a a 2 a
25Pointer arithmetic
- Pointers can be incremented and decremented
- If p is a pointer to a particular type, p1
yields the correct address of the next variable
of the same type. - p, pi, and p i also make sense.
- If p and q are of the same type and point to
elements in an array, q-p is the number of
elements between p and q.
26my_strncpy
- void my_strncpy(char dest, const char src, int
size) -
- int i
- for ( i0 i lt size i )
-
- dest src
- dest
- src
-
27my_strncpy
- void my_strncpy(char dest, const char src, int
size) -
- int i
- for ( i0 i lt size i )
-
- dest src
- dest
- src
-
src
dest
28my_strcpy
- void my_strncpy(char dest, const char src, int
size) -
- int i
- for ( i0 i lt size i )
-
- dest src
- dest
- src
-
src
dest
29my_strcpy
- void my_strncpy(char dest, const char src, int
size) -
- int i
- for ( i0 i lt size i )
-
- dest src
- dest
- src
-
src
dest
30my_strcpy
- void my_strncpy(char dest, const char src, int
size) -
- int i
- for ( i0 i lt size i )
-
- dest src
- dest
- src
-
src
dest
31my_strcpy
- void my_strncpy(char dest, const char src, int
size) -
- int i
- for ( i0 i lt size i )
-
- dest src
- dest
- src
-
src
dest
32my_strcpy
- void my_strncpy(char dest, const char src, int
size) -
- int i
- for ( i0 i lt size i )
-
- dest src
- dest
- src
-
src
dest
33my_strcpy
- void my_strncpy(char dest, const char src, int
size) -
- int i
- for ( i0 i lt size i )
-
- dest src
- dest
- src
-
src
dest
34Examples
35Example
- We need a function that gets a number of type
double and returns its integer and fraction
parts. - What parameters should the function have?
36Example
- We need a function that gets a number of type
double and returns its integer and fraction
parts. - What parameters should the function have?
- void split(double num, int int_part, double
frac_part) -
- int_part (int)num
- frac_part num - int_part
37Example
- How would you write a function like this?
- void replace_char(char str, int size, char c1,
char c2) - The function replaces each appearance of c1 by c2
in the string str. - How do you do this without the operator?
38Solution
- void replace_char(char str, int size, char c1,
char c2) -
- int i
- if (str NULL)
- return
- for ( i0 i lt size i, str )
-
- if (str c1)
- str c2
-