Teaching%20Session%2010

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

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 1D04 Teaching Session 10 © 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 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 3 recap: Arrays s 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 s 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++; } © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 5 A new kind of loop s 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 s 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 = © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 7 Two-Dimensional Arrays s 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 s Let us assume that we have an array of this form, where each element is an integer. s 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 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 9 Two-Dimensional Arrays s Let us assume that we have an array of this form, where each element is an integer. s What algorithm could we construct to total each row and each column?...
View Full Document

{[ snackBarMessage ]}

Page1 / 40

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

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online