Lesson 12 Arrays 2007.11.7. 2 Objectives: About the Fibonacci sequence One-Dimensional Arrays Array Initialization Arrays as Function Arguments Case Study:

Slides:



Advertisements
Similar presentations
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Multiple-Subscripted Array
Chapter 9: Arrays and Strings
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
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
© 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.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
A First Book of ANSI C Fourth Edition Chapter 8 Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
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.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
Arrays. Arrays are objects that help us organize large amounts of information.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
 2005 Pearson Education, Inc. All rights reserved Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Introduction to programming in java Lecture 21 Arrays – Part 1.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Computer Programming BCT 1113
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
7 Arrays.
CNG 140 C Programming (Lecture set 8)
EKT150 : Computer Programming
Declaration, assignment & accessing
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays Arrays A few types Structures of related data items
Lecture 14: Problems with Lots of Similar Data
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

Lesson 12 Arrays

2 Objectives: About the Fibonacci sequence One-Dimensional Arrays Array Initialization Arrays as Function Arguments Case Study: Computing Averages and Standard Deviations Two-Dimensional Arrays

3 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 f n+1 = f n + f n-1 f 0 = 1 f 1 = 1

4 Fibonacci Numbers in Nature

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

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

7 Use iterative algorithm Liner complexity O(n)

8 Use the Golden Ratio: (1+ sqr(5))/2 F(n) = round ( pow (1+sqr(5)) / 2, n) / sqr(5) )

9 By recursive powering formula for multiplying two symmetric 2x2 matrices: [ F(n+1) + F(n) F(n) + F(n-1) ] [ F(n +1 ) F(n) ] [ F(n+2) F(n+1)] [ F(n+1) F(n) ]

10 By recursive powering (cont.) [ 1 1] [ 1 0] n [ 1 1] [ 1 0] n/2 [ 1 1] [ 1 0] n/2 * + [ 1 1] [ 1 0] logn

11 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

12 Name + space + access scheme 刘栩佳 樊怡 张昊 严霄 李伟强 叶翔 蔡浩 李鑫 李彬彬 安马太 宫照恒 uid list name list Arrays: Introduction

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

14 Create a one-dimensional array #define NUMELS 5 int grades[NUMELS]; Const expression

15 To access an element Format: arrayname [ index value ]  First element at position 0 position offset deviation grades[3] One-Dimensional Arrays

16 Use a subscripted variable Subscripted variables can be used anywhere scalar variables are valid  grades[0] = 98;  grades[1] = grades[0] - 11; Any expression that evaluates an integer may be used as a subscript #define NUMELS 5 total = 0; for (i = 0; i < NUMELS; i++) total = total + grades[i];

17 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++) { printf("Enter a grade: "); scanf("%d", &grades[i]); } Be careful: C does not check the value of the index being used

18 Sample output: Enter a grade: 85 Enter a grade: 90 Enter a grade: 78 Enter a grade: 75 Enter a grade: 92 grades 0 is 85 grades 1 is 90 grades 2 is 78 grades 3 is 75 grades 4 is 92 Input and Output of Array Values From temporal sequence To spatial sequence Next round Next element

19 Input and Output of Array Values

20 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 */

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

22 Pass array by reference

23 Pass array by reference

24 Find MAX: write proof inside the function

25 Case Study: Computing Averages and Standard Deviations n-1 Average = ∑ a[ i ] n i =0 Standard deviation = √ ∑(a[ i ] – Ave) 2 / n

26 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

27 Analyze the Problem Array of data Main control function Find the average ave Find the average ave std double findAvg( int [] ); double stdDev( int [], double) ?

28 Select an Overall Solution Problem-Solver Algorithm is adapted:  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

29 Computing Averages and Standard Deviations

30 Computing Averages and Standard Deviations

31 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];

32 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

33 Two-Dimensional Arrays (continued)

34 Two-Dimensional Arrays (exp)

35 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

36 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

37 homework : Program exercise: 8.2 4; 8.3 5; a Due time: 12/Nov.