Chapter 8 - Arrays. Chapter 8 Common to want to deal with collection of items Common to want to deal with collection of items Keep information about all.

Slides:



Advertisements
Similar presentations
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Advertisements

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 13 ARRAY LISTS AND ARRAYS. CHAPTER GOALS To become familiar with using array lists to collect objects To learn about common array algorithms To.
Chapter 7 Arrays and Array Lists. Chapter Goals To become familiar with using arrays and array lists To learn about wrapper classes, auto-boxing and the.
Chapter 13 ARRAY LISTS AND ARRAYS CHAPTER GOALS –To become familiar with using array lists to collect objects –To learn about common array algorithms –To.
Chapter 10 Introduction to Arrays
Arrays, part 2. Array applications Arrays are useful whenever a relatively large amount of data must be kept available in memory for processing We will.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays Sections 1-4.
Chapter 7 – Arrays.
Computer Science A 10: 20/3. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[]
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 Arrays.
Chapter 10 Arrays. Topics Declaring and instantiating arrays Array element access Arrays of objects Arrays as method parameters Arrays as return values.
Introduction to arrays. Array Homogeneous collection of components stored in adjacent memory locations –All elements share same data type –Entire collection.
Datalogi A 8: 27/10. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[] data.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays Lists.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
For each primitive type there is a wrapper class for storing values of that type: Double d = new Double(29.95); Wrapper Classes Wrapper objects can be.
Chapter 9: Advanced Array Concepts
Lists in Python.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Arrays : Objectives After you have read and studied this chapter, you should be able to –Manipulate a collection of data values, using an array. –Declare.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Array Basics An array is a collection of data.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
Fall 2006Slides adapted from Java Concepts companion slides1 Arrays and Array Lists Advanced Programming ICOM 4015 Lecture 7 Reading: Java Concepts Chapter.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
CS1101: Programming Methodology Recitation 5 – Arrays and Collections.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays. Arrays are objects that help us organize large amounts of information.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Common Elementary Algorithms Some of the basic but frequently used algorithms for manipulating arrays. These algorithms are so important that: a)Some programming.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Chapter VII: Arrays.
Principles of Computer Science I
Chapter 8 - Arrays.
Chapter 8 – Arrays and Array Lists
Computer Programming BCT 1113
7.6 Common Array Algorithm: Filling
Basic Files + (Chapter 10 – Arrays) : Objectives
Chapter 6: Arrays.
Chapter 6 Arrays Solution Opening Problem
Chapter 10 Arrays ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 6: Arrays.
7 Arrays.
Presentation transcript:

Chapter 8 - Arrays

Chapter 8 Common to want to deal with collection of items Common to want to deal with collection of items Keep information about all 302 students (250) Keep information about all 302 students (250) Information about all video files on computer Information about all video files on computer All of my grades since kindergarten All of my grades since kindergarten Books in a library Books in a library Infeasible to enumerate an identifier for each Infeasible to enumerate an identifier for each book1,book2,book3,….,book500 book1,book2,book3,….,book500

Basic arrays Arrays are a sequence of a data type Arrays are a sequence of a data type Two components Two components Datatype Datatype Size Size

Datatype What type of element do we went as the base of the array? What type of element do we went as the base of the array? Can be a primitive or reference type Can be a primitive or reference typeSyntax: [] ; [] ;

8.1 Arrays Problem: Want to display differences in scores from average Problem: Want to display differences in scores from average We have to keep track of each score We have to keep track of each score We could just create one identifier for all 10 scores (score1, score2…) We could just create one identifier for all 10 scores (score1, score2…) But what if we wanted to take in more scores? But what if we wanted to take in more scores? Solution: Use an array of scores to store all scores Solution: Use an array of scores to store all scores double[] scores; //OR double scores[];

Component 2: Size Must use new operator Must use new operatorSyntax: = new double[ ]; = new double[ ];Example: scores = new double[10]; double[] scores = new double[10]; What data type is an array? What data type is an array?

Memory Diagram scores

Arrays Can access an individual item in the collection Can access an individual item in the collection Use a single identifier for the whole collection (scores) and then use an indexed expression to access an array element Use a single identifier for the whole collection (scores) and then use an indexed expression to access an array element Zero based indexing Zero based indexing Index numbered 0 through size-1 Index numbered 0 through size-1 scores[0] scores[0] scores[2]//3 rd element scores[2]//3 rd element Can use expressions  scores[i+1] Can use expressions  scores[i+1]

