Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 9: Advanced Array Manipulation
Chapter 10.
5 5 Arrays. OBJECTIVES In this lecture we will learn:  Case switch  To use the array data structure to represent a set of related data items.  To declare.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Arrays Chapter 6.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
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 9: Arrays and Strings
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.
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.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Programming Logic and Design Fourth Edition, Comprehensive
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Chapter 8 Arrays and Strings
Array Processing.
計算機程式語言 Lecture 07-1 國立臺灣大學生物機電系 7 7 Arrays.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
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.
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.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
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.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
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.
Arrays Chapter 7.
An Introduction to Programming with C++ Sixth Edition
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
7 Arrays.
CNG 140 C Programming (Lecture set 8)
Arrays Kingdom of Saudi Arabia
7 Arrays.
Arrays.
Presentation transcript:

Chapter 7: Arrays

In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays Arrays as arguments Statistical analysis Objectives 2C++ for Engineers and Scientists, Fourth Edition

The Standard Template Library (STL) Searching and sorting Common programming errors Objectives (continued) 3C++ for Engineers and Scientists, Fourth Edition

One-Dimensional Arrays 4C++ for Engineers and Scientists, Fourth Edition One-dimensional array: A list of related values with the same data type, stored using a single group name (called the array name) Syntax: dataType arrayName[number-of-items] By convention, the number of items is first declared as a constant, and the constant is used in the array declaration

One-Dimensional Arrays (continued) 5C++ for Engineers and Scientists, Fourth Edition Figure 7.1 The volts and code arrays in memory

One-Dimensional Arrays (continued) 6C++ for Engineers and Scientists, Fourth Edition Element: An item in the array –Array storage of elements is contiguous Index (or subscript) of an element: The position of the element within the array –Indexes are zero-relative To reference an element, use the array name and the index of the element Figure 7.2 Identifying array elements

Index represents the offset from the start of the array Element is also called indexed variable or subscripted variable Subscripted variable can be used anywhere that a variable can be used Expressions can be used within the brackets if the value of the expression – Yields an integer value – Is within the valid range of subscripts One-Dimensional Arrays (continued) 7C++ for Engineers and Scientists, Fourth Edition

All of the elements of an array can be processed by using a loop The loop counter is used as the array index to specify the element Example: sum = 0; for (i=0; i<5; i++) sum = sum + temp[i]; One-Dimensional Arrays (continued) 8C++ for Engineers and Scientists, Fourth Edition

Array elements can be assigned values interactively using a cin stream object Out of range array indexes are not checked at compile-time – May produce run-time errors – May overwrite a value in the referenced memory location and cause other errors Array elements can be displayed using the cout stream object Input and Output of Array Values 9C++ for Engineers and Scientists, Fourth Edition

Array elements can be initialized in the array declaration statement Example: int temp[5] = {98, 87, 92, 79, 85}; Initialization: – Can span multiple lines, because white space is ignored in C++ – Starts with array element 0 if an insufficient number of values is specified If initializing in the declaration, the size may be omitted Array Initialization 10C++ for Engineers and Scientists, Fourth Edition

char array will contain an extra null character at the end of the string Example: char codes[] = “sample”; Array Initialization (continued) 11C++ for Engineers and Scientists, Fourth Edition Figure 7.4 Initializing a character array with a string adds a terminating \0 character

Declaring and Processing Two-Dimensional Arrays Two-dimensional array: Has both rows and columns – Also called a table Both dimensions must be specified in the array declaration – Row is specified first, then column Both dimensions must be specified when referencing an array element C++ for Engineers and Scientists, Fourth Edition12

Example: int val[1][3]; Declaring and Processing Two-Dimensional Arrays (cont’d) 13C++ for Engineers and Scientists, Fourth Edition Figure 7.5 Each array element is identified by its row and column position

Two-dimensional arrays can be initialized in the declaration by listing values within braces, separated by commas Braces can be used to distinguish rows, but are not required Nested for loops are used to process two- dimensional arrays – Outer loop controls the rows – Inner loop controls the columns Declaring and Processing Two-Dimensional Arrays (cont’d) 14C++ for Engineers and Scientists, Fourth Edition

Arrays with more than two dimensions can be created, but are not commonly used Think of a three-dimensional array as a book of data tables Larger Dimensional Arrays 15C++ for Engineers and Scientists, Fourth Edition Figure 7.7 Representation of a three-dimensional array

Arrays as Arguments 16C++ for Engineers and Scientists, Fourth Edition An individual array element can be passed as an argument just like any individual variable The called function receives a copy of the array element’s value Passing an entire array to a function causes the function to receive a reference to the array, not a copy of its element values The function must be declared with an array as the argument Single element of array is obtained by adding an offset to the array’s starting location

Arrays as Arguments (continued) 17C++ for Engineers and Scientists, Fourth Edition Figure 7.10 Storage of the val array

Each element of an array is obtained by adding an offset to the starting address of the array: – Address of element i = starting array address + the offset Offset for one dimensional arrays: – Offset = i * the size of the element Offset for two dimensional arrays: – Offset = column index value * the size of an element + row index value * number of bytes in a complete row Internal Array Element Location Algorithm 18C++ for Engineers and Scientists, Fourth Edition

Internal Array Element Location Algorithm (continued) 19C++ for Engineers and Scientists, Fourth Edition Figure 7.11 The offset to the element with an index value of 5

