This preview shows pages 1–11. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full 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 TwoDimensional Arrays s A very common data structure is a 2D 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 TwoDimensional 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 TwoDimensional 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
This note was uploaded on 12/05/2010 for the course MATH 1Z04 taught by Professor Childs during the Spring '08 term at McMaster University.
 Spring '08
 CHILDS

Click to edit the document details