Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays (2) CGS 3460, Lecture 23 Mar 3, 2006 Hen-I Yang.

Similar presentations


Presentation on theme: "Arrays (2) CGS 3460, Lecture 23 Mar 3, 2006 Hen-I Yang."— Presentation transcript:

1 Arrays (2) CGS 3460, Lecture 23 Mar 3, 2006 Hen-I Yang

2 Quiz 2 Average: 73.47 Std Dev: 14.16 Max: 94 Min: 93 39.2%  100% of HW2 83.5%  90% or above

3 Announcement Textbook is now in library as a 2-hour course reserve The details of the Optional Final Exam will be announced next Wednesday after Quiz3 Hint on Homework 3 Guest lecturer next Monday

4 Hints on Homework 3 2 variables each for 5 places of interest. 2 variables each for 5 possible housing. StreetDist (x, y) = |x S1 – x S2 | + |y S1 – y S2 | StreetDist ( home-school ) + StreetDist ( home-clinic ) + StreetDist ( home-chess club ) + StreetDist ( home- library ) + StreetDist ( home-soccer field ) That’s a total of 10 variables you need to keep for each housing DO I HAVE TO WRITE A PROGRAM WITH 70 VARIABLES?

5 Hints on Homework 3 What are the “necessary” data needed to keep around? 2 variables each for 5 places of interest. 2 variables each for 5 possible housing. StreetDist (x, y) = |x S1 – x S2 | + |y S1 – y S2 | StreetDist ( home-school ) + StreetDist ( home-clinic ) + StreetDist ( home-chess club ) + StreetDist ( home- library ) + StreetDist ( home-soccer field ) That’s a total of 10 variables you need to keep for each housing

6 StreetDist (x, y) = |x S1 – x S2 | + |y S1 – y S2 | StreetDist ( home-school ) + StreetDist ( home-clinic ) + StreetDist ( home- chess club ) + StreetDist ( home-library ) + StreetDist ( home-soccer field ) double sum_street_dist = 0; double street_dist; int x, y; int i; for (i = 1; i <= 5; i++) { switch(i) { case 1: x = school_x; y = school_y; break; case 2: x = clinic_x; y = clinic_y; break; …. } street_dist=((home_x > x)?(home_x – x):(x - home_x)) + ((home_y > y)?(home_y – y):(y - home_y)); sum_street_dist = sum_street_dist + street_dist; } A

7 There are five housing. Can I use the same trick again? Yes! double min_street_dist = 3.14e+30; double sum_street_dist; int home_x, home_y; int j; int current_winner; for (j = 1; j <= 5; i++) { switch(j) { case 1: home_x = house1_x; home_y = house1_y; break; case 2: home_x = house2_x; home_y = house2_y; break; …. } A if (sum_street_dist < min_street_dist) { min_street_dist = sum_street_dist; current_winner = j; }

8 Only keep the variables you need for later use. Try reuse same variables for similar calculations, especially those in the loops. In case of “maximize” and “minimize” type of problem, you can always only keep the info of the winner. In most of the meaningful programs, loops are your best friend. We reduce from 85 to 47 statements by using for loops and switch. If using arrays, we can eliminate 30 more statements to a total of 17. (But you’re not allowed to do that in this homework.) Can you make this work for problem 2 with only minor modification? Hints

9 Previously… type conversion, casting Introduction to array

10 Agenda Arrays One-dimension array Multi-dimension arrays

11 Array Aggregate variable vs. Scalar variables C support 2 kinds of aggregate variable: Arrays and structures A data structure for multiple data values, all of the same type  Elements  Index (Subscript) Close usage between array and for loop

12 More about Arrays 0-based index Out of bound problem Traversing through elements Array initialization sizeof(a)/sizeof(a[0]) Supermarket problem in homework 2

13 When not using array. double min_street_dist = 3.14e+30; double sum_street_dist; int home_x, home_y; int j; int current_winner; for (j = 1; j <= 5; i++) { switch(j) { case 1: home_x = house1_x; home_y = house1_y; break; case 2: home_x = house2_x; home_y = house2_y; break; case 3: home_x = house3_x; home_y = house3_y; break; case 4: home_x = house4_x; home_y = house4_y; break; case 5: home_x = house5_x; home_y = house5_y; break; } A if (sum_street_dist < min_street_dist) { min_street_dist = sum_street_dist; current_winner = j; }

14 Why use Array? double min_street_dist = 3.14e+30; double sum_street_dist; int home_x, home_y; int j; int current_winner; for (j = 1; j <= 5; i++) { home_x = house_x[j]; home_y = house_y[j]; A if (sum_street_dist < min_street_dist) { min_street_dist = sum_street_dist; current_winner = j; }

15 Multidimension Array Row major vs. Column major Remember the notation: m[3][5] not m[3,5] Use of nested loop for multidimension array Initialization of multidimension array const modifier Can I assign an array to another variable?

16 Summary Hints on Homework 3 Array One-dimension array Multidimension array

17 Before you go Read Chapter 8. Exercise: 8.8, 8.9, 8.11 Homework 3 due Mar 7, Tuesday at 11:59 pm Quiz 3 will be held on Mar 8th (Wed). Quiz 3 includes  homework 3,  textbook up to Sec 7.1, and  slides up to last Friday (2/24)

18 Pickup your Quiz 2 Last name: K-ZLast name: A-J


Download ppt "Arrays (2) CGS 3460, Lecture 23 Mar 3, 2006 Hen-I Yang."

Similar presentations


Ads by Google