Passing Arrays to Functions. COMP104 Lecture 16 / Slide 2 Array Element Pass by Value * Individual array elements can be passed by value or by reference.

Slides:



Advertisements
Similar presentations
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Advertisements

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Passing Arrays to Functions Programming. COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 2 Passing Arrays as Parameters l Arrays are.
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.,
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
10/06/08MET CS Fall Arrays and Vectors 1 Multidimensional Types: 5. Arrays and Vectors Need: ability to define and manipulate variable objects.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
1 CS 105 Lecture 13Vectors version of Wed, Apr 27, 2011, 6:20 pm.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Multiple-Subscripted Array
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 9: Pass-by-Value.
Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
 CS105 C++ Lecture 2 Arrays. Parameter passing  2 types  Pass-by-value  Pass-by-reference 2.
Programming Functions: Passing Parameters by Reference.
COMP103 - C++ Review1 Variables and Special Chars (Ch. 2, 3)
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
CSC 107 – Programming For Science. Today’s Goal  Better understand arrays and how they work  Using array variable & its entries  When calling function,
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.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
More Array Access Examples Here is an example showing array access logic: const int MAXSTUDENTS = 100; int Test[MAXSTUDENTS]; int numStudents = 0;... //
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Chapter 7 Arrays Csc 125 Introduction to C++. Topics Arrays Hold Multiple Values Array Operations Arrays as function arguments Two-dimensional arrays.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
Module 1: Array ITEI222 - Advance Programming Language.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Multidimensional.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Arrays as Function Parameters. CSCE 1062 Outline  Passing an array argument (section 9.3)  Reading part of an array (section 9.4)  Searching and sorting.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Chapter 8 Exercises.
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Engineering Problem Solving with C++, Etter
Pointers and Pointer-Based Strings
C++ Arrays.
Arrays & Functions Lesson xx
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Pointers and dynamic objects
Pointers & Functions.
Pointers Lecture 2 Tue, Jan 24, 2006.
Pointers and Pointer-Based Strings
CS150 Introduction to Computer Science 1
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Pointers and dynamic objects
Pointers & Functions.
Presentation transcript:

Passing Arrays to Functions

COMP104 Lecture 16 / Slide 2 Array Element Pass by Value * Individual array elements can be passed by value or by reference * Pass-by-value example: void printcard(int c) { if(c==1) cout << "A";... } void main() { int cards[5] ;... for(int n=0; n<5; n++) printcard(card[n]); }

COMP104 Lecture 16 / Slide 3 Array Element Pass by Reference * Pass-by-reference example: void swap(int& x, int& y) { int temp; if (x > y){ temp = x; x = y; y = temp; } void main() { int A[10] = {9,8,7,6,5,4,3,2,1,0}; swap(A[3], A[5]); }

COMP104 Lecture 16 / Slide 4 Array Element Pass by Reference * Before: * After:

COMP104 Lecture 16 / Slide 5 Passing Entire Arrays to Functions * Arrays can be passed to functions in their entirety. * All that is required is the address of the first element and dimensions of the array. * The remainder of the array will be passed by reference automatically.

COMP104 Lecture 16 / Slide 6 Arrays to Functions: Example 1 //Find the largest value in an array //input: n - number of elements to check // a[ ] - array of elements // output:index to the largest element #include using namespace std; int max_element(int n, const int a[]) { int max_index = 0; for (int i=1; i<n; i++) if (a[i] > a[max_index]) max_index = i; return max_index; } void main() { int A[10] = {9,8,7,6,5,4,10,2,1,0}; cout << A[max_element(10,A)] << endl; }

COMP104 Lecture 16 / Slide 7 Arrays to Functions: Example 2 //Add a[i] and b[i] and store the sum in c[i] //Array elements with subscripts ranging from //0 to size-1 are added element by element void add_array(int size, const double a[], const double b[], double c[]){ for (int i=0; i<size; i++) c[i] = a[i] + b[i]; } In main(): add_array (5, x, y, z );

COMP104 Lecture 16 / Slide 8 Arrays to Functions: Example 2

COMP104 Lecture 16 / Slide 9 Passing Multidimensional Arrays * How to pass a multidimensional array to a function: void displayBoard(int b[][4]); // function prototype requires variable name for arrays void main(){ int board [4][4];... displayBoard(board);... } void displayBoard(int b[][4]){ // could also be:void displayBoard(int b[4][4]){ // but NOT:void displayBoard(int b[][]){... } * When passing a multidimensional array, only the size of the 1st dimension is optional, the 2nd, 3rd, etc. dimensions must be specified.