SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 7: Arrays In this chapter, you will learn about
Arrays.
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.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
© 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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
 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.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
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.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
The University of Texas – Pan American
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
 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.
Chapter 8 Arrays and Strings
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Arrays CE 102 Algorithms and Programming KTO Karatay University Arrays are data structures consisting of data items of the same type Arrays are not dynamic.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
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.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Arrays.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Computer Programming BCT 1113
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
7 Arrays.
7 Arrays.
Arrays Kingdom of Saudi Arabia
MSIS 655 Advanced Business Applications Programming
Arrays Week 2.
7 Arrays.
Arrays Arrays A few types Structures of related data items
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays

Content 2  One-dimensional arrays  Array elements  Approaches of initialization arrays  Out-of-Bound error  Array of characters  Passing arrays to functions  Searching arrays  Multidimensional arrays

Objectives 3  By the end you should recognize:  What arrays are, how they are used and why they are needed  How to use the array data structure to represent a set of related data items  How to use arrays to store, sort and search lists and tables of values  Declaration and initializing arrays  How to refer to the individual elements of arrays  Passing arrays as arguments to functions  Declaration and manipulation of multidimensional arrays  Create, trace, and debug programs that use arrays

Array  Data structure containing related data items of the same type  Hold in consecutive group of memory locations  “static” entity remain the same size throughout program execution  Syntax:  int c[5]; 4 dataType arrayName[arraySize]; Type of array elements Name of array (any valid identifier) Array size (Number of elements) ? ? ? ? ? c[0] c[1] c[2] c[3] c[4]

Array Elements  Data items in the array  Each element has a position in the array called index or subscript  First element has index 0 and last element has index arraySize-1  To access any element, giving array name followed by index in [ ]  c[0], c[1], …etc  [ ] is an operator has same precedence and associativity of ( )  Position number must be  Positive integer OR any expression that results in a positive integer int a=5, b=6; c[a+b] +=2; // Adds 2 to array element c[ 11 ] 5

Array Elements (cont.) 6 First element with index 0 Last element with index 11 Result of c[a+b] +=2; will store 80 in c[11]

Array Initialization – Approach (1) 7 Declare n as an array of int s with 10 elements Each int initialized is to 0  Using a loop to initialize array’s elements

8 n[ j ] returns int associated with index j in array n Each int has been initialized to 0

Array Initialization – Approach(2)  In declaration by using initializer list  Initializer list Items enclosed in braces {} Items in list separated by commas Items themselves called initializers int c[5] = { 10, 20, 30, 40, 50 };  If initializers are more than array elements (size)  Produce a compilation error int c[5] = { 10, 20, 30, 40, 50, 60, 70 }; c[0] c[1] c[2] c[3] c[4]

Array Initialization – Approach(2) (cont.)  If initializers are fewer than array elements (size)  Remaining elements are initialized to zero int c[5] = { 11 };  To initialize all array elements to 0  Explicitly initializes first element to zero  Implicitly initializes remaining four elements to zero int x[5] = { 0 };  Static array is initialized to zero by compiler No need to initialized explicitly c[0] c[1] c[2] c[3] c[4] x[0] x[1] x[2] x[3] x[4]

Array Initialization – Approach(2) (cont.)  Omitting array size in declaration, compiler determines array size based on number of items in initializer list int c[] = { 10, 20, 30, 40, 50 };  How many elements in array c ? OR Array size?  Index values?  Initialized values? 11

Array size As Constant variable 12 Declare constant variable arraySize using the const keyword Use array index to assign element’s value Declare array that contains 10 int s

Example: Summation of the array elements 13 Declare array with initializer list Sum all array values

Out-of-Bound Error  C++ has no array bounds checking  Does not prevent computer from referring to an element that does not exist  Logic error  Referring to an element outside the array bounds 14 i <= arraySize

Passing Arrays to Functions  Passing array as argument to function  by using array name without brackets [ ] int c[5]; modifyArray(c, 5);  To process elements within function Array size passed as argument OR Place array size as global variable  By default, arrays are passed by Reference  Called function can modify elements’ values in original array  Function call actually passes starting address of array So function knows where array is located in memory 15

Passing Arrays to Functions (cont.)  Function parameter list must specify array parameter by [ ] void modifyArray(int c[], int arraySize){}  Equivalent prototype void modifyArray(int [], int);  Array parameter may include the size of the array  Compiler will ignore it, though Compiler only cares about the address of the first element void modifyArray(int c[11], int arraySize){} 16

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

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

19 Function modifyElement manipulates array element’s copy

Passing Arrays to Functions (cont.)  Since passing array to function by reference  Called function can modify elements’ values  To prevent modification of array in called function Precede array parameter by const qualifier Void modifyArray(const int [], int); 20

21 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

Searching Arrays  Process of finding a particular element n(key value OR search key) in the array  Linear search is one of searching techniques  Compares each element of array with search key  Work well with small arrays OR unsorted arrays  Inefficient for large arrays  Binary search is another searching technique  Can be used with sorted arrays 22

23 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

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

Multidimensional Array  Multidimensional arrays with two dimensions  Called two dimensional or 2-D arrays  Represent tables of values with rows and columns  Elements referenced with two subscripts ([ x ][ y ])  Referencing a 2-D array element as c[ x ][ y ], incorrectly as c[ x, y ] 25

Initialization of Multidimensional Array  Can be initialized in declaration much like one-dimension array int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; 1 and 2 initialize b[ 0 ][ 0 ] and b[ 0 ][ 1 ] 3 and 4 initialize b[ 1 ][ 0 ] and b[ 1 ][ 1 ] int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; Row 0 contains values 1 and 0 (implicitly initialized to zero) Row 1 contains values 3 and 4 int b[ 2 ][ 2 ] = { 1, 2, 3, 4 }; 1 and 2 initialize b[ 0 ][ 0 ] and b[ 0 ][ 1 ] 3 and 4 initialize b[ 1 ][ 0 ] and b[ 1 ][ 1 ] 26

Multidimensional Array Parameters  Size of first dimension is not required  As with a one-dimensional array  Size of subsequent dimension is required  Compiler must know how many elements to skip to move to the second row void printArray( const int a[][ 3 ] ); 27

Common M-D Array Manipulation for ( int col = 0; col < 4; col++ ) a[ 2 ][ col ] = 0;  What the previous code does? total = 0; for ( row = 0; row < 3; row++ ) for ( col = 0; col < 4; col++ ) total += a[ row ][ col ];  What the previous code does? 28

Exercise – 1 (book ex. 7.18) 29 What does the following program do? #include using namespace std; int whatIsThis(int [], int); int main() { const int arraySize = 10; int a[arraySize] = {1,2,3,4,5,6,7,8,9,10} int result = whatIsThis(a, arraySize); cout<<“Result is“<<result<<endl; return 0; } int whatIsThis(int b[], int size) { if (size == 1) return b[0]; else return b[size-1] + whatIsThis(b, size-1); } #include using namespace std; int whatIsThis(int [], int); int main() { const int arraySize = 10; int a[arraySize] = {1,2,3,4,5,6,7,8,9,10} int result = whatIsThis(a, arraySize); cout<<“Result is“<<result<<endl; return 0; } int whatIsThis(int b[], int size) { if (size == 1) return b[0]; else return b[size-1] + whatIsThis(b, size-1); }

Included Sections 30 Chapter 7: sections 1, 2, 3, 4, 5, 6, 7, 9 and 10