Title: Arrays, Pointers, and String
1Arrays, Pointers, and String
2Outline
- One-dimensional Arrays
- Pointers
- Call-by-Reference
- The Relationship between Arrays and Pointers
- Pointer Arithmetic and Element Size
- Array as Function Arguments
- Example
- Dynamic Memory Allocation with calloc() and
malloc()
3Outline (continued)
- Example
- Strings
- String-Handling Functions in the Standard Library
- Multidimensional Arrays
- Arrays of Pointers
- Arguments to main()
4Outline (continued)
- Ragged Arrays
- Functions as Arguments
- Example
- Arrays of Pointers to Function
- The Type Qualifiers const and volatile
5One-dimensional Arrays
- Array is a date type, which can be used for a
large number of homogeneous values. - Array and pointer are close related concepts.
6One-dimensional Arrays
- In order to store homogeneous data, we need have
a sequential storage. It is used to store same
data type values shared with the same variable
name. - instead of using
int grade0, grade1, grade2,
int grade3
7One-dimensional Arrays
- For one dimensional array, we need to specify one
constant integral for the size (total number of
elements)
int asize
The size should be specified before declaration.
define N 1000 int aN space for a0,
a1, aN-1 is allocated.
8One-dimensional Arrays
- The element of an array can be accessed using
subscript variable - Arrays can be initialized when declaration.
- There are several way to initialize an array.
for (i0 iltN i) sumai
9One-dimensional Arrays
- Both size and initialized values are provided
- Initialize a constant value
float f50.0, 1.0, 2.0, 3.0, 4.0
int a0
10One-dimensional Arrays
- Only initialized values are provided
- char array can use alternative initialization,
besides of the regular initialization methods
float f0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0
char s "abcd" char s 'a', b', 'c',
'd'', '\0'
11One-dimensional Arrays
- Array's subscript or index can be an integral
expression. The subscript is used to referr each
element. It starts with 0!
12Pointers
- Each variable is stored in a certain number of
bytes at a particular memory location. - Pointer is introduced to access memory and
manipulate addresses and the corresponding value
stored. - Access address or location
if v is a variable, v is the location of the
variable in memory
13Pointers
- Pointer variables can be declared in programs and
then used to take addresses as value - Different pointer variables of data type are used
take the different variables' addresses.
int p
declare a pointer variable of int data-type. It
can be used to take the address of an int
variables.
14Pointers
float p
declare a pointer variable of float data-type. It
can be used to take the address of a float
variables. It can not be used to take int
variable's address
15Pointers
Initialization of pointer variable
p0 pNULL pi p(int ) 1776 / take the
address of absolute address in memory) /
pointer variable of int type is referring to int
variable i, or is pointing to the variable i
16Pointers
- Indirection or dereference using unary operator
p is the value of variable of which p is
pointing to. p is the value of variable to
which p is pointing to if we have pi,
p i
17include ltstdio.hgt int main(void) int i
7, p p i printf("sd\nsp\n",
" Value of i ", p, "Location of i ",
p) return 0
int a1, b2, p pa bp ba
Value of i 7 Location of i 7bff07c0
18Pointers
- A pointer can be initialized in a declaration.
int i, pi
int i3, j5, pi, qj, r double
x pi p(i) 1 p (p) 3 rx r(x
) illegal 7p/q7 (((7(p)))/(q)7 11 (rj
)p ((r(j)))(p) 15
19Pointers
p is the address of p, (p)
p (the value associated with the address of p
value if i
p
20Pointers
int i, pi
int p float q void r legal illegal p0 p
1 p(int ) 1 v1 pvq pq p(int ) q
21Pointers
- do not point at constants, except 0
- 3
- do not point at ordinary expression
- (k99)
- do not point at register variable
- register v
- v
22Call-by-Reference
- Passing the addresses of variables from the
calling function to the called function
23include ltstdio.hgt void swap(int , int
) int main(void) int i 3, j 5
swap(i, j) / 5 3 is printed /
printf("d d\n", i, j) return 0
void swap(int p, int q) int tmp tmp
p p q q tmp
5 3
24The Relationship Between Arrays and Pointers
- Array name itself is an address of the first
element. It is fixed. - However, array can use pointer notation.
- Pointer can points different address of variable
of the same type. - Pointer can use array notation
25array notation pointer notation
array ai (ai) pointer pi (p1)
define N 100 int aN, i, p, sum0 pa
pa0
pa1
pa1
26for (i0iltNi) sumai
for (papltaN p) sump
for (i0iltNi) sum(ai)
pa for (i0iltNi) sumpi
illegal (since a is a constant or static
pointer) ap a a2 a
27Pointer Arithmetic and Element Size
- Pointer arithmetic is one of the power features
of C. If the variable p is a pointer to a
particular type, then the expression p1 yields
the correct machine address for storing or
accessing the next variable of that type. - pi, p, and pi all make sense.
- pointer expression and arithmetic expression have
a difference
28double a2, p, q paqp1 printf("d\n",
q-p) output is 1 printf("d\n", (int) q-(int)
p)) output is 8, since double has 8
byte I should say the unit is different.m The
different in terms of array element is 1, but the
different in memory location is 8.
29Arrays as Function Arguments
- If you want to pass whole array to function, you
actually pass the value of address to the called
function.
double sum(double a, int n) int i double
sum0.0 for (i0iltni) sumai return
sum
double sum(double a, int n) int i double
sum0.0 for (i0iltni) sumai return
sum
30define N 10 double sum(double , int) int
main() int i double vN for
(i0iltNi) vi(double) i
printf("lf\n", sum(v,N)) printf("lf\n",
sum(v,8)) printf("lf\n", sum(v5,3))
printf("lf\n", sum(v3,3)) printf("lf\n",
sum(v3,221))
double sum(double a, int n) int i double
summation0.0 for (i0iltni)
summationai return summation
45.000000 28.000000 18.000000 12.000000 25.000000
31in main(), we have double v1003.0 sum(v,10
0) return v0v1v99 sum(v,88) retur
n v0v1v87 sum(v7, k-7) return
v7v8vk-1 sum(v7,2k) return
v7v8v2k6
32Example Bubble Sort
void swap(int p, int q) int tmp tmp
p p q q tmp
/ n is the size of a / void bubble(int a,
int n) int i, j void swap(int
, int ) for (i 0 i lt n - 1 i)
for (j n - 1 j gt i --j) if (aj-1 gt
aj) swap(aj-1, aj) / or
swap (a(j-1), aj) /
33Dynamic Memory Allocation with calloc() and
malloc()
- These functions in standard library can be used
to reserve memory allocation dynamically during
execution. - The function prototypes of these two functions
are in stdlib.h - calloc stands for contiguous allocation while
malloc stands for memory allocation
34Dynamic Memory Allocation with calloc() and
malloc()
- calloc(n, elementSize) return void
size of data type
number of elements
it allocates contiguous space in memory for an
array of n elements, with each element having
elementSize bytes It initializes each elements
a(int ) calloc(n, size(int)
type casting to int
35Dynamic Memory Allocation with calloc() and
malloc()
- malloc(nelementSize) return void
size of data type
number of elements
it allocates contiguous space in memory for an
array of n elements, with each element having
elementSize bytes It doesn't initialize each
element efficient.
a(int ) malloc(nsize(int)
type casting to int
36Dynamic Memory Allocation with calloc() and
malloc()
- free the reserved space using
free(prt)
37include ltstdio.hgt include ltstdlib.hgt include
lttime.hgt void fill_array(int a, int n) int
sum_array(int a, int n) void wrt_array(int
a, int n)
38include "array.h" void fill_array(int a, int
n) int i for (i 0 i lt n i)
ai rand() 19 - 9 int sum_array(int a,
int n) int i, sum 0 for (i 0 i lt
n i) sum ai return sum
39void wrt_array(int a, int n) int i
printf("a ") for (i 0 i lt n i)
printf("ds", ai, ((i lt n - 1) ? ", "
"\n"))
40include "array.h" int main(void) int a,
n srand(time(NULL)) / seed the random
number generator / printf("\ns\n",
"This program does the following repeatedly\n"
"\n" " 1 create space for an array
of size n\n" " 2 fill the array with
randomly distributed digits\n" " 3 print
the array and the sum of its element\n" "
4 release the space\n") for ( )
printf("Input n ")
41 if (scanf("d", n) ! 1 n lt 1)
break putchar('\n') a calloc(n,
sizeof(int)) / allocate space for a /
fill_array(a, n) wrt_array(a, n)
printf("sum d\n\n", sum_array(a, n))
free(a) printf("\nBye!\n\n") return
0
42 This program does the following repeatedly
1 create space for an array of size n 2 fill
the array with randomly distributed digits 3
print the array and the sum of its element 4
release the space Input n 10 a 7, -3, -8, 8,
-5, -3, -5, 5, 0, -6 sum -10 Input n -1 Bye!
43Dynamic Memory Allocation with calloc() and
malloc()
- Offsetting the Pointer
- Since array index starts with 0.
- If we want to shift 1, we can use offsetting the
pointer, example
int n double a a(double )
calloc(n1,sizeof(double))
44Dynamic Memory Allocation with calloc() and
malloc()
- Alternatively, we can use pointer arithmetic
int n double a a(double )
calloc(n,sizeof(double)) --a / this way
we can use a1. a2, in order to free it,
we need free(a1)
45Example Merge and Merge Sort
46merge.c file
/ // Merge a of size m and b of size n into
c. / include "mergesort.h" void merge(int
a, int b, int c, int m, int n) int i
0, j 0, k 0 while (i lt m j lt n)
if (ai lt bj) ck ai
47 else ck bj while (i lt m)
/ pick up any remainder /
ck ai while (j lt n) ck
bj
48mergesort.h file
include ltstdio.hgt include ltstdlib.hgt define
KEYSIZE 16 void merge(int a, int b, int
c, int m, int n) void mergesort(int key,
int n)
49/ mergesort.c // Mergesort Use merge() to sort
an array of size n. / include
"mergesort.h" void merge(int , int , int ,
int, int) void mergesort(int key, int n)
int j, k, m, w for (m 1 m lt n m
2) if (m ! n) printf("ERROR
Size of the array is not a power of 2 -
bye!\n") exit(1)
50 w calloc(n, sizeof(int)) /
allocate workspace / for (k 1 k lt n k
2) for (j 0 j lt n - k j 2 k)
merge(key j, key j k, w j, k, k)
/ merge into w / for (j 0 j lt n j)
keyj wj /
write w back into key / free(w)
/ free the workspace
/
51include "mergesort.h" void wrt(int key, int
sz) int i for (i0iltszi)
printf("4ds", keyi, ((iltsz-1)?"""\n"))
wrt.c
52include "mergesort.h" int main(void) int
sz, key 4, 3, 1, 67, 55, 8, 0, 4,
-5, 37, 7, 4, 2, 9, 1, -1
szsizeof(key)/ sizeof(int) printf("Before
mergesort\n") wrt(key,sz) mergesort(key,
KEYSIZE) printf("After mergesort\n")
wrt(key,sz) return 0
main.c
53Before mergesort 4 3 1 67 55 8 0
4 -5 37 7 4 2 9 1 -1 After
mergesort -5 -1 0 1 1 2 3 4 4
4 7 8 9 37 55 67
54Strings
- Strings are one-dimensional arrays of type char
- String is terminated by char '\0' or null char.
- string constants are written between double
quotes, "a" and 'a' is a string and a char. They
are different - string constant like an array and is treated by a
pointer
55Strings
- for example
- a constant string can be treated as a pointer
char p"abc" printf("s s\n", p,p1)
"abc"1 ("abc"2)
56Strings
- Use pointer to access a string or use a char
array to deal with a string
char p "abcd", s'a','b','c','d'
s
p
a b c b d \0
a b c d \0
57 char s int nfrogs .....
/ get nfrogs from somewhere /
s (nfrogs 1) ? "" "s" printf("We found
d frogs in the pond.\n", nfrogs, s)
58/ Count the number of words in a string.
/ include ltctype.hgt int word_cnt(char s)
int cnt 0 while (s ! '\0')
while (isspace(s)) / skip white space
/ s if (s ! '\0')
/ found a word / cnt
while (!isspace(s) s ! '\0')
s / skip the word /
return cnt
59String-Handling Functions in the Standard Library
- There are many built-in functions in stand
library which can handle string manipulation - concatenates two string
char strcat (char s1, const char s2)
append s2 to s1
60String-Handling Functions in the Standard Library
int strcmp (const char s1, const char s2)
compare s1 and S2 If the return result is lt, ,
gt0, indicates s1 is less then, equal, greater
than s2 lexicographically.
61String-Handling Functions in the Standard Library
- copy a string to another
- account the number of characters before null char
char strcpy (char s1, const char s2)
copy characters of s2 into s1
size_t strlen(const s)
62unsigned strlen(const char s) register int
n for (n 0 s ! '\0' s) n
return n
char strcpy(char s1, register const char s2)
register char ps1 while(ps2) return
s1
63char strcat(char s1, const char s2)
register char p s1 while (p)
p while (p s2) return
s1
64Example
char s1"beatiful big sky country", s2"how now
brown cow" strlen(s1) 25 strlen(s28) 9 strc
mp(s1,s2) negative number
65Multidimensional Arrays
int a1000 int b27 int c532
- Array of array
- Two-dimensional array
int a23 row1 a00 a01 a02 row2
a10 a11 a12
66Multidimensional Arrays
aij
(aij) (ai))j ((ai))j) (a00
3ij
int a23 row1 a00 a01 a02 row2
a10 a11 a12
67Multidimensional Arrays
- The storage mapping function mapping between
pointer values and array indices is called
storage mapping function
int a35 aij (a005ij)
Therefore, You can use a one-dimensional array to
replace a two-dimensional array. int i, j,
b15, jSize5, iSize3 /
iSizejSize15)/ for (j0jltjSizej)
for (i0iltiSizei) (bjSizeij)3
68includeltstdio.hgt define BSIZE 15 define
ROWSIZE 3 define COLUMNSIZE 5 void
print2Darray(int , int, int, int) int
main() / jSize is the column size, jSize
is row size / int i, j, bBSIZE
for (i0iltROWSIZEi) for
(j0jltCOLUMNSIZEj)
(bCOLUMNSIZEij)i print2Darray(b,BSIZ
E,ROWSIZE, COLUMNSIZE) return 0
69 void print2Darray(int x, int n, int isize,
int jsize) int i,j for
(i0iltisizei) for
(j0jltjsizej) printf("d
",(xjsizeij)) printf("\n")
70Multidimensional Arrays
- Formal Parameter Declarations
int a5 int a35 int (a)5
int sum(int a5) int i, j, sum 0
for (i 0 i lt 3 i) for (j 0 j lt 5
j) sum aij return sum
71Multidimensional Arrays
- Formal Parameter Declarations
header of the function definition can have the
following parameter declarations int b int
b3 int b char argv char argv3 char
argv
72Multidimensional Arrays
int sum(int a92) int i, j, k, sum
0 for (i 0 i lt 7 i) for (j 0
j lt 9 j) for (k 0 k lt 2 k)
sum aijk return sum
int a792 mapping function can be
(a00092i2jk)
73Multidimensional Arrays
- Initialization of multidimensional arrays
int a231,2,3,4,5,6 int a231,2,3,4
,5,6 int a31,2,3,4,5,6 int
a223 1,1,0, 2,0,0, 3,0,0,
4,4,0 int a231,1,2,3,4,4
int a2230
74Multidimensional Arrays
- The Use of typedef
- self-defined equivalent data-type
define N 3 typerdef double scalar typedef scal
ar vectorN typedef scalar matrixNN or ty
pedef vector matrixN
75void add(vector x, vector y, vector z) int
i for (i0iltNi) xiyizi scalar
dot_product(vector x, vector y) int i
scalar sum0.0 for (i0iltNi)
sumxiyi return sum
76void multiply(matrix a, matrix b, matrix c)
int i,j,k for (i0iltNi) for
(j0jltNj) aij0.0 for
(k0kltNk) aijbik(ckj
77Arrays of Pointers
sort.h
include ltstdio.hgt include ltstdlib.hgt include
ltstring.hgt define MAXWORD 50 / max
word size / define N 300
/ array size of w / void
error_exit_calloc_failed(void) void
error_exit_too_many_words(void) void
error_exit_word_too_long(void) void
sort_words(char w, int n) void swap(char
p, char q) void wrt_words(char w, int
n)
78/ Sort words lexicographically. / include
"sort.h" int main(void) char
wordMAXWORD / work space / char
wN / an array of pointers /
int n / number of words to
be sorted / int i
main.c
79 for (i 0 scanf("s", word) 1 i)
if (i gt N) error_exit_too_many_words()
if (strlen(word) gt MAXWORD)
error_exit_word_too_long() wi
calloc(strlen(word) 1, sizeof(char)) if
(wi NULL) error_exit_calloc_failed()
strcpy(wi, word) n i
sort_words(w, n) / sort the words /
wrt_words(w, n) / write sorted list
of words / return 0
80include "sort.h" void wrt_words(char w, int
n) int i for (i 0 i lt n i)
printf("s\n", wi)
wrt.c
81include "sort.h" void error_exit_calloc_failed(v
oid) printf("s", "ERROR The call to
calloc() failed to\n" " allocate the
requested memory - bye!\n") exit(1) void
error_exit_too_many_words(void)
printf("ERROR At most d words can be sorted -
bye!\n", N) exit(1)
error.c
82 void error_exit_word_too_long(void)
printf("sds", "ERROR A word with more
than ", MAXWORD, "\n" " characters
was found - bye!\n") exit(1)
error.c
83include "sort.h" void sort_words(char w, int
n) / n elements are to be sorted / int
i, j for (i 0 i lt n i) for (j
i 1 j lt n j) if (strcmp(wi,
wj) gt 0) swap(wi, wj)
sort_words.c
84include "sort.h" void swap(char p, char
q) char tmp tmp p p q
q tmp
swap.c
a.outltinput
85input file
The College of Dentistry currently enrolls nearly
300 students in its D.D.S. program and is the
only provider of pre-doctoral dental education in
Iowa. Faculty include generalists and specialists
in every discipline of dentistry as well as
faculty who hold Ph.D.'s in biological or related
fields. Most clinical faculty are private
practitioners, w ho usually practice one day per
week. Students at Iowa benefit from a 3-to-1
ratio of dental students to full-time clinical
faculty. Teaching is the primary responsibility
of 96 full-time faculty.
863-to-1 300 96 College D.D.S. Dentistry Faculty Iow
a Iowa. Most Ph.D.'s Students Teaching The a and
and are as as at benefit biological clinical clini
cal currently day dental dental dentistry discipli
ne education enrolls
every faculty faculty faculty. faculty. fields. fr
om full-time full-time generalists hold in in in i
n include is
is its nearly of of of of of one only or per pract
ice practitioners, predoctoral primary private
program provider ratio related responsibility spec
ialists students students the the to usually week.
well who who
87Arguments to main()
- Command line execution
- passing information about execution condition or
parameters to program - use two arguments, char arrays, argc and argv
- argc is used to count total parameters including
command itself - argv is used to store the total parameters
including command itself and save in the first
element
88/ Echoing the command line arguments.
/ include ltstdio.hgt int main(int argc, char
argv) int i printf("argc d\n",
argc) for (i 0 i lt argc i)
printf("argvd s\n", i, argvi) return
0
a.out a is for apple argc 5 argv0
a.out argv1 a argv2 is argv3
for argv4 apple
89Ragged Arrays
- include ltstdio.hgt
- int main(void)
-
- char a215 "abc", "a is for apple"
- char p2 "abc", "a is for apple"
- printf("ccc s s\nccc s s\n",
- a00, a01, a02, a0, a1,
- p00, p01, p02, p0, p1)
- return 0
abc abc a is for apple abc abc a is for apple
90Ragged Arrays
- An array of pointers whose elements are used to
point arrays of varying size is called a ragged
array.
91Function as Arguments
- pointers to functions can be passed as arguments,
used in arrays, return from functions, and so
forth. For example
92include ltmath.hgt include ltstdio.hgt double
f(double) double sum_square(double
()(double), int, int)
sum_sqr.h
double sum_square(double f(double), int m, int
n) int k double sum 0.0
for (k m k lt n k) sum f(k)
f(k) return sum
sum_sqr.c
93double f(double x) return 1.0 / x
fct.c
include "sum_sqr.h" int main(void)
printf("s.7f\ns.7f\n", " First
computation ", sum_square(f, 1, 10000),
"Second computation ", sum_square(sin, 2, 13))
return 0
main.c
94 First computation 1.6448341 Second computation
5.7577885
95An ExampleUsing Bisection to find the root of a
function
include ltassert.hgt include ltstdio.hgt typedef
double dbl extern int cnt extern
const dbl eps / epsilon, a small
quantity / dbl bisection(dbl f(dbl x), dbl a,
dbl b) dbl f(dbl x)
96include "find_root.h" dbl bisection(dbl f(dbl
x), dbl a, dbl b) dbl m (a b) / 2.0
/ midpoint / cnt
/ of fct calls / if (f(m) 0.0
b - a lt eps) return m else if (f(a)
f(m) lt 0.0) return bisection(f, a, m)
else return bisection(f, m, b)
97include "find_root.h" dbl f(dbl x) return
(x x x x x - 7.0 x - 3.0)
98/ Find a root of f() by the bisection method.
/ include "find_root.h" int cnt
0 const dbl eps 1e-13 / epsilon,
a small quantity / int main(void) dbl a
-10.0 dbl b 10.0 dbl root
assert(f(a) f(b) lt 0.0) root
bisection(f, a, b) / recursive fct
call / printf("sd\ns .15f\ns .15f\n",
"No. of fct calls ", cnt, "Approximate
root ", root, " Function value ",
f(root)) return 0
99No. of fct calls 49 Approximate root
1.719628091484431 Function value
-0.000000000000977
100Kepler Function
101include ltassert.hgt include ltmath.hgt include
ltstdio.hgt typedef double dbl extern int
cnt extern const dbl eps /
epsilon, a small quantity / extern const dbl
e / a parameter in the Kepler eqn
/ extern const dbl m / a parameter
in the Kepler eqn / dbl bisection(dbl f(dbl
x), dbl a, dbl b) dbl kepler(dbl x)
102include "kepler.h" dbl bisection(dbl f(dbl x),
dbl a, dbl b) dbl m (a b) / 2.0
/ midpoint / cnt
/ of fct calls / if (f(m) 0.0
b - a lt eps) return m else if (f(a)
f(m) lt 0.0) return bisection(f, a, m)
else return bisection(f, m, b)
103include "kepler.h" dbl kepler(dbl x)
return (x - e sin(x) - m)
104/ Use bisection to solve the Kepler equation.
/ include "kepler.h" int cnt
0 const dbl eps 1e-15 / epsilon,
a small quantity / const dbl e 0.5
/ a parameter in the Kepler eqn / const dbl
m 2.2 / a parameter in the Kepler
eqn / int main(void) dbl a -100.0
dbl b 100.0 dbl root
105 assert(kepler(a) kepler(b) lt 0.0) root
bisection(kepler, a, b) / recursive fct
call / printf("sd\ns .15f\ns .15f\n",
"No. of fct calls ", cnt, "Approximate
root ", root, " Function value ",
kepler(root)) return 0
No. of fct calls 59 Approximate root
2.499454528163501 Function value
0.000000000000000
106Arrays of Pointers to Function
- In C function name is treated by the compiler as
a pointer to the function. - Example
107fint_roots.h
include ltassert.hgt include ltmath.hgt include
ltstdio.hgt define N 4 / size
of array of ptrs to fcts / typedef double
dbl / // Create the type "ptr to fct taking a
dbl and returning a dbl." / typedef dbl
(pfdd)(dbl)
108extern int cnt extern const dbl eps
/ epsilon, a small quantity / dbl
bisection(pfdd f, dbl a, dbl b) dbl f1(dbl
x) dbl f2(dbl x) dbl f3(dbl x)
109include "find_roots.h" dbl bisection(pfdd f,
dbl a, dbl b) dbl m (a b) / 2.0
/ midpoint / cnt
/ of fct calls / if (f(m) 0.0
b - a lt eps) return m else if (f(a)
f(m) lt 0.0) return bisection(f, a, m)
else return bisection(f, m, b)
bisection.c
110include "find_roots.h" dbl f1(dbl x)
return (xxx - xx 2.0x - 2.0) dbl f2(dbl
x) return (sin(x) - 0.7xxx 3.0) dbl
f3(dbl x) return (exp(0.13x) - xxx)
fct.c
111/ Use bisection to find roots. / include
"find_roots.h" int cnt 0 const dbl
eps 1e-13 / epsilon, a small
quantity / int main(void) int
begin_cnt int i int nfct_calls
dbl a -100.0 dbl b 100.0 dbl
root dbl val
main.c
112 pfdd fN NULL, f1, f2, f3 for (i
1 i lt N i) assert(fi(a) fi(b)
lt 0.0) begin_cnt cnt root
bisection(fi, a, b) nfct_calls cnt -
begin_cnt val fi(root)
printf("sds .15f\nsds .15f\ns3d\n\n",
"For f", i, "(x) an approximate root is
x0 ", root, " Fct evaluation at the
root f", i, "(x0) ", val, "
Number of fct calls to bisection() ",
nfct_calls) return 0
113Result
For f1(x) an approximate root is x0
1.000000000000023 Fct evaluation at the root
f1(x0) 0.000000000000069 Number of fct
calls to bisection() 52 For f2(x) an
approximate root is x0 1.784414278382185 Fct
evaluation at the root f2(x0)
0.000000000000169 Number of fct calls to
bisection() 52 For f3(x) an approximate root
is x0 1.046387173807117 Fct evaluation at the
root f3(x0) -0.000000000000134 Number of
fct calls to bisection() 52
114The Type Qualifiers const and volatile
- const is used to indicate the variable is a
constant
static const int k3 the value of k can not
changed const int n3 int vn / complains/
115const int a7 int pa / complains / const
int a7 const in pa / pointer to const
variable / int a int const pa / const
pointer to a variable / / the pointer can
not direct other variable except variable a
/ const int a-7 const int const pa /const
pointer points to the const variable a /
116The Type Qualifiers const and volatile
- It is seldom used.
- A volatile object is one that can be modified in
some unspecified way by the hardware.
extern cons volatile int real_time_clock