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

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

View Full Document Right Arrow Icon
Engineering 1D04 Teaching Session 10
Image of page 1

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

View Full Document Right Arrow Icon
© 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];
Image of page 2
© 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
Image of page 3

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

View Full Document Right Arrow Icon
© 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 }
Image of page 4
© 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++; }
Image of page 5

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

View Full Document Right Arrow Icon
© 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 }
Image of page 6
© 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 =
Image of page 7

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

View Full Document Right Arrow Icon
© 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
Image of page 8
© 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
Image of page 9

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

View Full Document Right Arrow Icon
© 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.
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern