An introduction to arrays

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Why not just use Arrays? Java ArrayLists.
GAME:IT Junior Learning Game Maker: The Control Tab.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
While Loops. Challenge: ● Ask the user a simple math questions ● Continue asking the question until the user gets it right.
Internal Documentation Conventions. Kinds of comments javadoc (“doc”) comments describe the user interface: –What the classes, interfaces, fields and.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
First project Create a JApplet with 3 textfields (2 for input and one for the answer), appropriate labels, and buttons for plus, minus and times. Your.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
1 More About Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 3 Wanda M. Kunkle.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
29-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
An Introduction to Textual Programming
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Nesting Loops (That’s loops inside of loops. Why not?)
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: 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.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Semester Review. As we have discussed, Friday we will have in class time for you to work on a program, this program will come with instructions and you.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
Research Topics in Computational Science. Agenda Commenting Our Code Variable Review Button Program – Add Textbox Getting text from textbox Converting.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 4 – Logical Operators & Branching.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
Beginning Fortran Introduction 13 October 2009 *Black text on white background provided for easy printing.
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
An introduction to arrays, continued. Recall from last time… public static void main ( String args[] ) { //define number of rooms final int N = 100; //define.
A: A: double “4” A: “34” 4.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
int [] scores = new int [10];
Data Structures Arrays and Lists Part 2 More List Operations.
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Loop Design What goes into coding a loop. Considerations for Loop Design ● There are basically two kinds of loops: ● Those that form some accumulated.
Loops Brent M. Dingle Texas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Two dimensional arrays.
COMP 14 Introduction to Programming
2. Java language basics (2)
Operator Precedence Operators Precedence Parentheses () unary
Java for Beginners.
An introduction to arrays
An Introduction to Java – Part I, language basics
Logical Operations In Matlab.
CS139 October 11, 2004.
Two dimensional arrays.
Two dimensional arrays.
Presentation transcript:

An introduction to arrays

Problem: a hotel reservation system Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not. What data type should we use to indicate whether or not a particular room is occupied?

One solution: Let’s declare 100 boolean variables (one for each room). //false will indicate that the room is not // occupied boolean room1 = false; boolean room2 = false; … boolean room100 = false;

How can we determine if a particular room is occupied?

How do we determine if a particular room is occupied (w/ an if)? int which = in.nextInt(); if (which==1) { if (room1) System.out.println( “ occupied” ); else System.out.println( “ not occupied” ); } else if (which==2) { if (room2) System.out.println( “ occupied” ); } else if (which==3) { … } else if (which==100) { if (room100) System.out.println( “ occupied” ); }

Is a switch any better?

How do we determine if a particular room is occupied (w/ a switch)? int which = in.nextInt(); switch (which) { case 1: if (room1) System.out.println( “ occupied” ); else System.out.println( “ not occupied” ); break; case 2: if (room2) System.out.println( “ occupied” ); case 3: … case 100: if (room100) System.out.println( “ occupied” ); }

What if we want to determine the # of rooms that are occupied?

What if we want to determine the # of rooms that are occupied? int count = 0; if (room1) ++count; if (room2) ++count; … if (room100) ++count; System.out.println( count + " rooms are occupied." );

More hotel problems: What if we add more rooms to our hotel? What if we want to see if we have a block (say 3) of adjacent rooms available? Our current approach quickly becomes unwieldy! So let’s introduce something new (arrays) to help us.

Recall scalars and vectors from math. Scalar – one number Vectors – list of numbers Vector X = < 1, -2, 52 > Or more generally, X = < x1, x2, x3 > subscript

Arrays List of the same type of things. Subscript (numbering) starts with 0. How do we declare an array? int x[] = new int [ 100 ]; How do we refer to a particular int? x[0] = 1; //first one x[1] = 7; … x[99] = 52; //last one

Hint: Remember that an array of 100 elements is subscripted by 0..99.

What happens if I try the following? int ray[] = new int [100]; // ok System.out.println( ray[-1] ); // ? int k = ray[100]; // ?

What happens if I try the following? int ray[] = new int [100]; System.out.println( ray[-1] ); int k = ray[100]; Boom!

How long (how many elements) is an array? Ask it! int howLongIsMyArray = a.length; (Reminds one of .length() and Strings.)

Back to the room reservation problem (now using arrays). It becomes much easier! How can we use arrays?

Back to the room reservation problem (now using arrays). It becomes much easier! final int N = 100; //# of rooms boolean rooms[] = new boolean[ N ]; //init all rooms to unoccupied How?

Back to the room reservation problem (now using arrays). It becomes much easier! final int N = 100; //# of rooms boolean rooms[] = new boolean[ N ]; //init all rooms to unoccupied for (int i=0; i<N; i++) { rooms[i] = false; }

Back to the room reservation problem (now using arrays). It becomes much easier! final int N = 100; //# of rooms boolean rooms[] = new boolean[ N ]; //init all rooms to unoccupied for (int i=0; i<N; i++) { rooms[i] = false; } What happens is we use <= instead of <?

Back to the room reservation problem (now using arrays). It becomes much easier! final int N = 100; //# of rooms boolean rooms[] = new boolean[ N ]; //init all rooms to unoccupied for (int i=0; i<N; i++) { rooms[i] = false; } I keep forgetting if false means occupied or not. Can you suggest a change to make it clearer?

Back to the room reservation problem (now using arrays). It becomes much easier! final int N = 100; //# of rooms boolean rooms[] = new boolean[ N ]; final boolean occupied = true; //init all rooms to unoccupied for (int i=0; i<N; i++) { rooms[i] = false; } I keep forgetting if false means occupied or not. Can you suggest a change to make it clearer? Here’s a hint.

Back to the room reservation problem (now using arrays). It becomes much easier! final int N = 100; //# of rooms boolean rooms[] = new boolean[ N ]; final boolean occupied = true; //init all rooms to unoccupied for (int i=0; i<N; i++) { rooms[i] = !occupied; } I keep forgetting if false means occupied or not. Can you suggest a change to make it clearer? Here’s a hint.

Interesting code to write: Write a piece of code to find a free room. It should determine the free room #. (It should yield -1 if no rooms are free.) Can you write this code?

Interesting code to write: Write a piece of code to randomly find a free room. It should determine the free room #. It should yield -1 if no rooms are free. Why is this a better approach in practice (in the “real world”)? Can you write this code?

Interesting code to write: Write a piece of code to count the total number of free rooms. Or conversely, write a piece of code to count the number of rooms in use. Can you write this code?

Interesting code to write: Write a piece of code to find a block of 3 free rooms. It should determine the lowest room number of the 3 free rooms (otherwise, -1). Can you write this code?

Interesting code to write: Use another array for wakeup calls. Use another array to indicate whether or not a room allows smoking or not. Can you write this code?