CHAPTER 6: ARRAYS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.

Slides:



Advertisements
Similar presentations
CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Advertisements

Introduction to arrays
RAPTOR Syntax and Semantics By Lt Col Schorsch
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Chapter 10 Introduction to Arrays
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
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.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
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.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
Chapter 7: Working with Arrays
CHAPTER 3: CORE PROGRAMMING ELEMENTS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Java Unit 9: Arrays Declaring and Processing Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
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.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Lists in Python.
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
19/10/20151 Data Structures Arrays. 219/10/2015 Learning Objectives Explain initialising arrays and reading data into arrays. Design and write routine/s.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
Computer Science 12 Mr. Jean May 2 nd, The plan: Video clip of the day Review of common errors in programs 2D Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Working with Arrays in MATLAB
CPS120: Introduction to Computer Science Lecture 15 Arrays.
CS 100 Introduction to Computing Seminar October 7, 2015.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
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.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter 6 Arrays Lecturer: Mrs Rohani Hassan
7 Arrays.
EKT150 : Computer Programming
Introduction To Programming Information Technology , 1’st Semester
Starting Out with Programming Logic & Design
CHAPTER 8: USING OBJECTS
Presentation transcript:

CHAPTER 6: ARRAYS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

Arrays  A data structure is any organized means of storage  An array is a simple data structure, belonging to (instantiated from) the Array Class Figure 6.1: An ordered list of variables (c) 2012 Ophir Frieder et al

One Dimensional Arrays  Arrays are like rows of numbered compartments  Arrays start counting their elements at the index zero  The n th element can be found at index n – 1  An array is one- dimensional when it has only one index or dimension  To access an element in an array, use: array_name[index] (c) 2012 Ophir Frieder et al

One Dimensional Arrays  To create a new array, use: array_name = Array.new  A simpler way to automatically create (instantiate) and initialize the same array (Example 6.2): Example 6.1: 1 arr = Array.new 2 arr[0] = 73 3 arr[1] = 98 4 arr[2] = 86 5 arr[3] = 61 6 arr[4] = 96 1 arr = [73, 98, 86, 61, 96] (c) 2012 Ophir Frieder et al

One Dimensional Arrays  To use the array, access array_name[index] as if it was a variable of the data type expected (Example 6.3) 1 arr = [5,6] 2 arr[0] = arr[0] puts arr[0] (c) 2012 Ophir Frieder et al

One Dimensional Arrays  Arrays cluster multiple data items under one name  Key advantage of using arrays: when they are used in conjunction with loops  Can use a variable for the index instead of literal numbers You can change the index in every loop iteration and traverse through every element in the array (c) 2012 Ophir Frieder et al

One Dimensional Arrays  To know when to stop traversing, get the number of elements in an array using: arr.size  New programmers often make errors dealing with the bounds of an array  Basic rules for array bounds: The first element in an array is at index 0 arr.size is not the highest indexed element The last element in an array is at arr.size – 1 (c) 2012 Ophir Frieder et al

One Dimensional Arrays  To traverse an array using a while loop:  Initialize the index to 0  Increment it for every loop iteration  The condition is index < arr.size Example 6.4: 1 arr = [73, 98, 86, 61, 96] 2 index = 0 3 while (index < arr.size) 4 puts arr[index] 5 index = index end (c) 2012 Ophir Frieder et al

One Dimensional Arrays  Running the code gives the following output:  That same array can be output with the code: puts arr (c) 2012 Ophir Frieder et al

Example: Find the Max of an Array of Positive Numbers (Example 6.5) 1 # Initialize array and loop values 2 arr = [73, 98, 86, 61, 96] 3 index = 0 4 max = # Loop over each element in arr 7 while (index < arr.size) 8 if (arr[index] > max) 9 # Update max 10 max = arr[index] 11 end 12 index = index end # Output calculated max 16 puts "Max ==> " + max.to_s (c) 2012 Ophir Frieder et al

