17arrays1_3014

17arrays1_3014 - Lecture File 17 COP 3014 October 22, 2009...

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

View Full Document Right Arrow Icon
Lecture File 17 COP 3014 October 22, 2009 A. Ford Tyson 1 ± One Dimensional Arrays – basic use of 1D arrays COP 3014 Lecture File 17 1 – passing as parameters – parallel arrays – frequency counting – sub-array processing Copyright 1997-present, Ann Ford Tyson ± a data structure ; contains a group of elements ± ADT list ("abstract data type") ± a structured data type array 2 ± maximum number of elements is fixed ± elements must all be same data type ± elements have an order (position in list) ± access elements via numeric index (subscript) and [ ] brackets ± e.g. list of 50 state names Allen Telescope Array (ATA) 3
Background image of page 1

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

View Full DocumentRight Arrow Icon
Lecture File 17 COP 3014 October 22, 2009 A. Ford Tyson 2 Example: why do we need arrays? ± Assume we need to store 50 integer game scores and do some data analysis on them ± OPTION 1: declare 50 int variables ? int score1 score2 score3 score50 4 int score1, score2, score3, … score50; ± OPTION 2: store them in one array inflexible tedious hard to access them all e.g. need 50 statements to read each one One-Dimensional Arrays cont. const int NUMSCORES = 50; int main ( ) 5 { int scores [ NUMSCORES ]; basetype number of elements ( size) integral, constant scores [ 0 ] scores [ 1 ] 50 consecutive memory locations 6 scores [ 49 ] scores index is offset from base index
Background image of page 2
Lecture File 17 COP 3014 October 22, 2009 A. Ford Tyson 3 Example Task ± Assume we have a list of integer scores in a text file named gameScores.txt ± Print the mean score sum of scores / number of scores 7 ± Print each score's difference from the mean (a rough statistic) – if mean is 1000, and a score is 1200, the difference score is +200 – if mean is 1000, and a score is 950, difference score is -50 Example Program arrayEg1.cpp We will now switch to our IDE and examine this program in lecture 8 the program is available on the class web site Food for thought ± why not skip using the array, declare one score variable, read in each value, process it and then discard it ? would have to read the file twice 9 – would have to read the file twice (very inefficient) ± what would you change to make this program modular (using functions) ?
Background image of page 3

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

View Full DocumentRight Arrow Icon
Lecture File 17 COP 3014 October 22, 2009 A. Ford Tyson 4 Range Error ± C++ does not check array indices as the program runs to see if they are valid ± range error: using an array index which is out of range for a particular array 10 ± here, valid range is 0 thru 49 ± range errors can overwrite important data in your program, can even overwrite OS code and force you to restart the machine E.g. OFF-BY-ONE Error What happens?
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/14/2011 for the course COP 3014 taught by Professor Tyson during the Fall '10 term at FSU.

Page1 / 14

17arrays1_3014 - Lecture File 17 COP 3014 October 22, 2009...

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

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