CS Programming I: Arrays

Similar documents
CS Programming I: Branches

CS Programming I: Branches

CS Programming I: ArrayList

CS Programming I: Primitives and Expressions

CIMA Asia. Interactive Timetable Live Online

AIMMS Function Reference - Date Time Related Identifiers

Introduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions

DATE OF BIRTH SORTING (DBSORT)

CIMA Asia. Interactive Timetable Live Online

CS Programming I: Inheritance

C Structures, Unions, Bit Manipulations, and Enumerations

Arrays and Pointers (part 2) Be extra careful with pointers!

CS Programming I: Exceptions

Undergraduate Admission File

OBJECT ORIENTED PROGRAMMING USING C++

Arrays and Pointers (part 2) Be extra careful with pointers!

CS Programming I: Classes

CS Programming I: Exceptions

CS Programming I: Programming Process

Grade 4 Mathematics Pacing Guide

CMIS 102 Hands-On Lab

typedef int Array[10]; String name; Array ages;

CS Programming I: File Input / Output

CIMA Certificate BA Interactive Timetable

Freedom of Information Act 2000 reference number RFI

Arrays. What if you have a 1000 line file? Arrays

Lecture 14. Dynamic Memory Allocation

CS Programming I: File Input / Output

CS Programming I: Using Objects

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Intermediate Programming

ECSE 321 Assignment 2

Sequential Search (Searching Supplement: 1-2)

CS Programming I: Programming Process

CS Programming I: Programming Process

Multi-part functionality in PINES

Training of BRs/NCs reviewers and experts for Biennial Update Reports technical analysis. 5 th BRs and NCs lead reviewers meeting

CS Programming I: Using Objects

Introduction to Computer Programming for Non-Majors

Troop calendar

Ohad Barzilay and Oranit Dror

NMOSE GPCD CALCULATOR

Strings, Lists, and Sequences

Pushing the Limits. ADSM Symposium Sheelagh Treweek September 1999 Oxford University Computing Services 1

Library. Summary Report

Section 1.2: What is a Function? y = 4x

Practice with variables and types

Arrays. Arrays (8.1) Arrays. One variable that can store a group of values of the same type. Storing a number of related values.

High Performance Computing

INFORMATION TECHNOLOGY SPREADSHEETS. Part 1

Calendar PPF Production Cycles Non-Production Activities and Events

Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington

Coaching emerit Certified Event Find out what level you are ready for and what you need to JHB

TRIMBLE GEOSPATIAL SURVEY PRODUCTS SOFTWARE + FIRMWARE: LATEST RELEASES JAN 2018

Tutorial 8 (Array I)

Data Structures and Algorithms (DSA) Course 4. Iulian Năstac

Dictionaries. Looking up English words in the dictionary. Python sequences and collections. Properties of sequences and collections

YTD Check Register CALDWELL ISD Sort by Check Number

Contents:

TREES Lecture 12 CS2110 Spring 2018

MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY

Dashboard. 16 Jun Jun 2009 Comparing to: 16 Jun Jun % Bounce Rate. 62,921 Visits. 407,003 Page Views

Nigerian Telecommunications Sector

CONDITIONAL EXECUTION: PART 2

Trimble Products: Geospatial Survey

Syntax and Variables

CRIME ANALYSIS SACRAMENTO COUNTY SHERIFF S DEPARTMENT

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

New Concept for Article 36 Networking and Management of the List

Example. Section: PS 709 Examples of Calculations of Reduced Hours of Work Last Revised: February 2017 Last Reviewed: February 2017 Next Review:

BBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$12$

Previous Intranet Initial intranet created in 2002 Created solely by Information Systems Very utilitarian i Created to permit people to access forms r

Introduction to Computer Programming for Non-Majors

Conditional Formatting

MONITORING REPORT ON THE WEBSITE OF THE STATISTICAL SERVICE OF CYPRUS DECEMBER The report is issued by the.