1 # Initialize array and loop values 2 arr = [73, 98, 86, 61, 96] 3 index = 0 4 max = # Loop over each element in arr 7 while (index < arr.size) 8 if (arr[index] > max) 9 # Update max 10 max = arr[index] 11 end 12 index = index end # Output calculuated max 16 puts "Max ==> " + max.to_s Counter for the index Will store the maximum element Increments the loop (c) 2012 Ophir Frieder et al

1 # Initialize array and loop values 2 arr = [73, 98, 86, 61, 96] 3 index = 0 4 max = # Loop over each element in arr 7 while (index < arr.size) 8 if (arr[index] > max) 9 # Update max 10 max = arr[index] 11 end 12 index = index end # Output calculuated max 16 puts "Max ==> " + max.to_s Compares max to current element The output for the program is: Max ==> 98 (c) 2012 Ophir Frieder et al

Summary  An array is a data structure that stores multiple variables, belonging to the class Array  Data stored in an array are accessed using numbers as an index starting at zero (c) 2012 Ophir Frieder et al

Strings  Strings are data structures that can be viewed as one dimensional arrays of character, BUT they are NOT arrays  The most used string in programming books is “Hello World”  It does not belong to the Class Array, but to the Class String (c) 2012 Ophir Frieder et al

Strings  Strings, however, look like arrays, so it is natural to have for them access mechanisms and methods similar to arrays my_arr = Array.new my_str = String.new my_arr = [1,2,3,5,8]my_str = “Hello World” my_arr.size #5 my_str.size #11 my_arr.size #3 my_str[2] # “l” my_arr[2..3] # [3,5] my_str[2..3] # “ll” my_arr[2,3] # [3,5,8] my_str[2,3] # “llo” my_arr[2..4] # [3,5,8] my_str[8..9] # “rl” my_arr[2,4] # [3,5,8] my_str[8,9] # “rld” (c) 2012 Ophir Frieder et al

Strings  Strings, being elements (or objects) of the Class String, also have defined operations “Hello” + “ ” + “World” produces “Hello World” (c) 2012 Ophir Frieder et al

Strings and Arrays  Arrays, being objects of the Class Array, also have defined operations, such as +, with a meaning similar to String [1,2,3] + [3,5] produces [1,2,3,3,5] (c) 2012 Ophir Frieder et al

Strings and Arrays  Arrays, being objects of the Class Array, also have defined operations, such as -, which is a bit unusual [1,2,3] - [3,5] produces [1,2] [3,5] – [1,2,3] produces [5] (c) 2012 Ophir Frieder et al

Strings and Arrays  What is the meaning of – for strings? “ I am not” – “I am” Should it be “ not” ???????? NO!!!!!!! The operation (method) - Is NOT defined for the Class String (c) 2012 Ophir Frieder et al

Strings and Arrays Note also the following 3 * [1,2] is an error [1,2] * 3 is [1,2,1,2,1,2] 3 * “ab “ is an error “ab “ * 3 is “ab ab ab “ (c) 2012 Ophir Frieder et al

Multi-Dimensional Arrays  Arrays that have more than one dimension are called multidimensional arrays  Ruby basically recognizes only one dimensional arrays, but it is very flexible  For Ruby, you must put an array inside an array  A common type is the two-dimensional array, which is used to represent matrices and coordinate systems (c) 2012 Ophir Frieder et al

Multi-Dimensional Arrays  Consider the following set of grades: Geraldo73, 98, 86,61, 96 Brittany60, 90, 96, 92, 77 Michael44, 50, 99, 65, 19 (c) 2012 Ophir Frieder et al