Memory Diagram scores scores[2]

Output Difference double[] scores = new double[10]; double avg, sum = 0; for(int i=0; i < 10; i++){ System.out.print(“Enter Score”); scores[i] = in.nextDouble(); sum += scores[i]; } avg = sum/10; for(int i=0; i < 10; i++){ System.out.print("Score "+i+" "); System.out.print(score[i]+" "); System.out.println(score[i] – avg); }

Sample Run Input Enter score: 22 Enter score: 24 Enter score: 90 Enter score: 88 Enter score: 75 Enter score: 95 Enter score: 65 Enter score: 80 Enter score: 92 Enter score: 69 Output Score Score Score Score Score Score Score Score Score Score

Initial Values When array is created, all values are initialized depending on array type: When array is created, all values are initialized depending on array type: Numbers: 0 Numbers: 0 Boolean: false Boolean: false Object References: null Object References: null Means compiler does not force initialization like with primitives Means compiler does not force initialization like with primitives

Length We assumed size of array was 10…what if we aren’t sure? Or want to change size? We assumed size of array was 10…what if we aren’t sure? Or want to change size? Every array has a public constant data member length (no parenthesis) Every array has a public constant data member length (no parenthesis) for(int i=0; i < scores.length; i++){ System.out.print("Score "+i+" "); System.out.print(score[i]+" "); System.out.println(score[i] – avg); }

Initialization Say we wanted to store all the month names? Say we wanted to store all the month names? Can keep array of Strings Can keep array of Strings String[] monthName = new String[12]; monthName[0] = “Jan”; monthName[1] = “Feb”; monthName[2] = “Mar”; monthName[3] = “Apr”; monthName[4] = “May”; monthName[5] = “Jun”; monthName[6] = “Jul”; monthName[7] = “Aug”; monthName[8] = “Sep”; monthName[9] = “Oct”; monthName[10] = “Nov”; monthName[11] = “Dec”;

Adv 8.1 Initializing arrays You can also initialize all values right off the bat instead of individually as before You can also initialize all values right off the bat instead of individually as before String[] monthName = {“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”} No need to use new, or specify size No need to use new, or specify size Knows size from number of values in list Knows size from number of values in list

Array sizes So far, we have used constants to define the size of the array So far, we have used constants to define the size of the array Fixed-size array declaration Fixed-size array declaration What about if we aren’t sure how many scores we want? What about if we aren’t sure how many scores we want? Solution 1 – declare array of size 100 and get input until quit or 100 sizes are input Solution 1 – declare array of size 100 and get input until quit or 100 sizes are input

Array sizes 2 problems 2 problems Inefficient – wastes space (what if we get 1 score? Wasted space for other 99) underutilization Inefficient – wastes space (what if we get 1 score? Wasted space for other 99) underutilization Limiting – what if we want more than 100 scores? Limiting – what if we want more than 100 scores? Solution 2 – variable size array – non-constant values for size Solution 2 – variable size array – non-constant values for size But you still cannot change the size once created (length is constant) But you still cannot change the size once created (length is constant)

User defined size int size; double [] scores; System.out.println("Enter number of scores to enter: "); size = in.nextInt(); scores = new double[size]; Can use any mathematic expression Can use any mathematic expression Must give a whole number (integer) Must give a whole number (integer)

Array of Objects BankAccount[] ba; System.out.print(“Enter number of accounts: “); int numAccounts = in.nextInt(); ba = new BankAccount[numAccounts]; ba[0].deposit(500); What’s wrong?

Objects and arrays We have created the array(i.e. the bins) We have created the array(i.e. the bins) What’s in the bins? What’s in the bins? We still need to create an object for each bin We still need to create an object for each bin

Memory Diagram ba … …

Create objects Each “bin” (index of the array) is a reference Each “bin” (index of the array) is a reference When we declare an array, we declare numAccounts identifiers in essence When we declare an array, we declare numAccounts identifiers in essence Now we need to create a BankAccount object for each bin Now we need to create a BankAccount object for each bin

