Introduction to Algorithms and Data Structures

Similar documents
Introduction to Algorithms and Data Structures. Structuring Data: Multidimensional Arrays, Arrays of Structures, and Structures Containing Arrays

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Chapter 8 Multi-Dimensional Arrays

Introduction to Computer Programming

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved

Chapter 7 Multidimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Introduction to Computer Programming

Introduction to Computer Programming

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

Arrays. Eng. Mohammed Abdualal

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Introduction. Data in a table or a matrix can be represented using a two-dimensional array. For example:

Exercise 1: Class Employee: public class Employee { private String firstname; private String lastname; private double monthlysalary;

CS 101 Fall 2006 Midterm 3 Name: ID:

Object Oriented Programming. Java-Lecture 6 - Arrays

Chapter 7 Multidimensional Arrays

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

Programming with Java

Question 1 [20 points]

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

COMPUTER SCIENCE. 4. Arrays. Section 1.4.

Over and Over Again GEEN163

Chapter 4: Control Structures I

University of Palestine. Mid Exam Total Grade: 100

Midterm Examination (MTA)

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Web-CAT submission URL: CAT.woa/wa/assignments/eclipse

CSC 172 Intermediate Progamming

Controls Structure for Repetition

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

1 Short Answer (10 Points Each)

Chapter 8 Multidimensional Arrays

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

Chapter 6. Arrays. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

HST 952. Computing for Biomedical Scientists Lecture 5

ESC101 : Fundamental of Computing

CAT.woa/wa/assignments/eclipse

Chapter 5: Arrays. Chapter 5. Arrays. Java Programming FROM THE BEGINNING. Copyright 2000 W. W. Norton & Company. All rights reserved.

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Arrays. Chapter 6. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

Multidimensional Arrays. CSE 114, Computer Science 1 Stony Brook University

Fundamentals of Programming Data Types & Methods

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

1 Short Answer (5 Points Each)

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Building Java Programs

Chapter 10. Further Abstraction Techniques

Computer Programming, I. Laboratory Manual. Experiment #9. Multi-Dimensional Arrays

Building Java Programs

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Lab Assignment Three

Arrays. Chapter 6. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Arrays. CSE / ENGR 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures.

Computação I. Exercises. Leonardo Vanneschi NOVA IMS, Universidade Nova de Lisboa. Leonardo Vanneschi Computação I NOVA IMS

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Full file at

Last Class. While loops Infinite loops Loop counters Iterations

Oct Decision Structures cont d

CS Computers & Programming I Review_01 Dr. H. Assadipour

CPSC 219 Extra review and solutions

Module 5. Arrays. Adapted from Absolute Java, Rose Williams, Binghamton University

Computer Science is...

CS111: PROGRAMMING LANGUAGE II

Arrays CHAPTER. 6.1 INTRODUCTION TO ARRAYS 338 Creating and Accessing Arrays 339 The length Instance Variable 342 Initializing Arrays 345

Computer Programming, I. Laboratory Manual. Final Exam Solution

CS141 Programming Assignment #10

Exercise 4: Loops, Arrays and Files

CS141 Programming Assignment #5

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

Object Oriented Programming. Java-Lecture 1

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals

Loops. GEEN163 Introduction to Computer Programming

Classes. Classes as Code Libraries. Classes as Data Structures

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

Computer Science II Data Structures

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Introduction to Computer Science Unit 2. Notes

COS 126 General Computer Science Spring Written Exam 1

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

St. Edmund Preparatory High School Brooklyn, NY

AP COMPUTER SCIENCE A

Bjarne Stroustrup. creator of C++

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

CS S-08 Arrays and Midterm Review 1

CONDITIONAL EXECUTION

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Control Structures in Java if-else and switch

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

Building Java Programs

Transcription:

Introduction to Algorithms and Data Structures Lecture 4 Structuring Data: Multidimensional Arrays, Arrays of Objects, and Objects Containing Arrays Grouping Data We don t buy individual eggs; we buy them in units of 12 ( dozens ). We often think in terms of these groups and not the individual members. Examples - classes, baseball teams, encyclopedia sets, etc. It is extremely helpful to be able to group data items that are closely related, e.g., class grades on a test, pay rates for a group of employees, etc. 1

Instead of writing: int x; Declaring Arrays we can write: int[] x = new int[10]; the name x refers to the collection (or array) of integer values, which can contain up to 10 values. The general format is: BaseType[] arrayname= new BaseType[Length]; Using An Array We can assign a value to any element in the array by specify the array by name and its index: x[0] = 87; lowest index x[1] = 90; x[9] = 93; highest index 2

Using An Array (continued) An index can be any integer or character literal, constant, variable or expression: x[6] = x[5] + 4; x[five] = 34; x[i+1] = x[i] + 3; This is really useful, because we do not want to have to write separate statement to assign values to each array element. Using a Counting Loop To Set An Array Counting loops are really useful when manipulating arrays: for (i = 0; i < 10; i++) x[i] = keyb.nextint(); 3

Example Finding the High Score We want to find the highest grade among a group of test scores. There will be than 10 grades. Available input the 10 test scores Required output the highest score Algorithm: 1. Read the scores 2. Find the highest score 3. Print the result Refining the Algorithm 1. Read the scores 2. Find the highest score 3. Print the result 1.1 For each score: 1.1.1 Prompt the user 1.1.2 Read the scores 4

Refining the Algorithm 1.1 For each score: 1.1.1 Prompt the user 1.1.2 Read the scores 2. Find the highest score 3. Print the result 2.1 For each score: 2.1.1 If it is higher than the current highest score 2.1.1.1 It is now the new highest score import java.util.scanner; FirstArray.java public class FirstArray { // FirstArray() - Find the highest score among a // group of 10 scores public static void main(string[] args) { Scanner keyb = new Scanner(System.in); int [] scores = new int[10]; int i, maxscore = 0; // Read in the array for (i = 0; i < x.length; i++) { System.out.println("Enter grade #" + i); scores[i] = keyb.nextint(); 5

// Find the high score for (i = 0; i < x.length; i++) if (x[i] > maxscore) maxscore = x[i]; // Print the result System.out.println("The highest score is " + maxscore); Initializing Arrays You can initialize an entire array by writing: int [] age = { 2, 12, 1; age is now an array of length 3 where age[0] = 2, age[1] = 12 and age[2] = 1. 6

Arrays are Objects Arrays are a special type of object. When we write x = new int[10]; We are allocating space for the object s data (i.e., properties and methods). The name x by itself refers to the address at which the object x s data is stored. Arrays as Parameters Even though Java normally passes parameters by value, it will passes arrays and other types of objects by reference (because the object s name is a reference). Passing an element in an array is different from passing the entire array, i.e., f(x) and f(x[i]) are very different 7

SecondArray.java import java.util.scanner; public class SecondArray { // SecondArray() - Shows how passing array // parameters works public static void main(string[] args) { int [] x = { 1, 2, 3, 4; // First use the scalar parameter myfunc(x[0]); for (int i = 0; i < x.length; i++) System.out.print(" " + x[i]); System.out.println(); // Second use the array parameter myfunc(x); for (int i = 0; i < x.length; i++) System.out.print(" " + x[i]); System.out.println(); 8

// myfunc() - The array method public static void myfunc(int [] a) { System.out.println("This is the array method."); for (int i = 0; i < a.length; i++) a[i] = 5 * (i + 1); // myfunc() - The scalar method public static void myfunc(int a) { System.out.println("This is the scalar method."); a = 99; Comparing Arrays Arrays, like any other method, cannot be compared using the == operator. This would only see if they hold the same reference. One needs to write an equals method to compare the arrays. 9

ArrayEquals.java public class ArrayEquals { public static void main(string [] args) { int [] x = new int[10], y = new int[10]; int i; for (i = 0; i < x.length; i++) x[i] = i; for (i = 0; i < y.length; i++) y[i] = i; if (x == y) System.out.println ("x and y are equal by \"==\""); else System.out.println ("x and y are not equal by \"==\""); if (arrayequals(x, y)) System.out.println ("x and y are equal by \"equalsarray\""); else System.out.println ("x and y are not equal by \"equalsarray\""); 10

public static boolean arrayequals(int [] a, int [] b) { if (a.length!= b.length) return false; for (int i = 0; i < a.length; i++) if (a[i]!= b[i]) return false; return true; Output x and y are not equal by "==" x and y are equal by "equalsarray" Parameters for main() Sometimes you may wish for the main() method to receive parameters. Because this is usually via the command line, they are also called command line parameters. These are passed to the main() method as an array of Strings. Like any other array, it has a length property that we can use. 11

PrintCommandParameters.java public class PrintCommandParameters { public static void main(string [] args) { for (int i = 0; i < args.length; i++) System.out.println("Parameter #" + i + " is " + args[i]); Output >Java PrintCommandParameters this is a test Parameter #0 is this Parameter #0 is is Parameter #0 is a Parameter #0 is test Grades.java A Class With an Array import java.util.scanner; public class Grades { final static int numstudents = 30, numexams = 4; static int[][] grades = new int [numstudents][numexams]; static int[] averages = new int[numstudents]; 12

// CalcAverages() - Calculate the term // averages for a class // The average is based on // four exams public static void main(string[] args) { //Get the grades, find the averages and //print them readgrades(); findaverages(); writegrades(); // readgrades() - Read the complete set of // grades public static void readgrades() { Scanner keyb = new Scanner(System.in); String instring = new String(); int i, j; // Get each students grade for (i = 0; i < numstudents; i++) { //Get the next grade for this student for (j = 0; j < numexams; j++) { System.out.println("Grade on test #" + j + " for student # " + i + "\t?"); grades[i][j] = keyb.nextint(); //Skip one line for clarity System.out.println(); 13

// findaverages() - Find the average for each // student public static void findaverages() { int i, j, sum; for (i = 0; i < numstudents; i++) { sum = 0; for (j = 0; j < numexams; j++) sum += grades[i][j]; averages[i] = sum/numexams; // WriteAverage() - Output the grades and // average for each student public static void writegrades() { int i, j; // Print a heading System.out.println("Student Exam1\tExam2\t" + "Exam3\tExam4\tAverage"); for (i = 0; i < numstudents; i++) { // Number each line, then print the grades // and the average for the enxt student System.out.print(i); 14

for (j = 0; j < numexams; j++) { System.out.print("\t"); System.out.print(grades[i][j]); System.out.println("\t" + averages[i] ); Revising Grades.java import java.util.scanner; public class Grades { private final int maxstudents = 30, numexams = 4; private int numstudents; private int[][] grades; private int [] averages; // Grades() - The default constructor public Grades() { // Constructing the array grades = new int[maxstudents][numexams]; 15

// readgrades() - Read the complete set of // grades public int readgrades() { int j; String instring = new String(); Scanner keyb = new Scanner(System.in); System.out.println("Enter a negative grade " + "for exam 1 to finish"); numstudents = 0; //There are no students yet // Get each students grade do { //Get the first exam grades. If it's // non-negative, get the rest. If // it IS negative, there are no more // students. System.out.println("Grade on test #1" + " for student # " + (numstudents+1) + "\t?"); grades[numstudents][0] = keyb.nextint(); if (grades[numstudents][0] < 0) break; for (j = 1; j < numexams; j++) { System.out.println("Grade on test #" + (j+1) + " for student # " + (numstudents+1) + "\t?"); grades[numstudents][j] = keyb.nextint(); 16

//Skip a line for clarity System.out.println(); numstudents++; while (numstudents < maxstudents); if (numstudents == maxstudents) --numstudents; return numstudents; // FindAverages() - Find the average for each // student public void findaverages() { int i, j, sum; averages = new int[numstudents]; for (i = 0; i < numstudents; i++) { sum = 0; for (j = 0; j < numexams; j++) sum += grades[i][j]; averages[i] = sum/numexams; 17

// WriteAverage() - Output the grades and // average for each student public void writegrades() { int i, j; // Print a heading System.out.println("Student\tExam1\t" + "Exam2\tExam3\tExam4\tAverage"); for (i = 0; i < numstudents; i++) { // Number each line, then print the grades // and the average for the next student System.out.print(i); for (j = 0; j < numexams; j++) System.out.print("\t" + grades[i][j]); System.out.println("\t" + averages[i]); CalcAverages.java // CalcAverages() - Calculate the term // averages for a class // The average is based on four exams public class CalcAverages { public static void main(string[] args) { Grades myclass = new Grades(); int nstudents; //Get the grades, find the averages and // print them nstudents = myclass.readgrades(); myclass.findaverages(); myclass.writegrades(); 18

Matrices A matrix (the plural is matrices) is a twodimensional array of numbers. While it has special uses in mathematics, it is extremely useful to be able to work with 2- and 3-dimensional arrays of data of various types. Matrix.java import java.util.scanner; public class Matrix { public final int numrows = 4, numcolumns = 4; private int[][] matrix; public Matrix() { matrix = new int[numrows][numcolumns]; 19

//read() - Read in a matrix public void read() { int i, j; Scanner keyb = new Scanner(System.in); for (i = 0; i < numrows; i++) { System.out.println("Enter row #" + (i+1) + "\t?"); for (j = 0; j < numcolumns; j++) { System.out.println("Enter x[" + i + "][" + j + "]"); matrix[i][j] = keyb.nextint(); // write() Write an i x j matrix void write() { int i, j; for (i = 0; i < numrows; i++) { for (j = 0; j < numcolumns; j++) System.out.print("\t" + matrix[i][j]); System.out.println(); 20

// multmatrix() Multiply this x b to get c public Matrix mult(matrix b) { Matrix c = new Matrix(); int i, j, k; for (i = 0; i < numrows; i++) for (j = 0; j < numcolumns; j++) { c.matrix[i][j] = 0; for (k = 0; k < numrows; k++) c.matrix[i][j] += matrix[i][k]*b.matrix[k][j]; return c; Using An Array of Objects Sometimes the array with which we are going to work is an array of objects. This is really not much different from working with an array of integers or doubles or Strings. 21

StudentRecord.java import java.util.scanner; public class StudentRecord { private String firstname; private String lastname; private int year; // Year of graduation private double gpa; // Accessors public String getfirstname() { return firstname; public String getlastname() { return lastname; public int getyear() { return year; public double getgpa() { return gpa; // Mutators public void setfirstname(string fn) { firstname = fn; public void setlastname(string ln) { lastname = ln; 22

public void setyear(int yr) { year = yr; public void setgpa(double gpav) { gpa = gpav; StudentRecords.java import java.util.scanner; public class StudentRecords{ public final int maxstudents = 15; private int numstudents; public StudentRecord[] students; public StudentRecords() { students = new StudentRecord[maxStudents]; for (int i = 0; i < maxstudents; i++) students[i] = new StudentRecord(); //Initially, no student records have been // entered numstudents = 0; 23

//ReadRecords() - Read in the input records public int readrecords() { Scanner keyb = new Scanner(System.in); String instring = new String(); int newyear; double newgpa; //Tell the user how to end the input System.out.println("Enter \"end\" to indicate" + " no more students"); // For each student, input the first and last // name, year of graduation and grade point // average for (numstudents = 0; numstudents < maxstudents; numstudents++) { System.out.println("First name\t?"); instring = keyb.next(); //If they entered "end", there aren't any // more records if (instring.equals("end")) break; students[numstudents].setfirstname( instring.trim()); System.out.print("Last name\t?"); instring = keyb.next(); students[numstudents].setlastname( instring.trim()); System.out.println("Year\t?"); newyear = keyb.nextint(); students[numstudents].setyear(newyear); System.out.println("Grade point average\t?"); newgpa = keyb.nextdouble(); students[numstudents].setgpa(newgpa); return numstudents; 24

//WriteRecord() - Enter the student record public void writerecord(int index) { // Make sure that all significant places print //Print the record, using four spaces for year // and gpa System.out.println( students[index].getfirstname() + "\t" + students[index].getlastname() + "\t" + students[index].getyear() + "\t" + students[index].getgpa()); import java.util.scanner; DeansList.java // DeansList() - Read Student records and print // the Data on studetns on the // Dean's List public class DeansList { public static void main(string[]args) { StudentRecords stud = new StudentRecords(); int i, numstudents = 0; //Input the data numstudents = stud.readrecords(); 25

//Print the data for students with // gpa's of 3.5 or higher for (i = 0; i < numstudents; i++) if (stud.students[i].getgpa() >= 3.5) stud.writerecord(i); 26