1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C arrays ❏ To be able to pass arrays and array elements to functions.

Slides:



Advertisements
Similar presentations
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
Advertisements

Chapter 9: Advanced Array Manipulation
Computer Science: A Structured Programming Approach Using C1 8-4 Array Applications In this section we study two array applications: frequency arrays with.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
An Introduction to Programming with C++ Fifth Edition
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
©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.
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
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
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.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
A First Book of ANSI C Fourth Edition
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT],MPhil (Comp.Sci), PGDCA, ADCA, Dc. Sc. & Engg.
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.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
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.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Introduction to C++ Programming Language Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University,
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.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Multidimensional Arrays.
Arrays Chapter 7.
Lecture 5 of Computer Science II
Chapter 8 Arrays Objectives
Topics discussed in this section:
Chapter 5: Arrays: Lists and Tables
Topics discussed in this section:
Arrays An Array is an ordered collection of variables
Topics discussed in this section:
Chapter 8 Multidimensional Arrays
Chapter 8 Arrays Objectives
Chapter 11 Data Structures.
Topics discussed in this section:
Arrays Chapter 7.
Multidimensional Arrays
Chapter 8 Arrays Objectives
Presentation transcript:

1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C arrays ❏ To be able to pass arrays and array elements to functions ❏ To understand the classical approach to sorting arrays: bubble sorting ❏ Array Application ❏ Two-Dimensional Arrays ❏ Multidimensional Arrays Chapter 8 Chapter 8 Arrays Arrays

2 FIGURE 8-1 Derived Types

3 8-1 Concepts Imagine we have a problem that requires us to read, process, and print a large number of integers. We must also keep the integers in memory for the duration of the program. To process large amounts of data we need a powerful data structure, the array. An array is a collection of elements of the same data type. To process large amounts of data we need a powerful data structure, the array. An array is a collection of elements of the same data type. Since an array is a sequenced collection, we can refer to the elements in the array as the first element, the second element, and so forth until we get to the last element. Since an array is a sequenced collection, we can refer to the elements in the array as the first element, the second element, and so forth until we get to the last element.

4 FIGURE 8-3 Process 10 variables FIGURE 8-2 Ten Variables

5 FIGURE 8-4 An Array of Scores

6 FIGURE 8-5 Loop for 10 Scores

7 8-2 Using Arrays in C In this section, we first show how to declare and define arrays. Then we present several typical applications using arrays including reading values into arrays, accessing and exchanging elements in arrays, and printing arrays. Declaration and Definition Accessing Elements in Arrays Storing Values in Arrays Index Range Checking Topics discussed in this section:

8 FIGURE 8-6 The Scores Array

9 FIGURE 8-7 Declaring and Defining Arrays

10 FIGURE 8-8 Initializing Arrays Only fixed-length arrays can be initialized when they are defined. Variable length arrays must be initialized by inputting or assigning the values. Note

11 FIGURE 8-9 Exchanging Scores—the Wrong Way One array cannot be copied to another using assignment. Note

12 FIGURE 8-10 Exchanging Scores with Temporary Variable

13 PROGRAM 8-1Print Ten Numbers per Line

14 PROGRAM 8-2 Squares Array

15 PROGRAM 8-3 Print Input Reversed

Inter-function Communication To process arrays in a large program, we have to be able to pass them to functions. We can pass arrays in two ways: pass individual elements or pass the whole array. In this section we discuss first how to pass individual elements and then how to pass the whole array. Passing Individual Elements Passing the Whole Array Topics discussed in this section:

17 FIGURE 8-11 Passing Array Elements FIGURE 8-12 Passing the Address of an Array Element

18 FIGURE 8-13 Passing the Whole Array 不是每一種編譯器都接受 Variable-size Array

19 PROGRAM 8-4 Calculate Array Average

20 PROGRAM 8-5 Average Elements in Variable-length Array

21 PROGRAM 8-6 Change Values in an Array

Array Applications In this section we study two array applications: frequency arrays with their graphical representations and random number permutations. Frequency Arrays Histograms Random Number Permutations Topics discussed in this section:

23 FIGURE 8-14 Frequency Array

24 FIGURE 8-15 Frequency Histogram FIGURE 8-16 Histogram Program Design

25 PROGRAM 8-7 Frequency and Histogram

26

27

28

29

30

31 FIGURE 8-17 Design for Random Number Permutations

32 PROGRAM 8-8 Generate a Permutation

33

34

Sorting One of the most common applications in computer science is sorting—the process through which data are arranged according to their values. We are surrounded by data. If the data are not ordered, we would spend hours trying to find a single piece of information. Bubble Sort Topics discussed in this section:

36 PROGRAM 8-10 Bubble Sort /* ==================== bubble Sort ==================== Sort list using bubble sort. Adjacent elements are compared and exchanged until list is ordered. Pre the list must contain at least one item last contains index to last element in list. Post list rearranged in sequence low to high */ int main () { int temp; int i, j; int list[10]={1, 3, 5, 7, 9, 2, 4, 6, 8, 10}; printf("Before sorting\n"); for(i = 0; i < 10; i++) printf("Data %d: %d\n", i, list[i]); // Outer loop for (i = 0; i < 10-1; i++) { // Inner loop: Bubble up one element each pass for (j = i+1 ; j < 10; j++) if (list[i] > list[j]) { temp = list[i]; list[i]=list[j]; list[j] = temp; } // if } // for i printf("After sorting\n"); for(i = 0; i < 10; i++) printf("Data %d: %d\n", i, list[i]); system("pause"); return 0; }

Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional arrays because the data are organized linearly in only one direction. Many applications require that data be stored in more than one dimension. One common example is a table, which is an array that consists of rows and columns. Declaration Passing A Two-Dimensional Array Topics discussed in this section:

38 FIGURE 8-34 Two-dimensional Array FIGURE 8-35 Array Of Arrays

39 PROGRAM 8-15Fill Two-dimensional Array

40 FIGURE 8-36 Memory Layout

41 PROGRAM 8-16 Convert Table to One- dimensional Array

42 FIGURE 8-37 Passing a Row

43 FIGURE 8-38 Calculate Average of Integers in Array

44 PROGRAM 8-17 Fill Matrix

Multidimensional Arrays Multidimensional arrays can have three, four, or more dimensions. The first dimension is called a plane, which consists of rows and columns. The C language considers the three-dimensional array to be an array of two-dimensional arrays. Declaring Multidimensional Arrays Topics discussed in this section:

46 FIGURE 8-41 C View of Three- dimensional Array FIGURE 8-40 A Three- dimensional Array (3 x 5 x 4)

Programming Example— Calculate Averages We previously introduced the programming concept known as incremental development. In this chapter we develop an example—calculate average—that contains many of the programming techniques. First Increment: mainYour First C Second Increment: Get Data Third Increment: Calculate Row Averages Fourth Increment: Calculate Column Averages Fifth Increment: Print Tables Topics discussed in this section:

48 FIGURE 8-42 Data Structures For Calculate Row–Column Averages

49 FIGURE 8-43 Calculate Row–Column Average Design

50 PROGRAM 8-18Calculate Row and Column Averages: main

51 PROGRAM 8-19 Calculate Row and Column Averages: Get Data

52 PROGRAM 8-19 Calculate Row and Column Averages: Get Data

53 PROGRAM 8-20 Calculate Row and Column Averages: Row Averages

54 PROGRAM 8-20 Calculate Row and Column Averages: Row Averages