Arrays are useful in applications that require multiple passes through the same set of data elements – Case Study 1: Statistical Analysis – Case Study 2: Curve Plotting Use the four step method to implement these problems Case Studies 20C++ for Engineers and Scientists, Fourth Edition

Standard Template Library (STL): Generic set of data structures that can be modified, expanded, and contracted Each STL class is coded as a template to permit the construction of a container Container: A generic data structure, referring to a set of data items that form a natural group Vector: Similar to an array – Uses a zero-relative index, but automatically expands as needed The Standard Template Library 21C++ for Engineers and Scientists, Fourth Edition

STL Vector class provides many useful methods (functions) for vector manipulation: – insert(pos, elem) : inserts elem at position pos – name.push_back(elem) : appends elem at the end of the vector – name.size : returns the size of the vector STL also provides generic functions called algorithms The STL (continued) 22C++ for Engineers and Scientists, Fourth Edition

Must include the header files for vector and algorithm, with the namespace std Syntax: – To create and initialize a vector: vector vectorName(start,end); – To modify a specific element: vectorName[index] = newValue; – To insert a new element: vectorName.insert(index, newValue); STL provides other containers, algorithms, and iterators The STL (continued) 23C++ for Engineers and Scientists, Fourth Edition

Sorting: Arranging data in ascending or descending order for some purpose Searching: Scanning through a list of data to find a particular item A Closer Look: Searching & Sorting 24C++ for Engineers and Scientists, Fourth Edition

Search Algorithms 25C++ for Engineers and Scientists, Fourth Edition Searches can be faster if the data is in sorted order Two common methods for searching: –Linear search –Binary search Linear search is a sequential search Each item is examined in the order it occurs in the list Average number of comparisons required to find the desired item is n/2 for a list of n items

Linear Search 26C++ for Engineers and Scientists, Fourth Edition Each item in the list is examined in the order in which it occurs Not a very efficient method for searching Advantage is that the list does not have to be in sorted order On average, the number of required comparisons is n/2, where n is the number of elements in the list

Pseudocode for a linear search Linear Search (continued) 27C++ for Engineers and Scientists, Fourth Edition

Binary search requires that the list is stored in sorted order Desired item is compared to the middle element, with three possible outcomes: – Desired element was found: finished – Desired element is greater than the middle element, so discard all elements below – Desired element is less than the middle element, so discard all elements above Binary Search 28C++ for Engineers and Scientists, Fourth Edition

Pseudocode for a binary search Binary Search (continued) 29C++ for Engineers and Scientists, Fourth Edition

On each pass of binary search, the number of items to be searched is cut in half After p passes through the loop, there are n/(2 p ) elements left to search Binary Search (continued) 30C++ for Engineers and Scientists, Fourth Edition

Linear and Binary Search 31C++ for Engineers and Scientists, Fourth Edition Table 7.4 A Comparison of while Loop Passes for Linear and Binary Searches

Big O Notation – Represents “the order of magnitude of” Sort algorithms come in two major categories: – Internal sort: entire list can be resident in memory at one time – External sort: for very large lists that cannot be totally in memory at one time Big O Notation 32C++ for Engineers and Scientists, Fourth Edition

Two major categories of sorting techniques exist – Internal sort: Use when data list is small enough to be stored in the computer’s memory – External sort: Use for larger data sets stored on external disk Internal sort algorithms – Selection sort – Exchange sort Sort Algorithms 33C++ for Engineers and Scientists, Fourth Edition

Smallest element is found and exchanged with the first element Next smallest element is found and exchanged with the second element Process continues n-1 times, with each pass requiring one less comparison Selection Sort 34C++ for Engineers and Scientists, Fourth Edition

Pseudocode for a selection sort Selection Sort (continued) 35C++ for Engineers and Scientists, Fourth Edition

Selection sort advantages : – Maximum number of required moves is n-1 – Each move is a final move Selection sort disadvantages: – n(n-1)/2 comparisons are always required – Order of magnitude of selection sort: O(n 2 ) Selection Sort (continued) 36C++ for Engineers and Scientists, Fourth Edition

Successive values in the list are compared Each pair is interchanged if needed to place them in sorted order If sorting in ascending order, the largest value will “bubble up” to the last position in the list Second pass through the list stops comparing at second-to-last element Process continues until an entire pass through the list results in no exchanges Exchange (Bubble) Sort 37C++ for Engineers and Scientists, Fourth Edition

Pseudocode for an exchange sort Exchange (Bubble) Sort (continued) 38C++ for Engineers and Scientists, Fourth Edition

Number of comparisons = O(n 2 ) Maximum number of comparisons: n(n-1)/2 Maximum number of moves: n(n-1)/2 Many moves are not final moves Exchange (Bubble) Sort (continued) 39C++ for Engineers and Scientists, Fourth Edition

Failing to declare the array Using a subscript that references a non-existent array element (out of bounds) Failing to use a counter value in a loop that is large enough to cycle through all array elements Failing to initialize the array Common Programming Errors 40C++ for Engineers and Scientists, Fourth Edition

An array is a data structure that stores a list of values having the same data type – Array elements: stored in contiguous memory locations; referenced by array name/index position – Two-dimensional arrays have rows and columns – Arrays may be initialized when they are declared – Arrays may be passed to a function by passing the name of the array as the argument Arrays passed as arguments are passed by reference Individual array elements as arguments are passed by value Summary 41C++ for Engineers and Scientists, Fourth Edition