Multi-Dimensional Arrays  To represent the following data, use an array of arrays: arr = [ [73,98,86,61,96], # arr[0] [60,90,96,92,77], # arr[1] [44,50,99,65,100] ] # arr[2] (c) 2012 Ophir Frieder et al

Multi-Dimensional Arrays  To access an individual score, use: array[row][column]  To find Brittany’s score for her third exam, type: puts “Brittany’s Third Exam: ” + arr[1][2].to_s ( Note the use of “ ” to allow the ’s )  The output should be: Brittany’s Third Exam: 96  Traversing a multidimensional array requires a nested loop for every additional dimension (c) 2012 Ophir Frieder et al

Example 6.6: Outputting Multidimensional Arrays 1 # Initialize array and loop values 2 arr = [[73, 98, 86, 61, 96], 3 [60, 90, 96, 92, 77], 4 [44, 50, 99, 65, 100]] 5 row = 0 6 column = # Loop over each row 9 while (row < arr.size) 10 puts "Row: " + row.to_s 11 # Loop over each column 12 while (column < arr[row].size) 13 # Print the item at position row x column 14 puts arr[row][column] 15 column = column end 17 # Reset column, advance row 18 column = 0 19 row = row end You can also output everything using one line: puts arr The only problem is that output will have no formatting (c) 2012 Ophir Frieder et al

Example 6.7: Modified Find the Max 1 # initialize the array and index/score variables 2 arr = [[73, 98, 86, 61, 96], 3 [60, 90, 96, 92, 77], 4 [44, 50, 99, 65, 10]] 5 6 row = 0 7 column = 0 8 maxscore = 0 9 maxrow = # for each row 12 while (row < arr.size) 13 # for each column 14 while (column < arr[row].size) 15 # update score variables 16 if (arr[row][column] > maxscore) 17 maxrow = row 18 maxscore = arr[row][column] 19 end 20 # increment column (c) 2012 Ophir Frieder et al

Example 6.7 Cont’d 21 column = column end 23 # reset column, increment row 24 column = 0 25 row = row end # output name and high score information 29 if maxrow == 0 30 puts "Geraldo has the highest score." 31 elsif maxrow == 1 32 puts "Brittany has the highest score." 33 elsif maxrow == 2 34 puts "Michael has the highest score." 35 else 36 puts "Something didn't work correctly." 37 end 38 puts "The high score was: " + maxscore.to_s (c) 2012 Ophir Frieder et al

1 # initialize the array and index/score variables 2 arr = [[73, 98, 86, 61, 96], 3 [60, 90, 96, 92, 77], 4 [44, 50, 99, 65, 10]] 5 6 row = 0 7 column = 0 8 maxscore = 0 9 maxrow = # for each row 12 while (row < arr.size) 13 # for each column 14 while (column < arr[row].size) 15 # update score variables 16 if (arr[row][column] > maxscore) 17 maxrow = row 18 maxscore = arr[row][column] 19 end 20 # increment column Initialize array arr Variables for location. Needed for traversal. Variables for the highest score and the person Loop traverses and updates the maxrow and maxscore (c) 2012 Ophir Frieder et al

21 column = column end 23 # reset column, increment row 24 column = 0 25 row = row end # output name and high score information 29 if maxrow == 0 30 puts "Geraldo has the highest score." 31 elsif maxrow == 1 32 puts "Brittany has the highest score." 33 elsif maxrow == 2 34 puts "Michael has the highest score." 35 else 36 puts "Something didn't work correctly." 37 end 38 puts "The high score was: " + maxscore.to_s End of loop (c) 2012 Ophir Frieder et al

Michael has the highest score. The high score was: 99. Output: (c) 2012 Ophir Frieder et al

Heterogeneous Arrays  All our examples used homogeneous arrays  In such arrays, all elements belong to the same class  Ruby allows an arbitrary mixing of elements, creating arbitrary dimensioned heterogeneous arrays (c) 2012 Ophir Frieder et al

Multi-Dimensional Arrays arr = Array.new arr[0] = “ Hi y’all” arr[1] = arr[2] = 17 arr[3] = [1,2,3] arr is [“ Hi y’all”, , 17, [1,2,3] ] (c) 2012 Ophir Frieder et al

Summary  Arrays are structures that use a table format to store variables  Data stored in an array are accessed using numbers as an index starting at zero  An array can have an infinite number of dimensions and can contain heterogeneous data  Hashes are like arrays, but can use any variable as a key (c) 2012 Ophir Frieder et al