Array of Objects BankAccount[] ba; System.out.print(“Enter number of accounts: “); int numAccounts = in.nextInt(); ba = new BankAccount[numAccounts]; ba[0] = new BankAccount(); ba[0].deposit(500);

Memory Diagram ba … … :BankAccount 500 balanc e

Arrays of Objects We will use the Person class to illustrate this concept. We will use the Person class to illustrate this concept. An array of objects is declared and created just as an array of primitive data types is. An array of objects is declared and created just as an array of primitive data types is. Person[] person; person = new Person[20];

Fig Wu An array of Person objects after the array is created. An array of Person objects after the array is created.

The person array with one Person object added to it. The person array with one Person object added to it.

Arrays of Objects To assign data values to this object, we can execute To assign data values to this object, we can execute person[0].setName (“Ms. Latte”); person[0].setAge (20); person[0].setGender (‘F’); The syntax here is the one used to call an object’s method; we are using an indexed expression to refer to the object instead of a simple variable. The syntax here is the one used to call an object’s method; we are using an indexed expression to refer to the object instead of a simple variable.

The following program illustrates various aspects of array processing: The following program illustrates various aspects of array processing: Creating a new person array. Creating a new person array. Creating a new Person object and assigning values to it. Creating a new Person object and assigning values to it. Finding the average age of the persons in the array. Finding the average age of the persons in the array. Finding the youngest and oldest persons in the array. Finding the youngest and oldest persons in the array. Finding a particular person in the array. Finding a particular person in the array.

