Arrays And Functions.

Slides:



Advertisements
Similar presentations
One Dimensional Arrays
Advertisements

Lists in Python.
chap8 Chapter 8 Arrays (Hanly) chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
C++ Pointers Review. Overview  What is a pointer  Why do I care?  What can be 'pointed to'?  Example.
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.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
Objects with Functions and Arrays. Objects can be Passed Class defines type – Can use as type of function or parameter.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Department of Computer Science Western Michigan University
Arrays.
Chapter 11 - JavaScript: Arrays
ARRAYS 2D ARRAY APPLICATIONS DYNAMIC ARRAYS
EGR 2261 Unit 9 One-dimensional Arrays
Arrays Low level collections.
Motivation and Overview
Computer Programming BCT 1113
Microsoft Visual Basic 2005: Reloaded Second Edition
Array An “average.cpp” program
Tutorial 8 Pointers and Strings
Pointers and Pointer-Based Strings
Student Book An Introduction
Hashing Exercises.
Stacks Chapter 4.
Arrays in C.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
C Passing arrays to a Function
Building Java Programs
Call by Value Call by Reference Review
Object Oriented Programming COP3330 / CGS5409
Const in Classes CSCE 121 J. Michael Moore.
Heterogeneous aggregate datatypes
Pass by Reference, const, readonly, struct
Vectors.
One-Dimensional Array Introduction Lesson xx
7 Arrays.
Chapter 10: Records (structs)
Chapter 8 Arrays Objectives
Lecture 18 Arrays and Pointer Arithmetic
7. 11 Introduction to C++ Standard Library Class Template vector (Cont
Pointers Call-by-Reference CSCI 230
Objects with Functions and Arrays
Reference Parameters.
Functions with arrays.
Topics discussed in this section:
Why did the programmer quit his job?
CSE 143 Lecture 5 References and Linked Nodes
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
Chapter 8 Arrays Objectives
Visit for more Learning Resources
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Suggested self-checks: Section 7.11 #1-11
Java SE 7 One and Multi Dimensional Arrays Module 6
CS150 Introduction to Computer Science 1
Using a Queue Chapter 8 introduces the queue data type.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Using a Queue Chapter 8 introduces the queue data type.
COP 3330 Object-oriented Programming in C++
Pointers and dynamic objects
CMSC 202 Lesson 6 Functions II.
Copy Assignment CSCE 121.
Introduction to Pointers
Introduction to Computer Science
Presentation transcript:

Arrays And Functions

Pop Quiz What does this print?

Pop Quiz What does this print?

Array Parameters Array parameters passed as base address Function uses that to find elements

Debugging Need to tell debugger to display parameter as array:

Array Parameters Array parameters always act like passed by reference Modification in function modifies original

Arrays by Ref Declare array as const to prevent changes in function ALWAYS do this if function does not have to modify array!!!

I take the address of an array of exactly 5 integers Size This function is dumb: I take the address of an array of exactly 5 integers

I take the address of any size array of integers This function is broken: I take the address of any size array of integers Crap

Size Parameter Must pass size as second parameter No other way to know

Samples Add up elements in array

Debugger Trick Can make debugger show you "current item"

Samples 10 9 Swap two elements 1 2 3 4 5 6 7 8 Don't need size Modify original array – non const 1 2 3 4 5 6 7 8 10 9

Arrays Can't Be Returned Function can not have return type of array

Arrays Can't Be Returned Solution : take in existing array as non const parameter:

Samples Make a Reversed Copy Const original, non-const empty array

Samples Find smallest value 1 2 3 4 5 6 7 8 10 9

Samples 10 9 Index more powerful than value 1 2 3 4 5 6 7 8 Min value = 4 No easy way to find location Min element is at 7 Can easily get value : array[minLocation] Can manipulate based on location 1 2 3 4 5 6 7 8 10 9

Samples Find index instead of value:

Summary Passing array to function passes memory address Always "by reference" Need to pass in size No returning arrays Take as parameter & change