Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite.
Introduction to C Programming
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
One Dimensional Arrays
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Lecture 22: Arrays (cont). 2 Lecture Contents: t Searching in array: –linear search –binary search t Multidimensional arrays t Demo programs t Exercises.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 8: Arrays Problem Solving & Program Design in C Sixth Edition.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Chapter 10 Recursion Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Lab 11 rArrays rExercises Note: Read the whole Chapter 8.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
CS1101: Programming Methodology Aaron Tan.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
chap8 Chapter 8 Arrays (Hanly) chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Chapter 8 Arrays and Strings
Chapter 4 Selection Structures: if and switch Statements Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Introduction to C++ Programming Language Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University,
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
CP104 Introduction to Programming Array Lecture 25 __ 1 Josephus Problem In the Jewish revolt against Rome, Josephus and 39 of his comrades were holding.
Chapter 8 Arrays Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
Arrays Why we need data structure? Simple data types use a single memory cell to store a variable. Sometimes (for example scores of a class) it is more.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Chapter 8 Arrays Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Chapter 11 Structure and Union Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 7 Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 9 Strings J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Unit 6 Data Types and Arrays. Key Concepts Explicit and automatic conversion ASCII Enumerated types Function parameters Arrays Loops and arrays Passing.
Problem Solving and Program Design in C Chap. 7 Arrays Chow-Sing Lin.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.
Arrays.
Arrays and Records.
Chapter 8: Arrays Problem Solving and Program Design in C 5th Edition
Presentation transcript:

Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-2 Stacks A stack is a data structure in which only the top element can be accessed. pop remove the top element of a stack push insert a new element at the top of the stack

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-3 Stacks (Cont’d) Example –char s[STACK_SIZE]; /* a stack of characters */ –int s_top = -1; /* stack s is empty */ –max_size: STACK_SIZE –STACK_EMPTY: a previously defined constant macro

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-4 Figure 8.14 Functions push and pop

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-5 Figure 8.14 Functions push and pop (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-6 Stack Exercise Implement a program to output the input data in reverse order by stack. –For example, input abcde will output edcba

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-7 Searching an Array Target search Linear search

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-8 Linear Search Algorithm –Assume the target has not been found. –Start with the initial array element. –repeat while the target is not found and there are more array elements if the current element matches the target –Set a flag to indicate that the target has been found. else –Advance to the next array element. –if the target was found Return the target index as the search result. –else Return -1 as the search result.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-9 Figure 8.15 Function That Searches for a Target Value in an Array

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-10 Exercise How to find ALL elements which match the target?

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-11 Sorting an Array Selection sort ALGORITHM FOR SELECTION SORT –for each value of fill from 0 to n-2 Find index_of_min, the index of the smallest element in the unsorted subarray list[fill] through list[n-1]. if fill is not the position of the smallest element (index_of_min) –Exchange the smallest element with the one at position fill.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-12 Figure 8.16 Trace of Selection Sort

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-13 Figure 8.17 Functio n select_ sort

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-14 Exercise Implement the function get_min_range.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-15 Multidimensional Arrays Multidimensional array an array with two or more dimensions char tictac[3][3]; ]

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-16 Multidimensional Arrays (Cont’d) In the declaration of a multidimensional array function parameter, only the first dimension, the number of rows, can be omitted. –char tictac[ ][3]

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-17

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-18 Figure 8.19 Function to Check Whether Tic-tac-toe Board Is Filled

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-19 Exercise Implement a Tic-Tac-Toe game, allowing to users to play in turn. This program will judge which one is winner.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-20 Initialization of Multidimensional Arrays char tictac[3][3] = { {' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '} };

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-21 Arrays with Several Dimensions assume that the college offers 100 (MAXCRS) courses at five different campuses for each grade of students be aware of the amount of memory space required by each large array in a program

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-22 Figure 8.20 Three-Dimensional Array enroll

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-23 Arrays with Several Dimensions (Cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-24 Arrays with Several Dimensions (Cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-25 Exercise Print the number of enrolled students for each grade.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-26 Common Programming Errors subscript-range error. –int celsius[100]; –Reference to celsius[150] may cause an error message such as: access violation at line no. 28 –In many situations, however, no run-time error message will be produced –entirely the responsibility of the programmer

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-27 Common Programming Errors (Cont’d) Range for both the initial and the final values of the loop control variable. When using arrays as arguments to functions, be careful not to apply the address-of operator to just the array name. –Consider &celsius[10] and &celsius Use int z[ ] for array parameters to assist readers of your code.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-28 Figure 8.21 Sales Analysis Output

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-29 Figure 8.22 Sales Analysis Main Function

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-30 Figure 8.23 Function scan_table and Helper Function initialize

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-31 Figure 8.23 Function scan_table and Helper Function initialize (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-32 Figure 8.23 Function scan_table and Helper Function initialize (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-33 Figure 8.24 Function display_tabl e and Helper Function display_quar ter

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-34 Figure 8.24 Function display_table and Helper Function display_quarter (cont’d)