/* Chapter 10 Sample Program: Illustrate the processing of an array of Person objects File: Ch10ProcessPersonArray.java */ class Ch10ProcessPersonArray { public static void main (String[] args) { public static void main (String[] args) { Person[] person; //declare the person array Person[] person; //declare the person array person = new Person[5]; //and then create it person = new Person[5]; //and then create it

// Create person Array // String name, inpStr; String name, inpStr; int age; int age; char gender; char gender; for (int i = 0; i < person.length; i++) { for (int i = 0; i < person.length; i++) { //read in data values //read in data values System.out.println(“Enter info for a person”); System.out.println(“Enter info for a person”); name = in.nextLine(); name = in.nextLine(); age = in.nextInt(); age = in.nextInt(); inpStr = in.next().charAt(0); inpStr = in.next().charAt(0); //create a new Person and assign values //create a new Person and assign values person[i] = new Person( ); person[i] = new Person( ); person[i].setName ( name ); person[i].setName ( name ); person[i].setAge ( age ); person[i].setAge ( age ); person[i].setGender( gender ); person[i].setGender( gender );}

// Compute Average Age // float sum = 0, averageAge; float sum = 0, averageAge; for (int i = 0; i < person.length; i++) { for (int i = 0; i < person.length; i++) { sum += person[i].getAge(); sum += person[i].getAge(); } averageAge = sum / (float) person.length; averageAge = sum / (float) person.length; System.out.println("Average age: " + averageAge); System.out.println("Average age: " + averageAge); //-- Find the youngest and oldest person // Person youngest, //points to the youngest person oldest; //points to the oldest person oldest; //points to the oldest person youngest = person[0]; oldest = person[0]; oldest = person[0];

for (int i = 1; i < person.length; i++) { if ( person[i].getAge() < youngest.getAge() ) { if ( person[i].getAge() < youngest.getAge() ) { //found a younger person //found a younger person youngest = person[i]; youngest = person[i]; } else if (person[i].getAge() > oldest.getAge() ) { else if (person[i].getAge() > oldest.getAge() ) { //found an older person //found an older person oldest = person[i]; oldest = person[i]; }} System.out.println("Oldest: " + oldest.getName() + " is " + oldest.getAge() + " years old."); + " is " + oldest.getAge() + " years old."); System.out.println("Youngest: " + youngest.getName() + " is " + youngest.getAge() + " years old.");

Fig An array of Person objects with two Person variables. An array of Person objects with two Person variables.

Deleting object How do we delete an object? How do we delete an object? tms[x] = null; Remember garbage collection takes any objects no longer being referenced to (i.e. can never be accessed again) Remember garbage collection takes any objects no longer being referenced to (i.e. can never be accessed again)

8.3 Wrappers

Wrapper classes Allow us to treat primitives as objects Allow us to treat primitives as objects Each wrapper class has a primitive data member Each wrapper class has a primitive data member

8.4 Enhanced for Loop The traditional for loop – iterating through arrays The traditional for loop – iterating through arrays double[] data =...; double sum = 0; for (int i = 0; i < data.length; i++) { double e = data[i]; sum = sum + e; }

8.5 Simple Array Methods Finding Average Finding Average double[] scores =...//Get scores from user double avg = 0; for(int i = 0; i < scores.length; i++){ avg += scores[i]; } avg = avg / scores.length;

Count double[] scores =...//Get scores from user double avg = 0; for(int i = 0; i < scores.length; i++){ avg += scores[i]; } avg = avg / scores.length; int count = 0; for(int i = 0; i < scores.length; i++){ if(scores[i] >= avg){ count++;}}

Find Value double[] scores =...//Get scores from file double search =...//Get value to search from user boolean found = false; for(int j = 0; j < scores.length; j++) { if(Math.abs(scores[j],search) <= 1E-14) { found = true; }}

Maximum/Minimum double max = scores[0]; double min = scores[0]; for(int i = 1; i < scores.length; i++) { if(scores[i] > max) { max = scores[i]; } else if(scores[i] < min) else if(scores[i] < min){ min = scores[i]; }}

For Objects public class Bank { public int count(double atLeast) { int matches = 0; for (int j = 0; j = atLeast) matches++; // Found a match } return matches; }... private BankAccount[] accounts; }

For Objects public class Bank { public BankAccount find(int accountNumber) { for (int j = 0; j < accounts.length; j++) { if (accounts[j].getAccountNumber() == accountNumber) // Found a match return accounts[j]; } return null; // No match in the array list } }

For Objects public BankAccount maximum() { if (accounts.length == 0) return null; BankAccount largestYet = accounts[0]; for (int i = 1; i largestYet.getBalance()) largestYet = a; } return largestYet; }

10.5 Dimensional Arrays So far: Only one dimensional arrays So far: Only one dimensional arrays Many problems may require us to represent data in a “table” format, or along two-dimensions Many problems may require us to represent data in a “table” format, or along two-dimensions Two-dimensional arrays Two-dimensional arrays What do we need to add? What do we need to add?

2D Arrays Two indices – x and y Two indices – x and y Example: Example: We have multiple students, and each student has multiple scores – row for student, column for test We have multiple students, and each student has multiple scores – row for student, column for test int[][] scores; scores = new int[numStudents][numTests];

10.5 Two-Dimensional Arrays To declare our example array, we state: double[][] payScaleTable; or double payScaleTable[][]; and create the array as payScaleTable = new double[4][5];

Fig Wu Accessing an element of a two- dimensional array. Accessing an element of a two- dimensional array.

2D Arrays Arrays are a specific structure Arrays are a specific structure Two dimensional arrays are not Two dimensional arrays are not They are an array of arrays They are an array of arrays How do you check number of rows? Columns? (using the length member) How do you check number of rows? Columns? (using the length member)

10.5 Two-Dimensional Arrays The sample array creation The sample array creation payScaleTable = new double[4][5]; is a shorthand for payScaleTable = new double [4][ ]; payScaleTable[0] = new double [5]; payScaleTable[1] = new double [5]; payScaleTable[2] = new double [5]; and so on. and so on.

Fig A Wu Executing the statements on the left in sequence will create the array of arrays shown on the right. Executing the statements on the left in sequence will create the array of arrays shown on the right.

10.5 Two-Dimensional Arrays The expression payScaleTable.length refers to the length of the payScaleTable array itself.

10.5 Two-Dimensional Arrays The expression The expressionpayScaleTable[1].length refers to the length of the array stored at row 1 of payScaleTable. refers to the length of the array stored at row 1 of payScaleTable.

How do you “traverse”, or go through the elements of an multidimensional array? How do you “traverse”, or go through the elements of an multidimensional array? Row-major order (first row first, etc) Row-major order (first row first, etc) Column-major order (first column first, etc) Column-major order (first column first, etc) We can make higher-dimensional arrays (3, 4, or more) the same way we make 2D arrays We can make higher-dimensional arrays (3, 4, or more) the same way we make 2D arrays

8.7 Copying Arrays Arrays are reference variables, treat like other objects Arrays are reference variables, treat like other objects Copying an array variable yields a second reference to the same array Copying an array variable yields a second reference to the same array double[] data = new double[10]; // fill array... double[] prices = data;

Clone Use clone to make true copy Use clone to make true copy double[] prices = (double[]) data.clone(); //Note clone returns an object, so we type // cast

Copying Elements System.arraycopy() not encouraged System.arraycopy() not encouraged Hard to manipulate Hard to manipulate Using for loops is more powerful Using for loops is more powerful

Copying certain elements How do I copy count positions from array from to array to, starting at positions fromStart and toStart respectively?

Common use: insert //insert x in position i of newData //we already have an array called int newData = new int[data.length+1]; // // //insert the new element newData[i] = x; // // data = newData;

Common use: insert //insert x in position i of newData //we already have an array called int newData = new int[data.length+1]; // // for (int pos=0; pos<i; pos++) newData[pos] = data[pos]; //insert the new element newData[i] = x; // // data = newData;

Common use: insert //insert x in position i of newData //we already have an array called int newData = new int[data.length+1]; // // for (int pos=0; pos<i; pos++) newData[pos] = data[pos]; //insert the new element newData[i] = x; // // for (int pos=i+1; pos<newData.length; pos++) newData[pos] = data[pos-1]; data = newData;

Growing array If the array is full and you need more space, you can grow the array If the array is full and you need more space, you can grow the array Create a new, larger array. Create a new, larger array. double[] newData = new double[2 * data.length]; Copy all elements into the new array Copy all elements into the new array Store the reference to the new array in the array variable Store the reference to the new array in the array variable data = newData; data = newData;

Passing Arrays to Methods Exactly the same as objects, the reference is passed to method parameters Exactly the same as objects, the reference is passed to method parameters And for returning an object And for returning an object length is especially important in methods, since we have no other way of knowing the size(except sending another parameter) length is especially important in methods, since we have no other way of knowing the size(except sending another parameter)

Passing Arrays to Methods public int searchMinimum(double[] number) { int indexOfMinimum = 0; for (int i = 1; i < number.length; i++){ if (number[i] < number[indexOfMinimum]) { if (number[i] < number[indexOfMinimum]) { //found a smaller element //found a smaller element indexOfMinimum = i; } } return indexOfMinimum; } To call this method (from a method of the same class), we write

Passing Arrays to Methods double[] arrayOne; //create and assign values to arrayOne... //get the index of the smallest element of arrayOne int minOne = searchMinimum(arrayOne); //output the result System.out.print(“Minimum value in Array One is ”); System.out.print(arrayOne[minOne] + “at position ” + minOne);...

Fig. 10.9B Wu Passing an array to a method means we are passing a reference to an array. Passing an array to a method means we are passing a reference to an array.

Adv 8.4 Partially Filled Arrays Use partially filled arrays to deal with sets where size is not known before hand Use partially filled arrays to deal with sets where size is not known before hand MUST keep track of capacity and size to ensure array does not get overfilled MUST keep track of capacity and size to ensure array does not get overfilled Make size large Make size large Either resize array or enforce a limit when full Either resize array or enforce a limit when full

int capacity = 100; double[] data = new double[capacity]; int dataSize = 0; boolean stop = false; while(!stop){ System.out.print(“Enter data: “); if(in.hasNextDouble()){ double x = in.nextDouble();

if(dataSize >= capacity) { capacity *= 2; double[] newData = new double[capacity]; for(int i = 0; i < data.length; i++) newData[i] = data[i]; data = newData; } data[dataSize] = x; dataSize++;}else{ stop = true; }}

©2001 Deb Deppeler. All rights reserved. Example: Candy in a Machine Create the 2D array with 3 rows and 5 columns. Create the 2D array with 3 rows and 5 columns. Assign prices to each item. Assign prices to each item. machine[r][c] = ?? c= r=0 1 2

©2001 Deb Deppeler. All rights reserved. Example: Candy in a Machine Write a code fragment to increase the price of all items by 5 cents. Write a code fragment to increase the price of all items by 5 cents. for ( int i=0; i < machine.length; i++ ) for ( int j=0; j < machine[i].length; j++ ) for ( int j=0; j < machine[i].length; j++ ) machine[i][j] = machine[i][j] ; machine[i][j] = machine[i][j] ;