20061206 chap8 Chapter 8 Arrays (Hanly). 20061206 chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.

Slides:



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

Introduction to C Programming
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.
Understanding the Need for Sorting Records
Chapter 9: Advanced Array Manipulation
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
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.
 2006 Pearson Education, Inc. All rights reserved Arrays.
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
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.
Introduction to Programming with C++ Fourth Edition
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.
Programming Logic and Design Fourth Edition, Comprehensive
 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 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8 Arrays and Strings
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.
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
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.
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.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 12 Manipulating Larger Quantities of Data.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
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 Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
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.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
 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.
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.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
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)
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Problem Solving and Program Design in C Chap. 7 Arrays Chow-Sing Lin.
Objectives You should be able to describe: One-Dimensional Arrays
Computer Programming BCT 1113
Arrays and Records.
Arrays … The Sequel Applications and Extensions
7 Arrays.
7 Arrays.
Arrays.
Presentation transcript:

chap8 Chapter 8 Arrays (Hanly)

chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a composite of related data items stored under the same name. Array : a collection of data items of the same type.

chap8 3 Declaring & Referencing Arrays An array is a collection of two or more adjacent elements, that are associated with a particular symbolic name. Declare both the name of the array and the number of cells. double x[8]; Subscripted variable: a variable followed by a subscript in brackets, designating an array element. x[0] read as x sub zero

chap8 4

5 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 initialization list. e.g., char vowels[] = {‘A’,‘E’, ’I’, ’O’, ’U’}; int prime[]={2, 3, 5, 7, 11, 13, 17, 19, 23};

chap8 6 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 the for loop is suitable for sequential manipulation. e.g., int square[SIZE], i; for(i=0; i<SIZE; i++) square[i]=i*i;

chap8 7 Statistical Computations Using Arrays One common use of arrays is for storage of a collection of related data values. Once the values are stored, we can perform some simple statistical computations.

chap8 8 Program to Print a Table of Differences (Demo-Fig 8.3)

chap8 9

10 Using Array Elements as Function Arguments 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].

chap8 11 Data Area for Calling Module and Function do_it

chap8 12 Array Arguments We can write functions that have arrays as arguments. Such functions can manipulate some, or all, of these elements corresponding to an actual array argument. We can 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

chap8 13 Example: fill_array (Fig 8.5) 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.

chap8 14 Argument Correspondence for Array Parameters Data Areas Before Return from fill_array (x, 5, 1);

chap8 15 Alternative Formats of Array Arugments 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[] or int *list. If you wish to prevent the modification of the array argument, you can write const fill_array(const int list[], …)

chap8 16 Example: Find the Largest Element in an Array

chap8 17 Returning an Array Result In C, it is not legal for a function ’ s return type to be an array. ~~~ Chapter 6 ~~~ Function to add two arrays.

chap8 18 Function Data Areas for add_arrays(x, y, x_plus_y, 5);

chap8 19 Stacks stack The stack is a data structure in which only the top element can be accessed. The plates stored in the spring-loaded device in a buffet line perform like a stack. A customer always takes the top plate; when a plate is removed, the plate beneath it moves to the top.

chap8 20 Stacks Popping the stack: remove a value from a stack. Pushing it onto stack: store an item in a stack. Array is one of the approaches to implement stacks. abcabc bcbc dbcdbc poppush d

chap8 21 Push: Insert a new element at the top

chap8 22 Pop: remove a value

chap8 23 Searching and Sorting an Array Two common problems in processing arrays Searching an array to determine the location of a particular value. Sorting an array to rearrange the array elements in numerical order. Examples Search an array of student exam scores to determine which student, if any, got a particular score. Rearrange the array elements in increasing order by score. We can locate several different scores in the array.

chap8 24 Array Search Linear search

chap8 25 Array Search

chap8 26 Sorting 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.

chap8 27 Function select_sort (Fig 8.17)

chap8 28 Multidimensional Arrays An array with two or more dimensions The memory allocated for array tictac[3][3] is shown below. 3*3=9 The number of total allocated element is 3*3=9 elements.

chap8 29 Multidimensional Array Declaration

chap8 30 Check Whether Tic-tac-toe Board Is Filled

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

chap8 32 Three-Dimensional Array enroll

chap8 33 Summary