Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

Introduction to C Programming
CS0007: Introduction to Computer Programming Arrays: Higher Dimensional Arrays.
One Dimensional Arrays
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come.
©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.
More loops Linag, Chpt 3, pp The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition? loop-body.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Games and Simulations O-O Programming in Java The Walker School
1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare.
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.
More on Classes Pepper With help from rs.html.
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
CSC172 Intro Pepper. Goals Reminder of what a class is How to create and use classes How to design classes How to think in terms of objects How to comment.
11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.
Java Arrays [Lists] in 10 Minutes. Declaring an array (and its types) int[] myIntArray; double[] myDoubleArray; String[] myStrings; //What’s the pattern?
Catie Welsh March 28,  Lab 7 due Friday, April 1st, 1pm 2.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
Chapter 8: Arrays.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
(Quickly) More on Null references: A null reference is a pointer to nothing. Pointers = space for address in memory Think of RAM, with each space in memory.
Introduction to Programming G50PRO University of Nottingham Unit 8 : Methods 2 - Arrays Paul Tennent
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
ICAPRG301A Week 2 Strings and things Charles Babbage Developed the design for the first computer which he called a difference engine, though.
Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter.
CS320n –Visual Programming
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.
Data Structures & Algorithms
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Array Processing 8.2 There Are Many Uses of Arrays and Many Programming.
Compsci 201 Recitation 10 Professor Peck Jimmy Wei 11/1/2013.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
Chapter 8 P 1 Arrays and Grids Single-dimension arrays Definition An array is a sequence of elements all referred to with a common name. Other terms: table,
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Lecture 7: Arrays Tami Meredith. Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about.
COMP 110 Arrays Luv Kohli November 5, 2008 MWF 2-2:50 pm Sitterson 014.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
CSC111 Quick Revision.
Chapter 6: Using Arrays.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Truth tables: Ways to organize results of Boolean expressions.
Truth tables: Ways to organize results of Boolean expressions.
CS2011 Introduction to Programming I Arrays (I)
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Presentation transcript:

Arrays Pepper

What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String is an array Ex: Array of money earned in the top row of a Jeopardy board:

Defining an Array int[] intJeopardy = new int[7] 6 is the highest subscript Array holds 7 items [called its length] Note the number used to create it is the length – 1 This would create the exact same array, plus put in initial values: int[] intJeopardy = {0,0,0,0,0,0,0}

Getting to an Array mini-box x = intJeopardy[1] ; means x will get the value -100 intJeopardy[3] = 500; means the 200 will change to 500 for (int intCount = 0 ; intCount<= 6; intCount++){ intJeopardy[intCount] = 100; } means every value will change to 100 intJeopardy[7] will CRASH

Getting to an Array mini-box What will the box look like after these statements: intX = 3; intJeopardy[1] = 500; intJeopardy[4] = intX; intJeopardy[intX] = 70; intJeopardy[2+intX] = 600; intJeopardy[6] = intX+5;

Create your own Array In BlueJ Set up an intJeopardy array with 7 elements and set some values, and then show the values of those elements by printing them: Put 600 in the first box Put 100 in the 4 th box (box 3)

Create your own Array Solution In BlueJ Set up an intJeopardy array with 7 elements and set some values, and then show the values of those elements by printing them: int [] intJeopardy = new int[7] intJeopardy[3] = 100 intJeopardy[0] = 600 System.out.println( intJeopardy[3] + “ is the value in box 3”] System.out.println( intJeopardy[0] + “ is the value in box 0”]

Initializing an Array How do you set all values in the array to 0? Hint: a for loop from 0 to 6 could give you all the subscripts in the loop for (int intCount = 0 ; intCount<=6; intCount++){ intJeopardy[intCount] = 0; }

More using an Array What will this do? System.out.println(“Enter a number”); intJeopardy[4] = scan.NextInt(); Resolve right side: scan.NextInt(); gets a number from the user Put into left side variable Puts the number into the mini-box above #4

Total the Value Held Loop through the array mini-boxes adding the values int intTot = 0; for (int intCount = 0; intCount <= 6; intCount++){ intTot = intJeopardy[intCount] + intTot; } System.out.println(“The total money earned in the top row is “ + intTot);

Parallel Arrays intJeopardy: Cash Value of each box: intPlayed: Played indicator of each box: Has Jeopardy question 5 been played? Does the 0 in box 5 mean the question hasn’t been played, or that it earned 0 points? Show a message saying the 0 is the score or not True FalseTrue false if (intPlayed[5] == true) { System.out.println( intJeopardy[5] + “ is the score” );} else { System.out.println( intJeopardy[5] + “ is not the score”);}

Two Dimensional Arrays int [][] intJeopardy= new int[1][6] ; intJeopardy[1][4] is value 666 intJeopardy[0][4] is value 200 What is intJeopardy[0][0] value: What is intJeopardy[1][6] value: What is intJeopardy[1][3] value: What is intJeopardy[2][6] value: What is intJeopardy[1][7] value:

Array of Objects You can have an array of objects: Player class holds name and score. Player [] objPlayer = new Player[3]; objPlayer[1].name = “Carrie”; objPlayer[1].score = 50; What is objPlayer[3].score? What is objPlayer[0].name? What is objPlayer[2].name? 0123 car rie 50 ma ry 100 am y 90 joh n 0

Functions using Arrays An array variable is actually a pointer to the set of boxes, so… – Methods can change the mini-boxes in the main program [though it cannot repoint to another location] Defining in method header: – Use [] after the variable type, and all else the same – Don’t know how big it will be, so leave [] empty public static int[] mymethod(int[] arrayin){