Your Federal Quarterly Tax Payments are due April 15th

# Pointer and array by nikeborome

VIEWS: 16 PAGES: 37

• pg 1
```									Lesson 12 Arrays

2007.11.7
Objectives:
• One-Dimensional Arrays
• Array Initialization
• Arrays as Function Arguments
• Case Study: Computing Averages and Standard
Deviations
• Two-Dimensional Arrays

2
Fibonacci Numbers
   One pair rabbit give birth
to a new pair baby rabbits
each year
   The baby rabbits need to
wait one year to bear new
baby
      fn+1 = fn + fn-1
f0 = 1
f1 = 1

3
Fibonacci Numbers in Nature
1
8

2

13
3

21
5

4
Compute the Fibonacci numbers
recursively
int fib(int n)
{
if (n <= 2)
return 1;
else
return fib(n-1) + fib(n-2);
}

5
Time complexity of the recursive
algorithm
fib(5)
   The growth rate of
+
the computing time
fib(3)                     fib(4)
with the problem size
+                           +
fib(1)       fib(2)         fib(2)       fib(3)
exponential                                      …
+                           +
fib(0)        fib(1)        fib(1)        fib(2)

6
Use iterative algorithm

Liner complexity
O(n)

7
Use the Golden Ratio: (1+ sqr(5))/2

   F(n) = round (
pow (1+sqr(5)) / 2, n) / sqr(5)
)

8
By recursive powering

formula for multiplying two symmetric 2x2 matrices:

[ F(n+1) + F(n)   F(n) + F(n-1) ]         [ F(n+2)   F(n+1)]
[ F(n +1 )         F(n)         ]         [ F(n+1)   F(n) ]

9
By recursive powering (cont.)
n
[ 1 1]
[ 1 0]

logn                    *

n/2                     n/2       [ 1 1]
[ 1 1]                  [ 1 1]         +
[ 1 0]                  [ 1 0]             [ 1 0]

10
Arrays: Introduction
   Atomic variable: variable whose value cannot be further
subdivided into a built-in data type
   Aggregate data type: data type with two main
characteristics
   Its values can be decomposed into individual data elements,
each of which is either atomic or another Aggregate data type
   It provides an access scheme for locating individual data
elements within the data structure

11
Arrays: Introduction

Name + space + access scheme

uid list   00611001   name list   刘栩佳
00611002               樊怡
00611003               张昊
00611004               严霄
00611005               李伟强
00611006               叶翔
00611007               蔡浩
00611008               李鑫
00611009               李彬彬
00611010               安马太
00611011               宫照恒

12
Arrays: Introduction

Arrays
   A set of data items with same type
   Stored in consecutive memory
   Fixed in size throughout program
—— not dynamic
index

13
Create a one-dimensional array

Const expression
#define NUMELS 5

14
One-Dimensional Arrays

To access an element
   Format: arrayname[ index value ]
 First   element at position 0
position
deviation

15
Use a subscripted variable
   Subscripted variables can be used anywhere
scalar variables are valid
   Any expression that evaluates an integer may be
used as a subscript
#define NUMELS 5
total = 0;
for (i = 0; i < NUMELS; i++)

16
Input and Output of Array Values
   Array elements can be assigned values by:
    using assignment statements
   interactively, using the scanf() function
#define NUMELS 5
for(i = 0; i < NUMELS; i++)
{
}
   Be careful: C does not check the value of the index
being used

17
Input and Output of Array Values

Sample output:
Enter temporal sequence
To spatial sequence

18
Input and Output of Array Values

19
Array Initialization
   Examples of initializations:
 int   grades[5] = {98, 87, 92, 79, 85};
 double     length[7] = {8.8, 6.4, 4.9};
 char    codes[5]= {‘h', ‘e', ‘l', ‘l', ‘o'};
 char    codes[] = {‘h', ‘e', ‘l', ‘l', ‘o'};
 char    codes[] = "sample"; /* size is 7 */

20
Passing an array to a function
 By reference
 Via pointer (left behind)

21
Pass array
by reference

22
Pass array
by reference

23
Find MAX:

write proof inside
the function

24
Case Study: Computing Averages and
Standard Deviations

n-1
   Average = ∑ a[ i ]   n
i =0

   Standard deviation = √ ∑(a[ i ] – Ave)2 ／n

25
Requirements Specification
   Need two functions
 Determine the average…
 Determine the standard deviation…
…of a list of integer numbers
   Each function must accept the numbers as an array and
return the calculated values to the calling function
   We now apply the top-down development procedure to
developing the required functions

26
Analyze the Problem

Array of data               Main control function

ave
std
ave

Find the average                     Find the average

double findAvg( int [] );          double stdDev( int [], double)

?

27
Select an Overall Solution
 Initialize
an array of integers
 Call the average function
 Call the standard deviation function
 Display the returned value of the average
function
 Display the returned value of the standard
deviation function

28
Computing Averages and
Standard Deviations

29
Computing Averages and
Standard Deviations

30
Two-Dimensional Arrays
   A two-dimensional array, or table, consists of
both rows and columns of elements
   Can be treated as array of arrays
int ar[3][4];

31
Two-Dimensional Arrays (continued)
   Initialization:
#define NUMROWS 3
#define NUMCOLS 4
int val[NUMROWS][NUMCOLS] = { {8,16,9,52},
{3,15,27,6},
{14,25,2,10} };
   The inner braces can be omitted:
int val[NUMROWS][NUMCOLS] =
{8,16,9,52,3,15,27, 6,14,25,2,10};
   Initialization is done in row order

32
Two-Dimensional Arrays (continued)

33
Two-Dimensional Arrays (exp)

34
Common Programming Errors
   Forgetting to declare the array
   Using a subscript that references a nonexistent
array element
   Not using a large enough conditional value in a
for loop counter to cycle through all the array
elements
   Forgetting to initialize the array

35
Summary
   A single-dimensional array is a data structure that can
store a list of values of the same data type
   Elements are stored in contiguous locations
 Referenced using the array name and a subscript
   Single-dimensional arrays may be initialized when they
are declared
   Single-dimensional arrays are passed to a function by
passing the name of the array as an argument

36
homework：
   Program exercise:
8.2 4; 8.3 5; 8.4 11a
Due time: 12/Nov.

37

```
To top