For Friday Read 10.3-10.5 No quiz Program 6 due. Program 6 Any questions?

Slides:



Advertisements
Similar presentations
Chapter 21 Implementing lists: array implementation.
Advertisements

Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Chapter 8: Arrays.
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.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005.
Programming with Collections Collections in Java Using Arrays Week 9.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
©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.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
COMP 14 Introduction to Programming Miguel A. Otaduy June 1, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Arrays.
CS0007: Introduction to Computer Programming Introduction to Arrays.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Program 6 Any questions?. System.in Does the opposite of System.out.
Problem: A company employs a group of fifty salespersons (with reference numbers ) who are paid commission if their individual sales exceeds two-thirds.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Computer Programming for Engineers
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Arrays. Arrays are objects that help us organize large amounts of information.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
For Friday Finish reading chapter 9 WebCT quiz 17.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
CSC 211 Java I for loops and arrays.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
For Monday Read WebCT quiz 18.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
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.
For Wednesday No new reading No quiz.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays I Handling lists of data.
Arrays in Java.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

For Friday Read No quiz Program 6 due

Program 6 Any questions?

Problem 2 Write a program that finds the smallest of several integers. Assume that input will end when a sentinel value of –999 is read. Do not count –999 as one of the integers to consider.

Problem 3 Write a program that sums a sequence of integers. Assume that the first integer read specifies the number of values remaining to be entered. Your program should read only one value at a time. A typical input sequence might be

Practice Problem Write Java code to ask for a student’s exam score out of 100. Your program is then to match the exam score to a letter grade and print the grade to the screen. The letter grade is to be calculated as follows: 90 and aboveA 80-89B 70-79C 60-69D below 60F

Arrays Sometimes we need to process lots of similar elements We could create a variable for each element But this can lead to very unwieldy code Instead, we use arrays

What is an Array? A sequence of elements, all of the same type. An array has a name just like other variables. The entire array is referred to by this name. An array is a special kind of object.

Examples?

Creating Arrays How do we declare an array of Student objects? How do we create an array of Student objects? How do we put a Student object into the array?

Accessing a Particular Element

Processing the Whole Array

Problem 1 Write code to print all of the Students in our array, assuming that the Student class includes an appropriate toString method.

Finding the One You Want

Problem 2 Write a method to locate and print a particular Student, given the Student’s name. Assume that the Student class has a getName method.

Primitive Array Declaration To specify that a variable is an array, we include square brackets, [], in the declaration. –int [] scores; –char [] gradeArr; The square brackets can come after the variable name, as they do in some other languages: –int scores[]; –char gradeArr[];

Review Since arrays are objects, we create them using what keyword?

Array Creation In the creation, we have to specify the type and size of the array: scores = new int[5]; gradeArr = new char[10]; price = new double[20]; Once the array is created, the size of the array cannot be changed.

Array Creation continued We often use named constants or variables for the size in an array declaration: final int SIZE = 10; final int MAX_ELEMS = 15; int [] arr = new int[SIZE]; double[] flArr = new double[MAX_ELEMS];

Accessing Individual Elements Subscripts (or indices) always start at 0, so an array with 5 elements has one at 0, one at 1, one at 2, one at 3, and one at 4. We access a particular array element by using the array name followed by the index in square brackets: score[0] arr[9]

Using Array Elements All of the following are valid: score[0] = 4; score[0] += 7; score[1] = score[0] -2; score[2] = score[1] + 5 * score[0]; score[j] = score[j + 1]; Note: index can be any integral expression.

Getting Data into Arrays score[0] = 30; grade[3] = ‘A’; price[2] = 10.39;

Array Initialization We can put initial values into an array when we create it. We must list all of the values: int [] num = {58, 43, 60, 21, 38};

Array Practice Create an array to hold the tax for up to 10 different sales Create an array to hold the final letter grades for a class with up to 40 students Create an array of integers which holds the final average for those 40 students Create an array of characters with initial values ‘a’, ‘d’, ‘y’, and ‘w’ Assign TAX_RATE * price to the first item in your first array

Problem 1 Write Java code to read values from the keyboard to fill the array scores. Input should stop when a negative number is entered. The maximum size of the array is in a constant ARR_SIZE.

Problem 2 Write Java code to add up the first num_elements values in the array myVals and store the sum in the variable mySum.