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

GAME:IT Junior Learning Game Maker: The Control Tab.
An introduction to arrays
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
1 More About Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 3 Wanda M. Kunkle.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
More arrays Primitive vs. reference parameters. Arrays as parameters to 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.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
An introduction to arrays, continued. Recall from last time… public static void main ( String args[] ) { //define number of rooms final int N = 100; //define.
int [] scores = new int [10];
Loop Design What goes into coding a loop. Considerations for Loop Design ● There are basically two kinds of loops: ● Those that form some accumulated.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
Scratch Programming Cards
Two dimensional arrays.
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
4. Java language basics: Function
COMP 14 Introduction to Programming
CSC 253 Lecture 9.
A basic tutorial for fundamental concepts
Recursion 4-Jun-18.
Recitation 13 Searching and Sorting.
IGCSE 6 Cambridge Effectiveness of algorithms Computer Science
Debugging and Random Numbers
Operator Precedence Operators Precedence Parentheses () unary
Java for Beginners.
Numeric Arrays Numeric Arrays Chapter 4.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Boolean Expressions and If
Psuedo Code.
Repetition-Sentinel,Flag Loop/Do_While
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Control Statement Examples
Recursion 12-Nov-18.
Introduction to Programming in Java
Starting Out with Java: From Control Structures through Objects
Iteration with While You can say that again.
Simplifying Flow of Control
Java for Beginners University Greenwich Computing At School DASCO
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Recursion 2-Dec-18.
Recursion 2-Dec-18.
int [] scores = new int [10];
searching Concept: Linear search Binary search
Recursion.
Recursion 29-Dec-18.
Arrays
Search,Sort,Recursion.
Logical Operations In Matlab.
CS139 October 11, 2004.
int [] scores = new int [10];
CS150 Introduction to Computer Science 1
Flowcharts and Pseudo Code
Loops and Arrays in JavaScript
Recursion 10-Apr-19.
Recursion 23-Apr-19.
Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR.
Introduction to Programming
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Two dimensional arrays.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Two dimensional arrays.
Lec 21 More Fun with Arrays: For Loops
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Parameters and Arguments
Programming Methodology
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.readint(); 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 thaet 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 if I would like to subscript by 1..100 (or in general, 1..N)?

Hint: Remember that an array of 100 elements is subscripted by 0..99. What if I would like to subscript by 1..100 (or in general, 1..N)? final int N = 100; int a[] = new int [N+1];

What happens if I try the following? int ray[] = new int [100]; 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;

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+1]; //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+1]; //init all rooms to unoccupied for (int i=0; i<=N; i++) { rooms[i] = false; }

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 method? Can you write this code?

Interesting code to write: Write a piece of code to count the number of free rooms. 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?