CSCI101 An Introduction to Programming using C - PowerPoint PPT Presentation

1 / 88
About This Presentation
Title:

CSCI101 An Introduction to Programming using C

Description:

An array variable is a special kind of object that can take many values - all of ... int result1; int result2; int result3; and so on . . . 11/8/09. Anne Dawson c 2006 ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 89
Provided by: drahd
Category:

less

Transcript and Presenter's Notes

Title: CSCI101 An Introduction to Programming using C


1
CSCI101 An Introduction to Programming using
C
2
  • Arrays

3
Arrays
  • 1 Introduction to Arrays
  • 2 Arrays in Functions
  • 3 Programming with Arrays

4
Arrays
  • 1 Introduction to Arrays
  • 2 Arrays in Functions
  • 3 Programming with Arrays

5
  • A variable (or simple variable) can take a
    single value.
  • An array variable is a special kind of object
    that can take many values - all of the same type.

6
  • Say you want to store the exam results of 30
    students. You could create 30 variables
  • int result1
  • int result2
  • int result3 and so on . . .

7
  • If you only had a few results to score, this
    method would be acceptable. But what if you had
    to store the results of each student in the
    college, not just one class?

8
  • Storing 2000 results in this way would be a
    very tedious and error-prone process. You would
    have to type 2000 declaration statements and 2000
    assignments.

9
  • Fortunately, by using an array variable, we
    get around this problem . . .

10
What is an array variable?
  • First consider a simple variable, the type we
    have used do far
  • int result

11
  • After this statement C reserves an area of
    memory which is referred to by the name result
    which is big enough to hold one integer number,
    for example the number 90. Imagine this area of
    memory as a mail box

  • result

90
12
An Array Variable
  • Consider an array variable as a line of these
    boxes, each box is referred to by a number
  • 0 1 2 3 4 5
    6 7
  • result

13
An Array Variable
  • The line of boxes is known as an array
    variable. An array variable is given a name,
    just like the simple variable, for example
    result, and each box is referred to by its
    number result0, result1 and so on...
  • 0 1 2 3 4 5
    6 7
  • result

14
Important
  • The number of the box is known as the
    subscript or index of the array element (box).
    The variables that make up the array are known as
    indexed variables, subscripted variables or
    elements.

15
Important
  • Note In C the index of an array always
    starts at 0.

16
Declaring an Array Variable
  • int result8
  • The number 8 is said to be the size of the
    array. int is said to be the base type of the
    array. The base type can be any type, in
    particular it can be a class type, i.e. an object.

17
Declaring an Array Variable
  • int result8
  • // note use of square brackets
  • // after declaring an array, until you assign
    values to the elements, the elements contain
    garbage (denoted by )
  • 0 1 2 3 4
    5 6 7
  • result









18
Assigning Values to Arrays
  • result0 75
  • result1 90
  • result4 72
  • 0 1 2 3 4 5
    6 7

75
90



72


19
Assigning Values to Arrays at Declaration
  • int result8 75,90,0,0,72,0,0,0
  • int result 75,90,0,0,72,0,0,0
  • 0 1 2 3 4 5
    6 7

75
90
0
0
0
72
0
0
20
Initializing Arrays
  • int children3 2, 12, 1
  • The above declaration is equivalent to the
    following code
  • int children3
  • children0 2
  • children1 12
  • children2 1

21
Initializing an Array
  • int b 5, 12, 11
  • is equivalent to
  • int b3 5, 12, 11

22
How big are the Elements?
  • An element is one of the boxes making up the
    array. The size of the box in bytes depends on
    the data type of what you put in there. . .

23
An Array Variable
  • Consider an array variable as a line of these
    boxes. The boxes are all the same size.
  • 0 1 2 3 4 5
    6 7
  • result

24
Data Types
  • Each data type takes up a fixed amount of
    memory. . .

25
  • If you declare result as an array of
    integers, each element occupies 4 bytes of
    memory.
  • If you declare result as an array of double,
    then each element occupies 8 bytes of memory

26
Integer Data Types
  • Type Name Memory
  • short (int) 2 bytes
  • int 4 bytes
  • long (int) 8 bytes

27
Floating point Data Types
  • Type Name Memory
  • float 4 bytes
  • double 8 bytes
  • long double 10 bytes

28
A character
  • A character occupies a single byte, and its
    byte value is the decimal value given by its
    ASCII code.
  • A is decimal 65 or binary 01000001

