SlideShare a Scribd company logo
A First Book of C++A First Book of C++
Chapter 7 (Pt 2)Chapter 7 (Pt 2)
ArraysArrays
∗ In this chapter, you will learn about:
∗ One-Dimensional Arrays
∗ Array Initialization
∗ Arrays as Arguments
∗ Two-Dimensional Arrays
∗ Common Programming Errors
∗ Searching and Sorting Methods
A First Book of C++ 4th Edition 2
Objectives
∗ Two-dimensional array (table): consists of both
rows and columns of elements
∗ Example: two-dimensional array of integers
col[0] col[1] col[2] col[3]
row[0] 8 16 9 5
row[1] 3 15 27 6
row[2] 14 25 2 10
∗ Array declaration: names the array val and
reserves storage for it
int val[2][2];
row colA First Book of C++ 4th Edition 3
Two-Dimensional Arrays
array val with 2 cols and 2 rows
row 0
row 1
col 0 col 1
∗ Locating array elements (Figure 7.7)
∗ val[1][3] uniquely identifies element in row 1, column 3
∗ Examples using elements of val array:
// assign value in row 2, col 3 into variable price
price = val[2][3];
val[0][0] = 62;//assign 62 to row 0, col 0
newnum = 4 * (val[1][0] - 5);
sumRow = val[0][0] + val[0][1] + val[0][2] +
val[0][3];
∗ The last statement adds the elements in row 0 and sum is
stored in sumRow
A First Book of C++ 4th Edition 4
Two-Dimensional Arrays (cont'd.)
Two-Dimensional Arrays (cont'd.)
A First Book of C++ 4th Edition 5
∗ Initialization: can be done within declaration
statements (similar to single-dimension arrays)
∗ Example:
int val[3][4] = { {8,16,9,52},
{3,15,27,6},
{14,25,2,10} };
∗ First set of internal braces contains values for row 0,
second set for row 1, and third set for row 2
∗ Commas in initialization braces are required; inner
braces can be omitted
A First Book of C++ 4th Edition 6
Two-Dimensional Arrays (cont'd.)
col 0 1 2 3
row
0
1
2
A First Book of C++ 4th Edition 7
Two-Dimensional Arrays (cont'd.)
A First Book of C++ 4th Edition 8
Two-Dimensional Arrays (cont'd.)
∗ Processing two-dimensional arrays:
∗ nested for loops are typically used
∗ Easy to cycle through each array element
∗ A pass through outer loop corresponds to a row
∗ A pass through inner loop corresponds to a column
∗ Nested for loop in Program 7.7 used to multiply each val
element by 10 and display results
∗ Output of Program 7.7
Display of multiplied elements
80 160 90 520
30 150 270 60
140 250 20 100
A First Book of C++ 4th Edition 9
Two-Dimensional Arrays (cont'd.)
A First Book of C++ 4th Edition 10
Two-Dimensional Arrays (cont'd.)
A First Book of C++ 4th Edition 11
Two-Dimensional Arrays (cont'd.)
∗ Prototypes for functions that pass two-dimensional
arrays can omit the row size of the array
∗ Example (Program 7.8): optional
display (int [ ][4]);
∗ Row size is optional, but column size is required
∗ Assuming 4 bytes for an int, the element val[1][3] is
located 28 bytes from the start of the array (i.e., offset)
A First Book of C++ 4th Edition 12
Two-Dimensional Arrays (cont'd.)
∗ Determining offset of an array (val [1][3])
∗ Computer uses row index, column index, and column
size to determine offset
A First Book of C++ 4th Edition 13
Two-Dimensional Arrays (cont'd.)
3+1
Two-Dimensional Arrays (cont'd.)
A First Book of C++ 4th Edition 14
4 bytes 4 bytes 4 bytes 4 bytes
4 bytes 4 bytes 4 bytes
∗ Arrays with more than two dimensions allowed in C++ but
not commonly used
∗ Example: int response[4][10][6]
∗ First element is response[0][0][0]
∗ Last element is response[3][9][5]
∗ A three-dimensional array can be viewed as a book of data
tables (Figure 7.10)
∗ First subscript (rank) is page number of table
∗ Second subscript is row in table
∗ Third subscript is desired column
A First Book of C++ 4th Edition 15
Larger Dimensional Arrays
Larger Dimensional Arrays
(cont'd.)
A First Book of C++ 4th Edition 16
∗ Forgetting to declare an array
∗ Results in a compiler error message equivalent to “invalid
indirection” each time a subscripted variable is
encountered within a program
∗ Using a subscript that references a nonexistent array
element (exceeding declared size/index)
∗ For example, declaring array to be of size 20 (e.g, int
val[20])and using a subscript value of 25 (e.g,
val[25])
∗ Not detected by most C++ compilers and will probably
cause a runtime error
A First Book of C++ 4th Edition 17
Common Programming Errors
∗ Not using a large enough counter value in a for loop
counter to cycle through all array elements
∗ Forgetting to initialize array elements
∗ Don’t assume compiler does this.
∗ Example:
val [8] = 60; // initialize or assign value to your array element
A First Book of C++ 4th Edition 18
Common Programming Errors
(cont'd.)
∗ One-dimensional array: a data structure that stores a
list of values of same data type
∗ Must specify data type and array size
∗ Example:
int num[100]; //creates an array of 100 integers
∗ Array elements are stored in contiguous locations (in
sequence) in memory and referenced using the array
name and a subscript
∗ Example: values[4]
∗ means access the 5th
element of the
values array at index 4
A First Book of C++ 4th Edition 19
Summary
∗ Two-dimensional array is declared by listing both a
row and column size with data type and name of
array
∗ Arrays may be initialized when they are declared
∗ For two-dimensional arrays, you list the initial values, in
a row-by-row manner, within braces and separating
them with commas
∗ Arrays are passed to a function by passing name of
array as an argument (to be discussed in next class)
A First Book of C++ 4th Edition 20
Summary (cont'd.)
∗ Most programmers encounter the need to both sort
and search a list of data items at some time in their
programming careers
A First Book of C++ 4th Edition 21
Chapter Supplement: Searching
and Sorting Methods
∗ Linear (sequential) search
∗ Each item in the list is examined in the order in which it
occurs until the desired item is found or the end of the
list is reached
∗ List doesn’t have to be in sorted order to perform the
search
∗ Binary search
∗ Starting with an ordered list, the desired item is first
compared with the element in the middle of the list
∗ If item is not found, you continue the search on either
the first or second half of the list
A First Book of C++ 4th Edition 22
Search Algorithms

More Related Content

PDF
5 chapter3 list_stackqueuepart2
PPTX
The Stack And Recursion
PPTX
Stack and Queue
PPTX
Programming in c Arrays
PPT
Array Presentation
PPT
PPTX
Review of basic data structures
PPTX
Stacks and Queue - Data Structures
5 chapter3 list_stackqueuepart2
The Stack And Recursion
Stack and Queue
Programming in c Arrays
Array Presentation
Review of basic data structures
Stacks and Queue - Data Structures

What's hot (20)

PDF
Queues-handouts
PPT
Data structure lecture7
PPTX
Array ppt
PPTX
Arrays in C language
PPTX
PPTX
Stack - Data Structure - Notes
PPTX
My lecture stack_queue_operation
PPTX
Array Introduction One-dimensional array Multidimensional array
PPT
03 stacks and_queues_using_arrays
PPT
Stack and queue
PDF
stacks and queues
PPT
Array Presentation (EngineerBaBu.com)
PPT
Stacks and queue
PPTX
C++ lecture 04
PPTX
Queue - Data Structure - Notes
PPTX
Stacks in c++
PPTX
A Presentation About Array Manipulation(Insertion & Deletion in an array)
PPT
C programming , array 2020
PPTX
Arrays In C++
Queues-handouts
Data structure lecture7
Array ppt
Arrays in C language
Stack - Data Structure - Notes
My lecture stack_queue_operation
Array Introduction One-dimensional array Multidimensional array
03 stacks and_queues_using_arrays
Stack and queue
stacks and queues
Array Presentation (EngineerBaBu.com)
Stacks and queue
C++ lecture 04
Queue - Data Structure - Notes
Stacks in c++
A Presentation About Array Manipulation(Insertion & Deletion in an array)
C programming , array 2020
Arrays In C++
Ad

Viewers also liked (13)

PDF
Daniel Witherite UT-UXO Certificate
PPTX
Growth Hacking with Kentico
PPTX
El informe de investigación
PDF
Casa design 2014
PPTX
Sistemas de Información en la Empresa
DOC
11review(inheritance andpolymorphism)
PDF
Mediterranean – Adriatic Underwater Cultural Heritage links
PDF
YALI_Certificate (2)
PDF
Apostila de empilhadeira rv1
PPTX
PRIMAVERA - A Nova Fiscalidade Nacional em Angola
PPT
Oportunidad Negocio (2 )
PDF
Startup Braga - value proposition workshop
PDF
Brochure Seminario 23 de mayo
Daniel Witherite UT-UXO Certificate
Growth Hacking with Kentico
El informe de investigación
Casa design 2014
Sistemas de Información en la Empresa
11review(inheritance andpolymorphism)
Mediterranean – Adriatic Underwater Cultural Heritage links
YALI_Certificate (2)
Apostila de empilhadeira rv1
PRIMAVERA - A Nova Fiscalidade Nacional em Angola
Oportunidad Negocio (2 )
Startup Braga - value proposition workshop
Brochure Seminario 23 de mayo
Ad

Similar to Csc1100 lecture09 ch07_pt2 (20)

PPT
Csc1100 lecture07 ch07_pt1-1
PPT
19-Lec - Multidimensional Arrays.ppt
PDF
ARRAYS
PDF
4.ArraysInC.pdf
PPT
Csc1100 lecture12 ch08_pt2
PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
PPT
Arrays and vectors in Data Structure.ppt
PDF
Introduction to Arrays in C
PPT
Algo>Arrays
PDF
Unit ii data structure-converted
PPTX
Unit4pptx__2024_11_ 11_10_16_09.pptx
PDF
Arrays and strings in c++
PPTX
object oriented programing in python and pip
PDF
Unit 2
PDF
Functions, Strings ,Storage classes in C
PPTX
Array In C++ programming object oriented programming
PPT
Array 31.8.2020 updated
Csc1100 lecture07 ch07_pt1-1
19-Lec - Multidimensional Arrays.ppt
ARRAYS
4.ArraysInC.pdf
Csc1100 lecture12 ch08_pt2
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Arrays and vectors in Data Structure.ppt
Introduction to Arrays in C
Algo>Arrays
Unit ii data structure-converted
Unit4pptx__2024_11_ 11_10_16_09.pptx
Arrays and strings in c++
object oriented programing in python and pip
Unit 2
Functions, Strings ,Storage classes in C
Array In C++ programming object oriented programming
Array 31.8.2020 updated

More from IIUM (20)

PDF
How to use_000webhost
PDF
Chapter 2
PDF
Chapter 1
PDF
Kreydle internship-multimedia
PDF
03phpbldgblock
PDF
Chap2 practice key
PDF
Group p1
PDF
Tutorial import n auto pilot blogspot friendly seo
PDF
Visual sceneperception encycloperception-sage-oliva2009
PDF
03 the htm_lforms
PDF
Exercise on algo analysis answer
PDF
Redo midterm
PDF
Heaps
PDF
Report format
PDF
Edpuzzle guidelines
PDF
Final Exam Paper
PDF
Final Exam Paper
PDF
Group assignment 1 s21516
PDF
Avl tree-rotations
PDF
Week12 graph
How to use_000webhost
Chapter 2
Chapter 1
Kreydle internship-multimedia
03phpbldgblock
Chap2 practice key
Group p1
Tutorial import n auto pilot blogspot friendly seo
Visual sceneperception encycloperception-sage-oliva2009
03 the htm_lforms
Exercise on algo analysis answer
Redo midterm
Heaps
Report format
Edpuzzle guidelines
Final Exam Paper
Final Exam Paper
Group assignment 1 s21516
Avl tree-rotations
Week12 graph

Csc1100 lecture09 ch07_pt2

  • 1. A First Book of C++A First Book of C++ Chapter 7 (Pt 2)Chapter 7 (Pt 2) ArraysArrays
  • 2. ∗ In this chapter, you will learn about: ∗ One-Dimensional Arrays ∗ Array Initialization ∗ Arrays as Arguments ∗ Two-Dimensional Arrays ∗ Common Programming Errors ∗ Searching and Sorting Methods A First Book of C++ 4th Edition 2 Objectives
  • 3. ∗ Two-dimensional array (table): consists of both rows and columns of elements ∗ Example: two-dimensional array of integers col[0] col[1] col[2] col[3] row[0] 8 16 9 5 row[1] 3 15 27 6 row[2] 14 25 2 10 ∗ Array declaration: names the array val and reserves storage for it int val[2][2]; row colA First Book of C++ 4th Edition 3 Two-Dimensional Arrays array val with 2 cols and 2 rows row 0 row 1 col 0 col 1
  • 4. ∗ Locating array elements (Figure 7.7) ∗ val[1][3] uniquely identifies element in row 1, column 3 ∗ Examples using elements of val array: // assign value in row 2, col 3 into variable price price = val[2][3]; val[0][0] = 62;//assign 62 to row 0, col 0 newnum = 4 * (val[1][0] - 5); sumRow = val[0][0] + val[0][1] + val[0][2] + val[0][3]; ∗ The last statement adds the elements in row 0 and sum is stored in sumRow A First Book of C++ 4th Edition 4 Two-Dimensional Arrays (cont'd.)
  • 5. Two-Dimensional Arrays (cont'd.) A First Book of C++ 4th Edition 5
  • 6. ∗ Initialization: can be done within declaration statements (similar to single-dimension arrays) ∗ Example: int val[3][4] = { {8,16,9,52}, {3,15,27,6}, {14,25,2,10} }; ∗ First set of internal braces contains values for row 0, second set for row 1, and third set for row 2 ∗ Commas in initialization braces are required; inner braces can be omitted A First Book of C++ 4th Edition 6 Two-Dimensional Arrays (cont'd.) col 0 1 2 3 row 0 1 2
  • 7. A First Book of C++ 4th Edition 7 Two-Dimensional Arrays (cont'd.)
  • 8. A First Book of C++ 4th Edition 8 Two-Dimensional Arrays (cont'd.)
  • 9. ∗ Processing two-dimensional arrays: ∗ nested for loops are typically used ∗ Easy to cycle through each array element ∗ A pass through outer loop corresponds to a row ∗ A pass through inner loop corresponds to a column ∗ Nested for loop in Program 7.7 used to multiply each val element by 10 and display results ∗ Output of Program 7.7 Display of multiplied elements 80 160 90 520 30 150 270 60 140 250 20 100 A First Book of C++ 4th Edition 9 Two-Dimensional Arrays (cont'd.)
  • 10. A First Book of C++ 4th Edition 10 Two-Dimensional Arrays (cont'd.)
  • 11. A First Book of C++ 4th Edition 11 Two-Dimensional Arrays (cont'd.)
  • 12. ∗ Prototypes for functions that pass two-dimensional arrays can omit the row size of the array ∗ Example (Program 7.8): optional display (int [ ][4]); ∗ Row size is optional, but column size is required ∗ Assuming 4 bytes for an int, the element val[1][3] is located 28 bytes from the start of the array (i.e., offset) A First Book of C++ 4th Edition 12 Two-Dimensional Arrays (cont'd.)
  • 13. ∗ Determining offset of an array (val [1][3]) ∗ Computer uses row index, column index, and column size to determine offset A First Book of C++ 4th Edition 13 Two-Dimensional Arrays (cont'd.) 3+1
  • 14. Two-Dimensional Arrays (cont'd.) A First Book of C++ 4th Edition 14 4 bytes 4 bytes 4 bytes 4 bytes 4 bytes 4 bytes 4 bytes
  • 15. ∗ Arrays with more than two dimensions allowed in C++ but not commonly used ∗ Example: int response[4][10][6] ∗ First element is response[0][0][0] ∗ Last element is response[3][9][5] ∗ A three-dimensional array can be viewed as a book of data tables (Figure 7.10) ∗ First subscript (rank) is page number of table ∗ Second subscript is row in table ∗ Third subscript is desired column A First Book of C++ 4th Edition 15 Larger Dimensional Arrays
  • 16. Larger Dimensional Arrays (cont'd.) A First Book of C++ 4th Edition 16
  • 17. ∗ Forgetting to declare an array ∗ Results in a compiler error message equivalent to “invalid indirection” each time a subscripted variable is encountered within a program ∗ Using a subscript that references a nonexistent array element (exceeding declared size/index) ∗ For example, declaring array to be of size 20 (e.g, int val[20])and using a subscript value of 25 (e.g, val[25]) ∗ Not detected by most C++ compilers and will probably cause a runtime error A First Book of C++ 4th Edition 17 Common Programming Errors
  • 18. ∗ Not using a large enough counter value in a for loop counter to cycle through all array elements ∗ Forgetting to initialize array elements ∗ Don’t assume compiler does this. ∗ Example: val [8] = 60; // initialize or assign value to your array element A First Book of C++ 4th Edition 18 Common Programming Errors (cont'd.)
  • 19. ∗ One-dimensional array: a data structure that stores a list of values of same data type ∗ Must specify data type and array size ∗ Example: int num[100]; //creates an array of 100 integers ∗ Array elements are stored in contiguous locations (in sequence) in memory and referenced using the array name and a subscript ∗ Example: values[4] ∗ means access the 5th element of the values array at index 4 A First Book of C++ 4th Edition 19 Summary
  • 20. ∗ Two-dimensional array is declared by listing both a row and column size with data type and name of array ∗ Arrays may be initialized when they are declared ∗ For two-dimensional arrays, you list the initial values, in a row-by-row manner, within braces and separating them with commas ∗ Arrays are passed to a function by passing name of array as an argument (to be discussed in next class) A First Book of C++ 4th Edition 20 Summary (cont'd.)
  • 21. ∗ Most programmers encounter the need to both sort and search a list of data items at some time in their programming careers A First Book of C++ 4th Edition 21 Chapter Supplement: Searching and Sorting Methods
  • 22. ∗ Linear (sequential) search ∗ Each item in the list is examined in the order in which it occurs until the desired item is found or the end of the list is reached ∗ List doesn’t have to be in sorted order to perform the search ∗ Binary search ∗ Starting with an ordered list, the desired item is first compared with the element in the middle of the list ∗ If item is not found, you continue the search on either the first or second half of the list A First Book of C++ 4th Edition 22 Search Algorithms