Lecture 9 – Array (Part 2) FTMK, UTeM – Sem 1 2013/2014.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
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.
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 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
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
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.
Lesson 7 Arrays CS 1 Lesson 7 -- John Cole1. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored.
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.
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).
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
 CS105 C++ Lecture 2 Arrays. Parameter passing  2 types  Pass-by-value  Pass-by-reference 2.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 2006 Pearson Education, Inc. All rights reserved Arrays.
CPS120: Introduction to Computer Science Functions.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
Lecture: Arrays. 7.1 Arrays Hold Multiple Values.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
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”
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
 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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 7 Arrays.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Chapter 7 Arrays Csc 125 Introduction to C++. Topics Arrays Hold Multiple Values Array Operations Arrays as function arguments Two-dimensional 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.
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.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Multidimensional.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1.
Lecture 8 – Array (Part 1) FTMK, UTeM – Sem /2014.
Copyright © 2012 Pearson Education, Inc. Chapter 7: Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
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.
ARRAYS (C) KHAERONI, M.SI. OVERVIEW Introduction to Arrays Arrays in Functions Programming with Arrays Multidimensional Arrays.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Chapter 7: Arrays. 7.1 Arrays Hold Multiple Values.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Arrays Hold Multiple Values 7.1.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Chapter 8: Arrays. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
L what is a void-function? l what is a boolean function? l is it possible for a function to have no parameters? l what is program stack? function frame?
C++ Review Data Structures.
Pointers and Pointer-Based Strings
New Structure Recall “average.cpp” program
Chapter 7: Arrays.
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Previous Lecture Review
7 Arrays.
Standard Version of Starting Out with C++, 4th Edition
7 Arrays.
Pointers and Pointer-Based Strings
Arrays Arrays A few types Structures of related data items
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.
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014

 At the end of this lecture, you should be able to  Differentiate passing array by value and passing array by reference  Pass individual elements from array to a function  Pass an entire array to a function 2

 We can use function to process arrays in large program  Pass by value  The variable ‘s value is passed to a formal parameter  Changing the value of the local parameter inside the function does not effect the value of the variable outside the function  Refer to passing individual elements  Pass by reference  Passing an array 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  Refer to passing the whole array 3

By passing individual elements Same as passing any ordinary variable to a function By passing the whole array When we need the function to operate on the whole array 4

 Passing individual element  Array element must matches the function parameter type  When as a value parameter, function cannot change the value of the element in the calling function  E.g : a function, print_square receives an integer and prints its square, using an array, we can loop through the array and pass each element in turn to print_square 5 1-D array : Passing individual elements

6 Passing individual element - Example 1

7 #include using namespace std; void print(int a); // function prototype void main() { int i,j,ary1[4][3]={{1,2,3},{2,4,6},{3,6,9},{3,2,1}}; for(i = 0; i < 4; i++) { for(j = 0; j < 3; j++) print( ary1[i][j] ); cout << endl; } // end for } // end main void print(int a) { cout << a << “\t“; } // end print Passing an individual element - Example 2

 Passing the whole array  When we use large arrays in functions, by passing each value we need an extra memory to do so  E.g : if an array containing 20k elements were passed by value to a function, another 20k elements would have to be allocated in the function and each element would have to be copied from one array to another  Instead of passing the whole array, C++ passes the address of the array 8 1-D array : Passing the whole array

 An array name is the address of the first element in the array  Because of the name of array is in fact its address, passing an array name allows the called function to refer to the array back in the calling function  Two rules associated with passing the whole array  The function must be called by passing only the name of the array  In the function definition, the formal must be an array type, the size of the array doesn’t need to be specified. If provided, it is ignored by the compiler.  A function can change the elements in array by passing the array name without the constant modifier 9 1-D array : Passing the whole array

Passing the whole arrays for updating - Example 1 10 Arrays are passed by reference only The symbol & is not used when declaring an array as a formal parameter C++ does not allow functions to return a value of the type array

#include using namespace std; #define SIZE 7 void reverseOrder(int [], int); // function prototype void main() { int numbers[SIZE]={1,2,3,4,5,6,7}; reverseOrder(numbers, SIZE); } // end main void reverseOrder(int list[], int arraySize) { for (int i = arraySize-1; i >= 0; i--) cout << list[i] << "\t"; } // end reverseOrder 11

 Passing array by reference could lead to errors if your function changed accidentally  Put const keyword before the array parameter to tell the compiler that the array can’t be changed 12

13 Passing the whole array as constants : Example

 Passing an individual element - Pass the individual element by indexing the array name with the row number and the column number  Passing a row  Pass the whole row by indexing the array name with only the row number  Passing the whole array  Use the array name as the actual parameter 14 Passing the two-dimensional array to function

#include using namespace std; void print(int a); void main() { int i,j,ary1[4][3]={{1,2,3},{2,4,6},{3,6,9},{3,2,1}; for(int i=0; i<4; i++){ for(int j=0; j<3; j++) print(ary1[i][j]); cout << endl; } void print(int a) { cout << a << “\t“; } 2D Array: Passing an individual element - Example 15

2D Array : Passing a row - Example 16

2D Array : Passing the whole array – Example 1 17

#define NUMBER_OF_STUDENTS 5 // const int NUMBER_OF_STUDENTS=5; #define NUMBER_OF_QUESTIONS 10 // const int NUMBER_OF_QUESTIONS=10; void checkAnswer(char[], char[][NUMBER_OF_QUESTIONS], int, int, int[]); // function prototype void main() { char key[NUMBER_OF_QUESTIONS] = {'C','C','B','A','D','B','A','B','C','D' }; char answer[NUMBER_OF_STUDENTS][NUMBER_OF_QUESTIONS]= { {'C','D','C','C','B','B','A','B','C','B'}, {'C','C','B','C','D','B','A','B','A','B'}, {'D','D','C','A','D','B','A','B','A','D'}, {'B','C','C','C','B','B','B','B','C','C'}, {'C','C','C','A','D','B','C','B','C','D'} }; int checked[NUMBER_OF_STUDENTS]; checkAnswer(key, answer, NUMBER_OF_STUDENTS, NUMBER_OF_QUESTIONS, checked); // passing array numbers } // end main 18 2D Array : Passing the whole array - Example 2

void checkAnswer(char key[], char answer[][NUMBER_OF_QUESTIONS], int numStud, int numQues, int checked[]) { int i, j; // declaring index for (i = 0; i < numStud; i++) // initializing checked (zeros) checked [i] = 0; for (i = 0; i < numStud; i++) { for (j = 0; j < numQues; j++) { if (answer[i][j] == key[j]) checked[i] += 1; } // inner for cout << "Student " << i << "'s marks : " << checked[i] << "/" << NUMBER_OF_QUESTIONS << "." << endl; } // end for } // end checkAnswer 19 cont. eg. 2D Array : Passing the whole array

 To pass an array to a function, just use the array name: showScores(tests);  To define a function that takes an array parameter, use empty [] for array argument: void showScores(int []); // function prototype void showScores(int tests[]) // function header

 When passing an array to a function, it is common to pass array size so that function knows how many elements to process: showScores(tests, ARRAY_SIZE);  Array size must also be reflected in prototype, header: void showScores(int [], int); // function prototype void showScores(int tests[], int size) // function header 7-21

(Program Continues)

Program 7-14 (Continued)

 Can you describe the difference between passing array by value and reference?  Do you know how to pass single values from an array to a function and pass a whole array to a function?  Do you know the purpose of using constant in argument list of a function when passing an array? 24