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 

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 7: Arrays In this chapter, you will learn about
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
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.
Arrays Chapter 6.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Introduction to Programming with C++ Fourth Edition
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.
 Pearson Education, Inc. All rights reserved Arrays.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
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
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
 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.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
 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
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Data Strcutures.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
 Pearson Education, Inc. All rights reserved Arrays.
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.
 Pearson Education, Inc. All rights reserved 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.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
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.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Chapter 11 - JavaScript: Arrays
Arrays Chapter 7.
Data Structures I (CPCS-204)
Computer Programming BCT 1113
7 Arrays.
Review of Arrays and Pointers
MSIS 655 Advanced Business Applications Programming
Data Structures (CS212D) Week # 2: Arrays.
Arrays Chapter 7.
Arrays Week 2.
Presentation transcript:

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  Types of data structures  Based on memory allocation o Static (or fixed sized) data structures (Arrays) o Dynamic data structures (Linked lists)  Based on representation o Linear(Arrays/linked lists) o Non-linear(Trees/graphs)

Array: motivation  You want to store 5 numbers in a computer  Define 5 variables, e.g. num1, num2,..., num5  What, if you want to store 1000 numbers?  Defining 1000 variables is a pity!  Requires much programming effort  Any better solution?  Yes, some structured data type o Array is one of the most common structured data types o Saves a lot of programming effort (cf variable names)

What is an Array?  A collection of data elements in which  all elements are of the same data type, hence homogeneous data o An array of students’ marks o An array of students’ names o An array of objects (OOP perspective!)  elements (or their references) are stored at contiguous/ consecutive memory locations  Array is a static data structure  An array cannot grow or shrink during program execution – its size is fixed

Basic concepts  Array name (data)  Index/subscript (0...9)  The slots are numbered sequentially starting at zero (Java, C++)  If there are N slots in an array, the index will be 0 through N-1  Array length = N = 10  Array size = N x Size of an element = 40  Direct access to an element

Homogeneity  All elements in the array must have the same data type Index: Value: Index: Value: Index: Value: ‘A’10.255‘X’ Not an array

Contiguous Memory  Array elements are stored at contiguous memory locations  No empty segment in between values (3 & 5 are empty – not allowed) Index: Value: Index: Value:

Declaring and Creating Arrays Declaring and Creating arrays ◦ Arrays are objects that occupy memory ◦ Created dynamically with keyword new int c[] = new int[ 12 ];  Equivalent to int c[]; // declare array variable c = new int[ 12 ]; // create array  We can create arrays of objects too String b[] = new String[ 100 ];

Using Arrays  Array_name[index]  For example, in Java  System.out.println(data[4]) will display 0  data[3] = 99 will replace -3 with 99

Using Arrays  Array_name[index]  For example, in Java  System.out.println(data[4]) will display 0  data[3] = 99 will replace -3 with 99

Using Arrays  Using an array initializer  Use initializer list Items enclosed in braces ( {} ) Items in list separated by commas int n[] = { 10, 20, 30, 40, 50 }; Creates a five-element array Index values of 0, 1, 2, 3, 4  Do not need keyword new

Using Arrays (Cont.)  To declare an array follow the type with (empty) []s int[] grade; //or int grade[]; //both declare an int array  In Java arrays are objects so must be created with the new keyword  To create an array of ten integers: int[] grade = new int[10]; Note that the array size has to be specified, although it can be specified with a variable at run-time

Examples Using Arrays (Cont.) Calculating the value to store in each array element –Initialize elements of 10-element array to even integers

Examples Using Arrays (Cont.) InitArray.java Line 10 Declare array as an array of int s Line 12 Create 10 int s for array Line 16 Use array index to assign array value

Examples Using Arrays (Cont.)

 Summing the elements of an array  Array elements can represent a series of values We can sum these values

