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.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Introduction to C Programming
Arrays.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
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.
Arrays CSE 5100 Data Structures and Algorithms. One-Dimensional Arrays  A list of values with the same data type that are stored using a single group.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Pointers CS 308 – Data Structures. Getting the address of a variable You need to use the address operator & #include void main() { int num; num = 22;
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
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.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Arrays. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Declare 14 double variables? What about.
 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.
Multiple-Subscripted Array
CS1061 C Programmuing Lecture 12 Arrays A. O’Riordan, 2004.
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.
Arrays CS 308 – Data Structures. One-Dimensional Arrays A list of values with the same data type that are stored using a single group name (array name).
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
Pointers CSE 5100 Data Structures and Algorithms.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
“In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”
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.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
Arrays.
1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the.
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.
CSIS 113A Lecture 10 Arrays Glenn Stevenson CSIS 113A MSJC.
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.
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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
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.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
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 & Arrays.
Learning Objectives Pointers Pointer in function call
Anatomy of a Function Part 2
4.9 Multiple-Subscripted Arrays
Multidimensional Arrays
Multidimensional Arrays
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.
7 Arrays.
Arrays Arrays A few types Structures of related data items
Pointers & Arrays.
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.
C++ Array 1.
Presentation transcript:

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 function as one of its arguments. int main () { int nums[3] = {3, 6, 5}; int sq; sq = square( nums[2] ); cout << nums[2] << “squared is “ << sq << endl; return 0; } int square( int x ) { return x * x; } copy (pass by value) Output : 5 squared is 25

2 Arrays & functions To pass the entire array as an argument: In the function prototype specify that the argument is an array by placing brackets after the name. Do NOT put the size of the array inside the brackets. Example: void printArray ( int arr[ ] ) ; Call the function using ONLY the array name as an argument. Example: int nums[5] = {1,2,3,4,5}; printArray( nums );

3 Arrays & functions int main () { int scores[5] = {11,12, 10, 9, 11}; printArray( scores, 5 ); return 0; } void printArray(int arr[ ], int size) { for (int i=0; i<size; i++) cout << arr[i] << “ “; cout << endl; } The parameters are again passed by value. However, the value of scores (which is copied onto arr ) is the address where the array begins. As a result, both arr and scores refer to the same location in memory! This allows a function to modify the elements of an array that is passed as an argument.

4 Arrays & functions int main () { int scores[5] = {11,12, 10, 9, 11}; cleanArray( scores, 5); printArray( scores, 5 ); return 0; } void printArray(int arr[], int size) { for (int i=0; i<size; i++) cout << arr[i] << “ “; cout << endl; } void cleanArray(int arr[], int size) { for (int i=0; i<size; i++) arr[i] = 0; } Output : This actually modifies the values stored in scores.

5 Multidimensional arrays 1D array: 2D array: 3D array: 1 row of 5 elements 3x5 array. Think of it as an array of arrays ( = an array of three elements which are arrays of five elements) 3x3x2 array.

6 Multidimensional arrays How are they stored in memory? What are the indices of the elements? row 1 row 2 row 3 3x2 array 0,00,10,2 1,01,11,21,2 0,3 1,3 2x4 array rowcolumn

7 Multidimensional arrays Declaration: similar to 1D, but specify #rows, #columns Example: double stats[10][4]; // a 10x4 array of doubles Initialization: similar to 1D. Use nested for-loops or initialize at declaration. Example 1: Example 2: for (int i = 0; i < 10; i++) for (int j = 0; j < 4; j++) stats[ i ][ j ] = 0.0; int grid[3][2] = { {0, 1}, {1, 1}, {0, 0} }; row 1 row 2 row 3

8 Multidimensional arrays CAUTION! When a multidimensional array is an argument to a function, the size of all but the first subscript MUST be specified. Example: void initArray( int arr[ ][10], int numrows); // prototype... int main() { int nums[5][10]; initArray(nums, 5); // function call... }