Name CIS 201 Midterm II: Chapters 1-8

Interim Report Technical Support for Integrated Library Systems Comparison of Open Source and Proprietary Software

Lecture 10: Boolean Expressions

Programming for Engineers Structures, Unions

COUNCIL MEETINGS July 2017 to June 2018

Arrays. Friday, November 9, Department of Computer Science Wellesley College

COE318 Lecture Notes Week 4 (Sept 26, 2011)

What future changes are planned to improve the performance and reliability of the Wairarapa Connection?

INTRODUCING CISCO SECURITY FOR AWS

TOWN MANAGER S WEEKLY REPORT

CSE 341 Section Handout #6 Cheat Sheet

Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values.

Data Types. 9. Types. a collection of values and the definition of one or more operations that can be performed on those values

MISO PJM Joint and Common Market Cross Border Transmission Planning

BHARATI VIDYAPEETH`S INSTITUTE OF MANAGEMENT STUDIES AND RESEARCH NAVI MUMBAI ACADEMIC CALENDER JUNE MAY 2017

Scientific Programming in C X. More features & Fortran interface

TOSHIBA. Semiconductor company Ref. : H Asia Pacific Satelite Industries TO : APPROVAL SHEET FOR TC7SH04FU(T5L,JF,T.

Data Types H&K Chapter 7. Instructor - Andrew S. O Fallon CptS 121 (October 17, 2018) Washington State University

Type Definition. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

Why arrays? To group distinct variables of the same type under a single name.

Computational Expression

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

I.A.C. - Italian Activity Contest.

Characterization and Modeling of Deleted Questions on Stack Overflow

CBERS-2. Attitude Control and its Effects on Image Geometric Correction. Follow up to TCM-06 INPE CBERS TEAM

Transcription:

CS 200 - Programming I: Arrays Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624

Array Basics

1/16 Arrays a: 0 1 2 3 4 5 6 7 8 9 11 34 29 25 42 16 12 43 47 15 An Array A data structure - a particular way of organizing some data. Organization: A container that sequentially groups a fixed number of variable slots of the same type. Access items based on an index starting from 0. A (1-D) table of data.

2/16 Creating Arrays Declaration type[] arrayname; A reference type C/C++ style works, but is discouraged: type arrayname[]; E.g. int[] a; //An int array reference variable

2/16 Creating Arrays Declaration type[] arrayname; A reference type C/C++ style works, but is discouraged: type arrayname[]; E.g. int[] a; //An int array reference variable Allocating Memory new operator: arrayname = new type [length]; Table of values: arrayname = new type [] {val0, val1,... }; Declaration plus values: type[] arrayname = {val0, val1,... };

Creating Arrays Declaration type[] arrayname; Allocating Memory new operator: arrayname = new type [length]; Table of values: arrayname = new type [] {val0, val1,... }; Declaration plus values: type[] arrayname = {val0, val1,... }; Some examples: int [] a = {11, 34, 29, 25, 42, 16, 12, 43, 47, 15}; a: 0 1 2 3 4 5 6 7 8 9 11 34 29 25 42 16 12 43 47 15 2/16

Creating Arrays Declaration type[] arrayname; Allocating Memory new operator: arrayname = new type [length]; Table of values: arrayname = new type [] {val0, val1,... }; Declaration plus values: type[] arrayname = {val0, val1,... }; Some examples: int [] a = {11, 34, 29, 25, 42, 16, 12, 43, 47, 15}; a: 0 1 2 3 4 5 6 7 8 9 11 34 29 25 42 16 12 43 47 15 2/16

Creating Arrays Declaration type[] arrayname; Allocating Memory new operator: arrayname = new type [length]; Table of values: arrayname = new type [] {val0, val1,... }; Declaration plus values: type[] arrayname = {val0, val1,... }; Some examples: int [] b = new int [6]; 0 1 2 3 4 5 b: 0 0 0 0 0 0 2/16

Creating Arrays Declaration type[] arrayname; Allocating Memory new operator: arrayname = new type [length]; Table of values: arrayname = new type [] {val0, val1,... }; Declaration plus values: type[] arrayname = {val0, val1,... }; Some examples: int [] b = new int [6]; 0 1 2 3 4 5 b: 0 0 0 0 0 0 2/16

3/16 Default Values Recall: Local Variables Local variables are NOT given default values. Using an uninitialized variable gives a compilation error. Contents of Arrays Contents of arrays are given default values: byte, short, int: 0 long: OL float: 0.0f double: 0.0d char: \0 (or in unicode: \u0000 ) boolean: false Any reference type: null

4/16 Accessing Array Values 0 1 2 3 4 5 6 7 8 9 a: 11 34 29 25 42 16 12 43 47 15 Arrays are Mutable You can change the values stored in an array after it is created. BUT: you CANNOT change the length of an array after it is created. Using and Modifying Values

4/16 Accessing Array Values 0 1 2 3 4 5 6 7 8 9 a: 11 34 29 25 42 16 12 43 47 15 Arrays are Mutable You can change the values stored in an array after it is created. BUT: you CANNOT change the length of an array after it is created. Using and Modifying Values Arrays are indexed starting from zero.

4/16 Accessing Array Values 0 1 2 3 4 5 6 7 8 9 a: 11 34 29 25 42 16 12 43 47 15 Arrays are Mutable You can change the values stored in an array after it is created. BUT: you CANNOT change the length of an array after it is created. Using and Modifying Values Arrays are indexed starting from zero. Access the value: int i = a[1];

Accessing Array Values 0 1 2 3 4 5 6 7 8 9 a: 11 34 29 25 42 16 12 43 47 15 Arrays are Mutable You can change the values stored in an array after it is created. BUT: you CANNOT change the length of an array after it is created. Using and Modifying Values Arrays are indexed starting from zero. Access the value: int i = a[1]; Assign a new value: a[2] = 10; 4/16

Accessing Array Values 0 1 2 3 4 5 6 7 8 9 a: 11 34 29 10 25 42 16 12 43 47 15 Arrays are Mutable You can change the values stored in an array after it is created. BUT: you CANNOT change the length of an array after it is created. Using and Modifying Values Arrays are indexed starting from zero. Access the value: int i = a[1]; Assign a new value: a[2] = 10; 4/16

5/16 TopHat Question 1 What is the output? int [] a = new int [5]; a [0]=4; a [1]=12; a [2]= -3; a [3]=0; a [4]=5; int [] b = a; b [0] = 3; System. out. println (a [0]);

6/16 Arrays and Memory Stack Heap

6/16 Array Basics Loops and Arrays More on Arrays Arrays and Memory int [] i = new int [3]; Stack Heap

Arrays and Memory int [] i = new int [3]; Stack Heap 0 0 0 i 6/16

Arrays and Memory int [] i = new int [3]; int [] j = i; Stack Heap 0 0 0 i 6/16

Arrays and Memory int [] i = new int [3]; int [] j = i; Stack Heap 0 0 0 j i 6/16

Arrays and Memory int [] i = new int [3]; int [] j = i; j [1] = 1; Stack Heap 0 0 0 j i 6/16

Arrays and Memory int [] i = new int [3]; int [] j = i; j [1] = 1; Stack Heap 0 1 0 j i 6/16

Arrays and Memory int [] i = new int [3]; int [] j = i; j [1] = 1; int [] k = {1, 2, 3, 4}; Stack Heap 0 1 0 j i 6/16

Arrays and Memory int [] i = new int [3]; int [] j = i; j [1] = 1; int [] k = {1, 2, 3, 4}; Stack Heap 0 1 0 k j i 1 2 3 4 6/16

Arrays and Memory int [] i = new int [3]; int [] j = i; j [1] = 1; int [] k = {1, 2, 3, 4}; i = new int [] {1, 2, 3, 4}; Stack Heap 0 1 0 k j i 1 2 3 4 6/16

Arrays and Memory int [] i = new int [3]; int [] j = i; j [1] = 1; int [] k = {1, 2, 3, 4}; i = new int [] {1, 2, 3, 4}; Stack Heap 0 1 0 k j i 1 2 3 4 1 2 3 4 6/16

Arrays and Memory int [] i = new int [3]; int [] j = i; j [1] = 1; int [] k = {1, 2, 3, 4}; i = new int [] {1, 2, 3, 4}; int [] l = {1, 2, 3, 4}; Stack Heap 0 1 0 k j i 1 2 3 4 1 2 3 4 6/16

Arrays and Memory int [] i = new int [3]; int [] j = i; j [1] = 1; int [] k = {1, 2, 3, 4}; i = new int [] {1, 2, 3, 4}; int [] l = {1, 2, 3, 4}; Stack Heap 0 1 0 1 2 3 4 l k j i 1 2 3 4 1 2 3 4 6/16

Arrays and Memory Stack int [] i = new int [3]; int [] j = i; j [1] = 1; int [] k = {1, 2, 3, 4}; i = new int [] {1, 2, 3, 4}; int [] l = {1, 2, 3, 4}; k [2] = 7; Heap 0 1 0 1 2 3 4 l k j i 1 2 3 4 1 2 3 4 6/16

Arrays and Memory Stack int [] i = new int [3]; int [] j = i; j [1] = 1; int [] k = {1, 2, 3, 4}; i = new int [] {1, 2, 3, 4}; int [] l = {1, 2, 3, 4}; k [2] = 7; Heap 0 1 0 1 2 3 4 l k j i 1 2 3 4 1 2 7 4 6/16

7/16 TopHat Question 2 What is the output? int [] a = {4, 12, -3, 0, 5}; int [] b = a; int [] c = {4, 12, -3, 0, 5}; System. out. print (a == b); System. out. print (" "); System. out. print (b == c);

8/16 TopHat Question 3 What is the output? int [] a = {4, 12, -3, 0, 5}; System. out. print (a); a. [4 12-3 0 5] b. Compile error c. I d like it to print out the array, but this is clearly a trick question and it probably prints out something like [I@4f8e5cde

TopHat Question 3 What is the output? int [] a = {4, 12, -3, 0, 5}; System. out. print (a); Default tostring() [I @ 4f8e5cde Class Name Hash Code Default String representation of an object. 8/16

TopHat Question 3 What is the output? int [] a = {4, 12, -3, 0, 5}; System. out. print (a); Default tostring() [I @ 4f8e5cde Class Name Hash Code Default String representation of an object. Hash code: A function that converts the object data into a signed 32-bit integer. 8/16

Loops and Arrays

9/16 Iterating through Arrays a: 0 1 2 3 4 5 6 7 8 9 11 34 29 25 42 16 12 43 47 15 Length of an Array Arrays have final member variable called length. int i = a.length; // i is 10 Iterating an Array TopHat Question 4: What loop would generally be the most suited for iterating through an array?

10/16 Example: Printing an Array public class PrintArrayEx { } public static void main ( String [] args ) { int [] a = {4, 12, -3, 0, 5}; for ( int i = 0; i < a. length ; ++i) { System. out. print (a[i] + " "); } }

Example: Printing an Array public class PrintArrayEx { public static void main ( String [] args ) { int [] a = {4, 12, -3, 0, 5}; for ( int i = 0; i < a. length ; ++i) { System. out. print (a[i] + " "); } } // Pretty Print System. out. print ("["); for ( int i = 0; i < a. length ; ++i) { System. out. print (a[i ]); if(i < a. length - 1) System. out. print (" "); } System. out. print ("]"); } 10/16

11/16 TopHat Question 5 What is the output? int [] a = {1, 2, 3, 4, 5}; int b = 0; for ( int i = 0; i < a. length ; ++i) { b += a[i]; } System. out. print (b/a. length );

12/16 Sorting Exercise Given an array of integers, sort the array from smallest to largest.

More on Arrays

13/16 Arrays of Reference Types String [] sarr = {" Dog ", " Cat ", " Bear "}; Stack Heap

Arrays of Reference Types String [] sarr = {" Dog ", " Cat ", " Bear "}; Stack Heap sarr 13/16

Arrays of Reference Types String [] sarr = {" Dog ", " Cat ", " Bear "}; Stack Heap sarr 13/16

Arrays of Reference Types String [] sarr = {" Dog ", " Cat ", " Bear "}; Stack Heap "Dog" "Cat" "Bear" sarr 13/16

Arrays of Reference Types String [] sarr = {" Dog ", " Cat ", " Bear "}; Stack Heap "Dog" "Cat" "Bear" sarr 13/16

Arrays of Reference Types String [] sarr = {" Dog ", " Cat ", " Bear "}; Stack Heap "Dog" "Cat" "Bear" sarr 13/16

Arrays of Reference Types String [] sarr = {" Dog ", " Cat ", " Bear "}; String [] tarr = new String [ 4]; Stack Heap "Dog" "Cat" "Bear" tarr sarr 13/16

Arrays of Reference Types String [] sarr = {" Dog ", " Cat ", " Bear "}; String [] tarr = new String [ 4]; Stack Heap "Dog" "Cat" "Bear" tarr sarr null null null null 13/16

Arrays of Reference Types String [] sarr = {" Dog ", " Cat ", " Bear "}; String [] tarr = new String [ 4]; Stack Heap "Dog" "Cat" "Bear" tarr sarr null null null null 13/16

14/16 Parallel Arrays Programming Technique A way of relating data (especially different types) via the same index across multiple arrays.

Parallel Arrays Programming Technique A way of relating data (especially different types) via the same index across multiple arrays. final int NUM_MONTHS String [] monthname = new String [ NUM_MONTHS ]; String [] monthabrv = new String [ NUM_MONTHS ]; int [] monthdays = new int [ NUM_MONTHS ]; int i = 0; monthname [i] = " January "; monthabrv [i] = " Jan "; monthdays [i ++] = 31; monthname [i] = " February "; monthabrv [i] = " Feb "; monthdays [i ++] = 28; monthname [i] = " March "; monthabrv [i] = " Mar "; monthdays [i ++] = 31; monthname [i] = " April "; monthabrv [i] = " Apr "; monthdays [i ++] = 30; monthname [i] = " May "; monthabrv [i] = " May "; monthdays [i ++] = 31; monthname [i] = " June "; monthabrv [i] = " Jun "; monthdays [i ++] = 30; monthname [i] = " July "; monthabrv [i] = " Jul "; monthdays [i ++] = 31; monthname [i] = " August "; monthabrv [i] = " Aug "; monthdays [i ++] = 31; monthname [i] = " September "; monthabrv [i] = " Sep "; monthdays [i ++] = 30; monthname [i] = " October "; monthabrv [i] = " Oct "; monthdays [i ++] = 31; monthname [i] = " November "; monthabrv [i] = " Nov "; monthdays [i ++] = 30; monthname [i] = " December "; monthabrv [i] = " Dec "; monthdays [i ++] = 31; 14/16

15/16 Searching Exercise Write a method that returns the index of a given value within a sorted array of integers (sorted smallest to largest; assuming only 1 instance of any value exists).

15/16 Searching Exercise Write a method that returns the index of a given value within a sorted array of integers (sorted smallest to largest; assuming only 1 instance of any value exists). Without iterating through every single element of the array.

16/16 Further Reading COMP SCI 200: Programming I zybooks.com, 2015. zybook code: WISCCOMPSCI200Fall2017 Chapter 7. Arrays

Appendix References Appendix

Appendix References References

Appendix References 17/16 Image Sources I https://brand.wisc.edu/web/logos/ http://www.zybooks.com/