Some more concepts  data[ -1 ] always illegal  data[ 10 ] illegal (10 > upper bound)  data[ 1.5 ] always illegal  data[ 0 ] always OK  data[ 9 ] OK  Q. What will be the output of? 1. data[5] data[3] = data[3] + 10

Array’s Dimensionality  One dimensional (just a linear list)  e.g.,  Only one subscript is required to access an individual element  Two dimensional (matrix/table)  e.g., 2 x 4 matrix (2 rows, 4 columns) Col 0Col 1Col 2Col 3 Row Row

Multidimensional arrays  Multidimensional arrays  Tables with rows and columns Two-dimensional array Declaring two-dimensional array b[2][2] int b[][] = { { 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[][] = { { 1, 2 }, { 3, 4, 5 } }; row 0 contains elements 1 and 2 row 1 contains elements 3, 4 and 5

Multidimensional arrays (Cont.)  Creating multidimensional arrays  Can be allocated dynamically 3 -by- 4 array int b[][]; b = new int[ 3 ][ 4 ]; Rows can have different number of columns int b[][]; b = new int[ 2 ][ ]; // allocate rows b[ 0 ] = new int[ 5 ]; // allocate row 0 b[ 1 ] = new int[ 3 ]; // allocate row 1

Multidimensional arrays (Cont.) a[ 1 ][ 0 ]a[ 1 ][ 1 ]a[ 1 ][ 2 ]a[ 1 ][ 3 ] Row 0 Row 1 Row 2 Column 0Column 1Column 2Column 3 Row index Array name Column index a[ 0 ][ 0 ]a[ 0 ][ 1 ]a[ 0 ][ 2 ]a[ 0 ][ 3 ] a[ 2 ][ 0 ]a[ 2 ][ 1 ]a[ 2 ][ 2 ]a[ 2 ][ 3 ]

Multidimensional arrays (Cont.) InitArray.jav a Line 16 Declare array1 with six initializers in two sublists Line 17 Declare array2 with six initializers in three sublists

Multidimensional arrays (Cont.) InitArray.java Line 34 array[row].leng th returns number of columns associated with row subscript Line 35 Use double-bracket notation to access two- dimensional array values

Searching Arrays: Linear Search and Binary Search  Searching  Finding elements in large amounts of data Determine whether array contains value matching key value  Linear searching  Binary searching

Searching Arrays: Linear Search and Binary Search (Cont.)  Linear search  Compare each array element with search key If search key found, return element index If search key not found, return –1 (invalid index)  Works best for small or unsorted arrays  Inefficient for larger arrays

Linear Search LinearSearch.java Line 11 Declare array of int s

Linear Search(Cont.) LinearSearch.java Lines Allocate 100 int s for array and populate array with even int s Line 50 Loop through array Lines If array element at index matches search key, return index

Linear Search(Cont.) LinearSearch.java Line 61 Invoked when user presses Enter Line 68 Invoke method linearSearch, using array and search key as arguments

Binary Search  Binary search ◦ Efficient for large, sorted arrays ◦ Eliminates half of the elements in search through each pass  Compare middle array element to search key  If element equals key  Return array index  If element is less than key  Repeat search on first half of array  If element is greater then key  Repeat search on second half of array  Continue search until  element equals search key (success)  Search contains one element not equal to key (failure)

Binary Search (Cont.) Declae array of int s BinarySearch.java Line 14 Declare array of int s

Binary Search (Cont.) BinarySearch.java Lines Allocate 15 int s for array and populate array with even int s Line 56 Invoked when user presses Enter

Binary Search (Cont.) BinarySearch.java Line 65 Invoke method binarySearch, using array and search key as arguments

Binary Search (Cont.) BinarySearch.java Lines If search key matches middle array element, return element index Lines If search key is less than middle array element, repeat search on first array half Lines If search key is greater than middle array element, repeat search on second array half Lines Method build- Output displays array contents being searched

Binary Search (Cont.) BinarySearch.java Line 128 Display an asterisk next to middle element

Binary Search (Cont.)