29
A Program Using an Array array-01.cpp
  • http//www.annedawson.com/array-01.cpp

30
Indexed variable as an argumentarray-03.cpp
  • http//www.annedawson.com/array-03.cpp

31
adjust_days(vacationn)
32
  • for (n 0 n lt NUMBER_OF_EMPLOYEES n)
  • vacationn adjust_days(vacationn)
  • //
  • int adjust_days(int old_days)
  • return (old_days 5)

1.
2.
33
  • An entire array can be passed to a function. .
    .

34
Arrays
  • 1 Introduction to Arrays
  • 2 Arrays in Functions
  • 3 Programming with Arrays

35
  • The name of any array is the address of its
    first element.

36
Array Parameters
  • A formal parameter for an entire array is
    neither call-by-value or call-by-reference. It
    is a new kind of formal parameter referred to as
    an array parameter.

37
The formal parameter (in the function header) for
an entire array is, for exampleint arrayname
38
An example function headervoid fill_up(int
a, int size)
39
  • void fill_up(int a, int size) // prototype
  • void fill_up(int a, int size) // definition
  • cout ltlt "Enter " ltlt size ltlt "numbers\n"
  • for (int i 0 i lt size i)
  • cin gtgt ai
  • size-- // reduces size by 1
  • cout ltlt "The last array index used is "
  • ltlt size ltlt endl

40
  • An example function call
  • int score5
  • int number_of_scores 5
  • fill_up(score, number_of_scores)
  • the function call array integer

41
  • An array parameter behaves very much like a
    call-by-reference parameter. This is because,
    when you send an entire array to a function, you
    pass across to the function the array name. The
    name of any array is the address of its first
    element.

42
  • An array parameter behaves very much like a
    call-by-reference parameter. This is because,
    when you send an entire array to a function, you
    pass across to the function the array name. The
    name of any array is the address of its first
    element.

43
Using an entire array as a parameter to a function
  • You are actually passing the address of the
    array variable (the start address). . .

44
  • Changes made to an array passed to a method
    are permanent changes, as the function is working
    on the actual array, not on a copy.

45
  • Call-by-reference parameters also work by
    passing the address of the variable, rather than
    a copy of the value of the variable as in
    call-by-value. Think of the word 'reference' as
    'referring to an address'.

46
  • Call-by-reference parameters also work by
    passing the address of the variable, rather than
    a copy of the value of the variable as in
    call-by-value. Think of the word 'reference' as
    'referring to an address'.

47
Why is an array parameter different to a
call-by-reference parameter?
  • Call-by-reference is for simple variables
    (int, char etc), and the compiler knows the size
    (in bytes) of all the simple variables, as the
    size is fixed for each data type.

48
Integer Data Types
  • Type Name Memory
  • short (int) 2 bytes
  • int 4 bytes
  • long (int) 8 bytes

49
Floating point Data Types
  • Type Name Memory
  • float 4 bytes
  • double 8 bytes
  • long double 10 bytes

50
Using a simple variable as a call-by-reference
parameter to a function
  • You are actually passing the address of the
    variable when you use call-by-reference. And the
    compiler 'knows' the size in bytes of the
    variable, because you also have to specify the
    data type in the prototype.

51
Using an entire array as a parameter to a function
  • Again, you are actually passing the address of
    the array variable (the start address). . .
  • but the compiler does not know the size in
    bytes of the entire array, only the size in bytes
    of a single element.

52
When you pass an entire array to a function
  • The compiler does not know the size in bytes
    of the entire array, only the size in bytes of a
    single element.
  • This is why when you pass an array to a
    function, you should also pass an integer which
    is the number of elements in the array.

53
  • int score5
  • int number_of_scores 5
  • fill_up(score, number_of_scores)
  • the function call array array size

54
  • Changes made to an array passed to a function
    are permanent changes, as the function is working
    on the actual array, not on a copy.

55
The const parameter modifier
  • If you don't want a function to change an
    array passed to it you should then pass the array
    using the const parameter modifier. The array
    parameter is then known as a constant array
    parameter

funct1(const int a,int size_of_a) // the
function prototype
56
The const parameter modifier
  • . . . can be used with any type of parameter,
    but it is usually used only with array parameters
    and call-by-reference parameters for classes.

