CS 180 Problem Solving and OO Programming Fall 2010

CS 180 Problem Solving and OO Programming Fall 2010

CS 18000 Problem Solving and Object Oriented Programming Spring 2019 Section LE2 Week 5: Lecture 8 February 4. 2019 Slides updated: 12:17pm, February 4. 2019 Aditya Mathur Professor, Department of Computer Science Purdue University West Lafayette, IN, USA ps://www.cs.purdue.edu/homes/apm/courses/CS180_Java/ CS180Spring2019/ Review Repetitive computation Loop design: while-statement Loop design: for-statement 2/4/2019 Aditya Mathur. CS 180. Spring 2019.

Week 5 2 This week Arrays: Declaration, creation, initialization Data analysis: min, max, average, sort 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 3 Request and Announcement Posts on campuswire Extra Supplementary Instruction session: Wednesday, February 6 from 5:30 to 6:20pm WALC B093. Problem solving session: Sunday Feb 10, 2019. 6pm LWSN 3102 All are welcome

2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 4 Arrays 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 5 Arrays: What are these? A homogeneous collection of variables and objects. All elements of an array are of the same type. Examples: Array of integers where each integer represents the age

of a patient. Array of cars where each element of the array denotes a specific car. Array of flowers where each element of the array denotes a flower. 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 6 Arrays: When to use? When there is a need to retain data in memory for efficient processing. Data is created once and used many times. Examples: Array of flights: Search for a specific flight Array of cars: Search for a specific car, or find the average price. Array of laptops: find the cheapest laptop 2/4/2019

Aditya Mathur. CS 180. Spring 2019. Week 5 7 Declaring an Array Indicates age is an array variable int [] age; /* age refers to an array of integers; e.g. age of people in a database*/ double [] weight; /* weight refers to an array of doubles; e.g. weights of items shipped*/ String [] name; /* name refers to an array of elements each of type String; e.g., names of people in a database*/ 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5

8 Declaring an Array of objects [More on this in week 6] Bird [] bird; /* Bird refers to a class; bird is an array where each element is an object of type Bird. */ Aircraft [] fleet; /* Aircraft refers to a class; fleet is an array where each element is an object of type Aircraft. */ 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 9 Creating an Array

int [] age=new int[10]; /* age is an array of 10 elements each of type int*/ double [] weight=new double[100]; /* weight is an array of 100 elements each of type double*/ String [] name=new String[15]; /* name is an array of 15 elements each of type String*/ Aircraft [] fleet=new Aircraft[150]; /* fleet is an array of 150 elements each of type Aircraft*/ 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 10 Single dimensional array: Example int [] cloudyDays=new int [12] // average cloudy days/month Anchorage, Alaska 0 1

2 3 4 5 6 7 8 9 10 11 [19 18 18 18 20 20 22 21 21 21 20 21 ] cloudyDays[0] cloudyDays[5] 2/4/2019 Index Data

cloudyDays[11] Aditya Mathur. CS 180. Spring 2019. Week 5 11 Accessing an array element: general syntax Name of the array Index, starts from 0, must be an int name [expression] Examples: Note: First element of an array is located at index 0. age[i]; // 0<=i<10 fleet[k]; // 0<=k<150

weight[i+j]; // 0<=(i+j)<100 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 12 Accessing an array element int [] age=new int[10]; int p= age[5]; /* p gets element at index 5 of array age*/ double [] weight=new double[100]; double hisWeight=weight[i]; /* hisWeight gets the element of weight at index i*/ 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 13

Assigning a value to an array element int [] age=new int[10]; age[i]=15; /* (i+1)th element of age becomes 15. */ int [] age={-15, 20, 30} // All elements initialized double [] weight=new double[100]; weight[k]=weight[k]-10; /* (k+1)th element of the weight is decremented by 10.*/ 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 14 Iterating through elements of an array In many problems we need to iterate through all or a few of the elements of an array. Such an iteration is accomplished by using a for or a while loop. 2/4/2019

Aditya Mathur. CS 180. Spring 2019. Week 5 15 Problem 1: Initialize an array of elements // Initialize an array of n elements to random numbers // between 0 and 1. final int n=100;// Array size double [] rnum=new double [n]; // Create array to hold numbers for( int i=0; i

