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

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

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

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

Arrays (2)

CGS 3460, Lecture 23Mar 3, 2006Hen-I Yang

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

Quiz 2

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

Quiz 2 Grade Distribution

9

23

19

16

6 6

0

5

10

15

20

25

>90 >80 >70 >60 >50 <=50

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

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

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

Hints on Homework 3 2 variables each for 5 places of interest. 2 variables each for 5 possible housing. StreetDist (x, y) = |xS1 – xS2| + |yS1 – yS2| 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?

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

Hints on Homework 3 What are the “necessary” data needed to keep a

round? 2 variables each for 5 places of interest. 2 variables each for 5 possible housing. StreetDist (x, y) = |xS1 – xS2| + |yS1 – yS2| 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

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

StreetDist (x, y) = |xS1 – xS2| + |yS1 – yS2| 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

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

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; }}

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

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

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

Previously… type conversion, casting Introduction to array

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

Agenda

Arrays One-dimension array Multi-dimension arrays

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

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 typeElements Index (Subscript)

Close usage between array and for loop

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

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

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

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; }}

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

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; }}

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

Multidimension Array

Row major vs. Column major Remember the notation: m[3][5] not m[3,5] Use of nested loop for multidimension arra

y Initialization of multidimension array const modifier Can I assign an array to another variable?

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

Summary

Hints on Homework 3 Array One-dimension array Multidimension array

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

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)

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

Pickup your Quiz 2

Last name: K-ZLast name: A-J