57
Arrays
  • 1 Introduction to Arrays
  • 2 Arrays in Functions
  • 3 Programming with Arrays

58
Programming with Arrays
  • Most programs involving arrays commonly sort and
    search the elements of the array. . .

59
Sorting an Array
  • Sorting an array is the process of converting
    an unordered array into an ordered array.

60
  • An unordered numerical array
  • 0 1 2 3 4 5 6
    7
  • An ordered numerical array
  • 0 1 2 3 4 5 6
    7

34
21
43
12
99
72
32
2
2
12
21
32
99
34
43
72
61
  • An unordered character array
  • 0 1 2 3 4 5 6
    7
  • An ordered character array
  • 0 1 2 3 4 5 6
    7

'Z'
'B'
'O'
'Y'
'C'
'P'
'A'
'L'
'A'
'B'
'C'
'L'
'Z'
'O'
'P'
'Y'
62
  • Searching Arrays....

63
Searching an Array
  • A very common task is to search an array for a
    given value.
  • For example, an array contains student numbers
    for students enrolled in a course. . .
  • you may want to search the array to see if a
    particular student number is contained in it.

64
Linear Search Animation
  • http//www.cosc.canterbury.ac.nz/people/mukundan/d
    sal/LSearch.html

65
Searching an Array search01.cpp
  • http//www.annedawson.com/
  • search01.cpp
  • search02.cpp
  • search03.cpp

66
Searching an Array array-10.cpp
  • http//www.annedawson.com/
  • array-10.cpp

67
Sorting Arrays. . .
68
Sorting an Array sort01.cpp
  • http//www.annedawson.com/sort01.cpp

69
Sorting an Array
  • is the process of converting an unordered
    array into an ordered array.

70
Sorting an Array of Integers
  • The picture shows an array of six integers that
    we want to sort from smallest to largest

0 1 2 3
4 5
71
The Selectionsort Algorithm
  • Start by finding the smallest element.

0 1 2 3
4 5
72
The Selectionsort Algorithm
  • Start by finding the smallest element.
  • Swap the smallest element with the first element.

0 1 2 3
4 5
73
The Selectionsort Algorithm
  • Start by finding the smallest element.
  • Swap the smallest element with the first element.

0 1 2 3
4 5
74
The Selectionsort Algorithm
Sorted side
Unsorted side
  • Part of the array is now sorted.

0 1 2 3
4 5
75
The Selectionsort Algorithm
Sorted side
Unsorted side
  • Find the smallest element in the unsorted side.

0 1 2 3
4 5
76
The Selectionsort Algorithm
Sorted side
Unsorted side
  • Find the smallest element in the unsorted side.
  • Swap with the front of the unsorted side.

0 1 2 3
4 5
77
The Selectionsort Algorithm
Sorted side
Unsorted side
  • We have increased the size of the sorted side by
    one element.

0 1 2 3
4 5
78
The Selectionsort Algorithm
Sorted side
Unsorted side
Smallest from unsorted
  • The process continues...

0 1 2 3
4 5
79
The Selectionsort Algorithm
Sorted side
Unsorted side
Swap with front
  • The process continues...

0 1 2 3
4 5
80
The Selectionsort Algorithm
Sorted side is bigger
Sorted side
Unsorted side
  • The process continues...

0 1 2 3
4 5
81
The Selectionsort Algorithm
Sorted side
Unsorted side
  • The process keeps adding one more number to the
    sorted side.
  • The sorted side has the smallest numbers,
    arranged from small to large.

0 1 2 3
4 5
82
The Selectionsort Algorithm
  • We can stop when the unsorted side has just one
    number, since that number must be the largest
    number.

0 1 2 3
4 5
83
The Selectionsort Algorithm
  • The array is now sorted.
  • We repeatedly selected the smallest element, and
    moved this element to the front of the unsorted
    side.

0 1 2 3
4 5
84
Algorithm for a Selection Sort
  • see textbook, and
  • http//www.cosc.canterbury.ac.nz/people/mukundan/d
    sal/SSort.html

85
The Selection Sort array-12.cpp
  • http//www.annedawson.com/array-12.cpp

86
Homework
  • Read the chapter on Arrays - the first three
    sections only.

87
End of CPP_Arrays.ppt
88
  • Last updated Friday 24th March 2006, 1807 PT,
    AHD
Write a Comment
User Comments (0)
About PowerShow.com