Teaching%20Session%2010

# Teaching%20Session%2010 - Engineering 1D04 Teaching Session...

• Notes
• GeneralFreedomMole9553
• 40

This preview shows pages 1–11. Sign up to view the full content.

Engineering 1D04 Teaching Session 10

This preview has intentionally blurred sections. Sign up to view the full version.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 1 recap: Arrays const int numScores = 10; int [] score = new int[numScores]; string [] name = new string[numScores];
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 2 recap: Arrays const int numScores = 10; int [] score = new int[numScores]; string [] name = new string[numScores]; numScores elements in both arrays always points to element indexed as 0 always points to element indexed as 0

This preview has intentionally blurred sections. Sign up to view the full version.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 3 recap: Arrays square4 How do we work with elements in the array? int i, total; total = 0; for (i = 0; i < numScores; i++) { //do what we want to with element i total += score[i]; //for example }
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 4 recap: Arrays square4 Alternative int i, total; total = 0; i = 0; while (i < numScores) { //do what we want to with element i total += score[i]; //for example i++; }

This preview has intentionally blurred sections. Sign up to view the full version.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 5 A new kind of loop square4 Another (very safe) construct for loops. int total; total = 0; foreach (int scoreValue in score) { //do what we want to with element total += scoreValue; //for example }
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 6 A new kind of loop square4 Another (very safe) construct for loops. int total; total = 0; foreach (int scoreValue in score) { //do what we want to with element total += scoreValue; //for example } type must match type of each element in the array - the variable is local to the loop and can have any name it is the element in the array - not the index of the element element cannot appear on the left of the =

This preview has intentionally blurred sections. Sign up to view the full version.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 7 Two-Dimensional Arrays square4 A very common data structure is a 2-D array. A mathematical matrix is a good example. a 1,1 a 1,2 a 1,3 a 1,4 a 2,1 a 2,2 a 2,3 a 2,4 a 3,1 a 3,2 a 3,3 a 3,4 traditionally a i,j where i gives the row and j gives the column
© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 8 Two-Dimensional Arrays square4 Let us assume that we have an array of this form, where each element is an integer. square4 What algorithm could we construct to total each row and each column? a 1,1 a 1,2 a 1,3 a 1,4 a 2,1 a 2,2 a 2,3 a 2,4 a 3,1 a 3,2 a 3,3 a 3,4

This preview has intentionally blurred sections. Sign up to view the full version.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 9 Two-Dimensional Arrays square4 Let us assume that we have an array of this form, where each element is an integer. square4 What algorithm could we construct to total each row and each column? a 1,1 a 1,2 a 1,3 a 1,4 r 1 a 2,1 a 2,2 a 2,3 a 2,4 r 2 a 3,1 a 3,2 a 3,3 a 3,4 r 3 c 1 c 2 c 3 c 4 The first thing to realize is we are trying to calculate elements of two new 1-D arrays, r and c. Each element of r and each element of c is an integer.
This is the end of the preview. Sign up to access the rest of the document.
• Fall '08
• CHILDS
• Array, David Das, Ryan Lortie, Alan Wassyng

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern