CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.

Slides:



Advertisements
Similar presentations
Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
Advertisements

7 7 Quiz Function call stack Passing values to functions Recursion.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Arrays Chapter 6.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
 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.
8 8 Function call stack Passing values to functions Recursion.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Arrays Dale Roberts, Lecturer
 2007 Pearson Education, Inc. All rights reserved. 1 C Arrays.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
Array (continue).
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.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
 2008 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 2006 Pearson Education, Inc. All rights reserved Arrays.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing Arrays to Functions 4.6Sorting Arrays 4.7Case.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
 2008 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
CSC 113: C OMPUTER P ROGRAMMING (T HEORY = 03, L AB = 01) Computer Science Department Bahria University, Islamabad.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
7 Arrays and Vectors.
C++ Programming Lecture 15 Arrays – Part II
Pointers and Pointer-Based Strings
C++ Programming Lecture 15 Arrays – Part II
C Arrays.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
7 Arrays.
Dale Roberts, Lecturer IUPUI
7 Arrays.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays Strings and Parameter Passing CSCI N305
ECE 103 Engineering Programming Chapter 32 Array Parameters
6 C Arrays.
7 Arrays.
Pointers and Pointer-Based Strings
7 Arrays and Vectors.
7 Arrays and Vectors.
Presentation transcript:

CHAPTER 07 Arrays and Vectors (part II)

OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.

7.5 Passing Arrays to Functions  To pass an array argument to a function  Specify array name without brackets Array hourlyTemperatures is declared as int hourlyTemperatures[ 24 ]; The function call modifyArray( hourlyTemperatures, 24 ); passes array hourlyTemperatures and its size to function modifyArray  Array size is normally passed as another argument so the function can process the specific number of elements in the array

7.5 Passing Arrays to Functions  Arrays are passed by reference  Function call actually passes starting address of array So function knows where array is located in memory  Caller gives called function direct access to caller ’ s data Called function can manipulate this data

Performance Tip 7.3  Passing arrays by reference makes sense for performance reasons.  If arrays were passed by value, a copy of each element would be passed. For large, frequently passed arrays, this would be time consuming and would require considerable storage for the copies of the array elements.

7.5 Passing Arrays to Functions  Functions that take arrays as arguments  Function parameter list must specify array parameter Example void modArray(int b[], int arraySize );

Declare 5 - int array array with initializer list Function takes an array as argument Pass entire array to function modifyArray

Pass array element a[ 3 ] to function modifyElement Function modifyArray manipulates the array directly

Function modifyElement manipulates array element’s copy

7.5 Passing Arrays to Functions  const array parameters  Qualifier const  Prevent modification of array values in the caller by code in the called function  Elements in the array are constant in the function body  Enables programmer to prevent accidental modification of data

Using const to prevent the function from modifying the array Array a will be const when in the body of the function

Array cannot be modified; it is const within the body function

Common Programming Error 7.11  Forgetting that arrays in the caller are passed by reference, and hence can be modified in called functions, may result in logic errors.

Software Engineering Observation 7.4  Applying the const type qualifier to an array parameter in a function definition to prevent the original array from being modified in the function body is another example of the principle of least privilege. Functions should not be given the capability to modify an array unless it is absolutely necessary.

7.7 Searching Arrays with Linear Search  Arrays may store large amounts of data  May need to determine if certain key value is located in an array  Linear search  Compares each element of an array with a search key  Just as likely that the value will be found in the first element as the last On average, program must compare the search key with half the elements of the array  To determine that value is not in array, program must compare the search key to every element in the array  Works well for small or unsorted arrays

Function takes an array, a key value, and the size of the array as arguments Function returns location of key value, -1 if not found

Return location if current value equals key value Search through entire array