Problem 2: Initialize an array (read from keyboard) Scanner s=new Scanner(System.in); int [] a=new int [10]; for( i=0; i<10; i++){ a[i]=s.nextInt(); // Sets a[i] to number entered. } // End of for 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 17 Problem 3: Find average of numbers in an array final int n=100;// Array size double [] rnum=new double [n]; // Create array to hold numbers for( int i=0; i

for( int i=0; i=0){ pos=pos+1; // Increment positive count }else{

neg=neg+1; // Increment negative count } }// End of for 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 19 Problem 5: Search in an array: Step 1 initialize String [] wList={"Emilio", "Semion", Ziggy"}; Scanner s=new Scanner (System.in); String wanted=s.next(); // Get name to be searched boolean found=false; // Not yet found 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 20

Problem 5: Search in an array: Step 2 set up loop boolean found=false; // Not yet found int next=0; // Points to the first element in wanted list while(???) { if(wanted.equals(wList[nextItem])) // Compare found=true; // If equal then we have found the wanted! else next=next+1; // else point to the next item on the list }//End of loop 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 21 Problem 5: Search in an array: Step 3 complete loop condition int next=0; // Points to the first element in wanted list boolean found=false; // Not yet found

while(next

Aditya Mathur. CS 180. Spring 2019. Week 5 23 Problem 6: Sort an array in ascending order Example: Input array of integers: [5 4 -1 2] Sorted array of integers: [-1 2 4 5] 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 24 Problem 6: Sort algorithm: Bubble sort (Compare and exchange) Pass 1 Step

1 5 Pass 2 Step 2/4/2019 2 4 End of pass 1 3 4 4 4 5

-1 -1 -1 -1 5 2 2 2 2 5 1

2 4 -1 Total 3 steps in Pass 1 End of pass 2 -1 -1 4 2 2 2

4 5 5 5 Aditya Mathur. CS 180. Spring 2019. Week 5 Total 2 steps in Pass 2 25 Problem 6: Sort algorithm: Bubble sort Pass 3 Step 2/4/2019 1

End of pass 2 -1 -1 2 2 4 4 5 5 Total 1 step in Pass 3 Aditya Mathur. CS 180. Spring 2019. Week 5

26 Problem 6: Sort algorithm: Bubble sort: Analysis Given: Array of n elements Number of passes needed: Number of steps (comparisons) in Pass 1: Pass 2: . . Pass i: Total number of steps: 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 27

Problem 6: Sort algorithm: Bubble sort: Warning Bubble sort is not a recommended algorithm for large arrays (e.g. an array of, say, 100 elements). Better algorithms: Insertion sort Merge sort Quick sort Challenge: Code the bubble sort algorithm (on your own). 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 28 Arrays: Multidimensional 2/4/2019

Aditya Mathur. CS 180. Spring 2019. Week 5 29 Example MEAN MONTHLY CLOUDY DAYS IN ARIZONA 0 1 2 3 4 FLAGSTAFF PHOENIX TUCSON WINSLOW YUMA 0 J

12 10 10 12 9 1 F 11 9 9 10 6 2 M 12 8 9 9 6

3 A 9 6 6 7 4 4 M 7 3 4 5 2 5 J 4 2 3 4

1 6 J 9 4 9 8 3 7 A 8 4 7 6 3 8 S 5 3

4 4 2 rows 9 O 7 4 5 6 3 10 11 N D 8 11 6 9 6 10 8 10 5 8

columns int [][] cloudyDays=new int [5][12] What is the value of cloudyDays[1,8]? 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 30 Arrays: Typical errors Index out of bounds [Run time error] int [] a=new int [10]; a[i]=x; // i is greater than 9 or less than 0 Element not initialized [Compile time error] String [] name; name[i]=Bob; // element not initialized

2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 31 Week 5: February 4-8, 2019 Hope you enjoyed this week! 2/4/2019 Aditya Mathur. CS 180. Spring 2019. Week 5 32

Recently Viewed Presentations

  • Materials - DTOTW

    Materials - DTOTW

    Either way a table like this has to be remembered - and that is much easier to do if you understand what the differences between two named materials is - and that comes from knowing how the structure of hardwoods...
  •  Gender and Traits Selection prior to conception :

    Gender and Traits Selection prior to conception :

    August 2001 - US House of Representatives passes legislation to ban human cloning. September 2001 - Acting Chair of the Ethics Committee of the American Society for Reproductive Medicine (ASRM) writes a letter to a fertility expert condoning sex selection...
  • Factors that Affect the Rate of a Reaction - Weebly

    Factors that Affect the Rate of a Reaction - Weebly

    Collision Theory. If you have a situation involving two species they can only react with each otherif they come into contact with each other. They first have to collide, and then they may react. ... Factors that Affect the Rate...
  • NURS 2410 Unit 1 - Faculty Site Listing

    NURS 2410 Unit 1 - Faculty Site Listing

    Sperm penetration of an ovum. A, The sequential steps of oocyte penetration by a sperm are depicted moving from top to bottom. B, Scanning electron micrograph of human sperm surrounding a human oocyte (750οΎ—). The smaller spherical cells are granulosa...
  • Human-Computer Interaction for Tech Execs

    Human-Computer Interaction for Tech Execs

    Guest Lectures by Inventors. 2019 confirmed: Bill Atkinson, contributor to many early Apple Lisa and Machintosh interactions, including: the marching ants selection, the menu bar, the Selection lasso, FatBits, MacPaint, HyperCard pull-off menus, and many others
  • AN-MSI: An NSF-Sponsored Collaborative Project for Better ...

    AN-MSI: An NSF-Sponsored Collaborative Project for Better ...

    AN-MSI: An NSF-Sponsored Collaborative Project for Better Networking Dave Staudt AN-MSI Project Director [email protected]
  • Corporate Finance - pages.stern.nyu.edu

    Corporate Finance - pages.stern.nyu.edu

    Step 3: Estimate a probability of bankruptcy at each debt level, and multiply by the cost of bankruptcy (including both direct and indirect costs) to estimate the expected bankruptcy cost. In practice, analysts often do the first two steps but...
  • Hess - Free

    Hess - Free

    Interestingly enough, female subjects tested showed a more positive change in pupil dilation for slides of a baby, a mother and baby, a male "pinup" figure, while the male subjects favored a female "pinup" figure and a landscape.