Chapter 8 Arrays Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 7: Arrays In this chapter, you will learn about
The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and.
Programming and Data Structure
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
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.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Chapter 9: Arrays and Strings
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 8 Arrays and Strings
 2007 Pearson Education, Inc. All rights reserved C Arrays.
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 2 Overview of C Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
 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 8 Arrays and Strings
Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of.
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.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Chapter 8 Arrays Instructor: Yuksel & Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-2 What is an Array? Scalar data types use.
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.
Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
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.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
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.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Arrays as Function Parameters. CSCE 1062 Outline  Passing an array argument (section 9.3)  Reading part of an array (section 9.4)  Searching and sorting.
Problem Solving and Program Design in C Chap. 7 Arrays Chow-Sing Lin.
Objectives You should be able to describe: One-Dimensional Arrays
Chapter 7: User-Defined Functions II
Computer Programming BCT 1113
Chapter 7 Arrays.
Arrays and Records.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
7 Arrays.
7 Arrays.
Presentation transcript:

Chapter 8 Arrays Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-2 Arrays Simple data types use a single memory cell to store variable. ArrayArray is a data structure which groups two or more adjacent memory cells. Syntax: element-type aname[size]; element-type aname[size]={initialization lsit}; e.g., double x[8]; char name[4];

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-3 The Elements of Array array subscriptWe use the array subscript to specify the array element being manipulated, which ranges from zero to one less than the number of size. Array subscript Array size

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-4 Array Initialization We can initialize an array in its declaration. We can omit the size of an array that is being fully initialized since the size can be deduced from the initailization list. e.g., char vowels[] = {‘A’, ‘E’, ’I’, ’O’, ’U’}; int prime[]={2, 3, 5, 7, 11, 13, 17, 19, 23};

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-5 Using for Loops for Sequential Access The elements of an array are usually processed by for loops, because –the elements are sequentially stored in the memory, and –the for loop is suitable for sequential manipulation. e.g., int square[SIZE], i; for(i=0; i<SIZE; i++) square[i]=i*i;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-6 Using Array Elements as Function Arguments (1/2) Suppose that we have a function prototype as shown below. –void do_it(double arg_1, double *arg2_p, double *arg3_p); Let x be an array of type double elements. We can call the function do_it as follows. –do_it(x[0], &x[1], &x[2]); The modification on arg2_p and arg3_p will change the values of x[1] and x[2].

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-7 Using Array Elements as Function Arguments (2/2) arg_1 has a copy of the value from x[0] arg_3 is a pointer pointed to the memory of x[2].

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-8 Array Arguments We can also pass an entire array as the input argument for a function. However, the function does not copy the array but just manipulates the original array. –An assignment to one of the array elements by a statement in the function changes the contents of the original array.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-9 An Example of Array Arguments We have the flexibility to pass an array of any size to the function. –Because C does not allocate any memory space for the array, and thus the compiler does not need to know the size. int list[] is the declaration for passing an int array

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-10 Data Areas After Calling fill_array (x, 5, 1); The value of each element in the original array are changed to 1.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-11 Alternative Formats of Array Arguments fill_array(x, 5, 1) is the same as fill_array(&x[0], 5, 1). –This call may lead to misunderstanding of the reader. In the declaration for function fill_array, we can declare the array argument as either –int list[]int *list –int list[] or int *list. If you wish to prevent the modification of the array argument, you can write const –fill_array(const int list[], …)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-12 Returning an Array Result The add_arrays function computes the sum of corresponding elements in arrays ar1 and ar2, and then stores results into ar3. Input array arguments Output array arguments

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-13 Memory for calling add_arrays(x, y, x_plus_y) Indirectly read the values of array x Indirectly read the values of array y Indirectly write the values of x+y into x_plus_y

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-14 Stacks stackThe stack is a data structure in which only the top element can be accessed. There are two operations which are associated with the stack. –Pop –Pop: remove the top element of a stack. –Push –Push: insert a new element at the top of the stack. abcabc bcbc dbcdbc poppush d

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-15 Stacks The usage of stack is commonly in daily life. –e.g., Dishes used by guests in a buffet restaurant. Stacks are the frequently used data structure in programming. –e.g., Before jumping to a sub-function, the variables in the current function are pushed into a stack. ArrayArray is one of the approaches to implement the stack.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-16 The Implementation of a Stack by an Array (push) Suppose we have the following global variables. int size = 5; char stack[size]; int count=0; The push function: void push(char item){ if(count < size){ stack[count]=item; count++; } }

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-17 The Implementation of a Stack by an Array (pop) char pop(void){ char temp; if(count > 0){ temp=stack[count]; count--; }else{ printf(“Stack empty!\n”); return 0; } return temp; }

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-18 Searching and Sorting an Array Sometimes we may need to –search a particular value in an array, or –sort an array to rearrange the array elements in numerical order. Commonly used searching methods: –Sequential search, binary search, etc. Commonly used sorting methods: –Selection sort, bubble sort, quick sort, etc.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-19 Sequential Search in an Array target is the value we want to locate in the array Traverse the array to look for the value identical to target.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-20 Selection Sort in an Array Selection sort is an intuitive sorting algorithm. –Find the index of the smallest element in the array. –Swap the smallest element with the first element. –Repeat the above steps for the 2 nd, 3 rd, …, smallest elements.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-21 An Function for Selection Sort Suppose we have a function that can find the minimal element. Swap the minimal element with the previous larger element.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-22 Multidimensional Arrays Multidimensional arrays stand for the arrays with two or more dimensions. Syntax for normal usage: element-type aname[size 1 ][size 2 ]…[size n ]; Syntax for parameter in function prototype: element-type aname[][size 2 ]…[size n ] e.g., char tictac[3][3]; –A 2-dimensional array with three rows and three columns. e.g., tictac[][3]; –The declaration for the function prototype.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-23 The Memory Allocated for tictac[3][3] The memory allocated for array tictac[3][3] is shown below. 3*3=9The number of total allocated element is 3*3=9 elements.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-24 Initialization of Multidimensional Arrays The multidimensional array can also be initialized in declarations. The initialized values are grouped in rows. –e.g., char tictac[3][3]={{‘a’, ’b’, ’c’}, {‘d’, ’e’, ’f’}, {‘ ’,‘ ’,‘ ’}}; We can also declare arrays with more dimensions. –e.g., declare a 3-dimensional array. int enroll[course][campus][cls_rank];

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-25 The Memory Allocated for enroll[100][5][4] enroll[10 0][5][4] consists of 100*5*4 =2000 elements.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-26 Homework #5 (1/2) Write a function that computes the average grades and sorts the grades of two classes. Your program should input two lists of grades for each class. Your program should output: (1) average grades for each class; (2) a list of sorted grades of two classes. You can use the sorting function in p.401 in the text book. You can either assume the number of students in each class is fixed or prompt the user to input the number.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8-27 Homework #5 (2/2) e.g., Please input grades of 1 st class: Please input grades of 2 nd class: Output: The average grades of 1 st class is: 65 The average grades of 2 nd class is: 60 The sorted grades of two classes are: You may need to use the array concepts in this chapter.