Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 1 Chapter 6.1-6.4 Array Basics.

Slides:



Advertisements
Similar presentations
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Advertisements

Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Chapter 5 Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 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.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
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.
 Pearson Education, Inc. All rights reserved Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 7 Single-Dimensional.
 2006 Pearson Education, Inc. All rights reserved Arrays.
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
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Chapter 6 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 6 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 7 Single-Dimensional Arrays and C-Strings.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic 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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Chapter 7 Pointers and C-Strings
EGR 2261 Unit 9 One-dimensional Arrays
Computer Programming BCT 1113
Chapter 6 Arrays DDC 2133 Programming II.
Chapter 6 Arrays Lecturer: Mrs Rohani Hassan
Chapter 7 Single-Dimensional Arrays
Chapter 6 Arrays Solution Opening Problem
C Arrays.
Arrays Kingdom of Saudi Arabia
CSC138: Structured Programming
Chapter 7 Single-Dimensional Arrays and C-Strings
Chapter 6 Arrays.
Introducing Arrays Array is a data structure that represents a collection of the same types of data. From resourses of Y. Daniel Liang, “Introduction.
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 2 Introducing Arrays Array is a data structure that represents a collection of the same types of data.

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 3 Declaring Array Variables datatype arrayRefVar[arraySize]; Example: double myList[10]; C++ requires that the array size used to declare an array must be a constant expression. For example, the following code is illegal: int size = 4; double myList[size]; // Wrong But it would be OK, if size is a constant as follow: const int size = 4; double myList[size]; // Correct

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 4 Arbitrary Initial Values When an array is created, its elements are assigned with arbitrary values.

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 5 Using Indexed Variables After an array is created, an indexed variable can be used in the same way as a regular variable. For example, the following code adds the value in myList[0] and myList[1] to myList[2]. myList[2] = myList[0] + myList[1];

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 6 No Bound Checking C++ does not check array’s boundary. So, accessing array elements using subscripts beyond the boundary (e.g., myList[-1] and myList[11]) does not does cause syntax errors, but the operating system might report a memory access violation.

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 7 Array Initializers Declaring, creating, initializing in one step: dataType arrayName[arraySize] = {value0, value1,..., valuek}; double myList[4] = {1.9, 2.9, 3.4, 3.5};

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 8 Initializing Character Arrays char city[] = {'D', 'a', 'l', 'l', 'a', 's'}; char city[] = "Dallas"; This statement is equivalent to the preceding statement, except that C++ adds the character '\0', called the null terminator, to indicate the end of the string, as shown in Figure 6.2. Recall that a character that begins with the back slash symbol (\) is an escape character.

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 9 Initializing arrays with random values The following loop initializes the array myList with random values between 0 and 99: for (int i = 0; i < ARRAY_SIZE; i++) { myList[i] = rand() % 100; }

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 10 Printing arrays To print an array, you have to print each element in the array using a loop like the following: for (int i = 0; i < ARRAY_SIZE; i++) { cout << myList[i] << " "; }

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 11 Printing Character Array For a character array, it can be printed using one print statement. For example, the following code displays Dallas: char city[] = "Dallas"; cout << city;

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 12 Copying Arrays Can you copy array using a syntax like this? list = myList; This is not allowed in C++. You have to copy individual elements from one array to the other as follows: for (int i = 0; i < ARRAY_SIZE; i++) { list[i] = myList[i]; }

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 13 Summing All Elements Use a variable named total to store the sum. Initially total is 0. Add each element in the array to total using a loop like this: double total = 0; for (int i = 0; i < ARRAY_SIZE; i++) { total += myList[i]; }

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 14 Finding the Largest Element Use a variable named max to store the largest element. Initially max is myList[0]. To find the largest element in the array myList, compare each element in myList with max, update max if the element is greater than max. double max = myList[0]; for (int i = 1; i < ARRAY_SIZE; i++) { if (myList[i] > max) max = myList[i]; }

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 15 Finding the smallest index of the largest element double max = myList[0]; int indexOfMax = 0; for (int i = 1; i < ARRAY_SIZE; i++) { if (myList[i] > max) { max = myList[i]; indexOfMax = i; }

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 16 Passing Arrays to Functions Just as you can pass single values to a function, you can also pass an entire array to a function. Listing 6.3 gives an example to demonstrate how to declare and invoke this type of functions. PassArrayDemo Run

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 17 Passing Size along with Array Normally when you pass an array to a function, you should also pass its size in another argument. So the function knows how many elements are in the array. Otherwise, you will have to hard code this into the function or declare it in a global variable. Neither is flexible or robust.

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 18 Pass-by-Reference Passing an array variable means that the starting address of the array is passed to the formal parameter. The parameter inside the function references to the same array that is passed to the function. No new arrays are created. This is pass- by-reference. PassByReferenceDemoRun

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 19 const Parameters Passing arrays by reference makes sense for performance reasons. If an array is passed by value, all its elements must be copied into a new array. For large arrays, it could take some time and additional memory space. However, passing arrays by reference could lead to errors if your function changes the array accidentally. To prevent it from happening, you can put the const keyword before the array parameter to tell the compiler that the array cannot be changed. The compiler will report errors if the code in the function attempts to modify the array. ConstArrayDemo Compile error

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 20 Modifying Arrays in Functions Can you return an array from a function using a similar syntax? For example, you may attempt to declare a function that returns a new array that is a reversal of an array as follows: // Return the reversal of list int[] reverse(const int list[], int size) This is not allowed in C++.

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 21 Modifying Arrays in Functions, cont. However, you can circumvent this restriction by passing two array arguments in the function, as follows: // newList is the reversal of list void reverse(const int list[], list newList[], int size) ReverseArray Run