170 likes | 192 Views
Introduction to Computers and Programming. Class 21 Arrays Professor Avi Rosenfeld. Midterm #2 Review. Class Average 83. What is an Array?. It’s a group of the same type variables grouped into one name
E N D
Introduction to Computers and Programming Class 21 Arrays Professor Avi Rosenfeld
Midterm #2 Review • Class Average 83
What is an Array? • It’s a group of the same type variables grouped into one name • More specifically, it’s a group of memory locations for variables of the same type and specified by the same name • It has special conventions for setting it up and for its use but it makes dealing with related variables much easier
What exactly are we talking about?! • Let’s say you have ten students and you want to save the grades for each of them for use throughout your program (i.e. we need to remember every grade not just loop and count them or average them, etc.) • Could do it the hard way: • Set up ten variables called iStudentOneGrade, iStudentTwoGrade, iStudentThreeGrade, etc. • Very difficult to use and manipulate • Instead, could use an array to do it the easy way…
Parts of the array • The array has some terminology we haven’t seen yet • Elements • Index (or Position Number) • Subscript • Zeroth element
Elements • Refers to the number of individual items represented by the array. For example, • an array of 10 integers is said to have 10 elements • an array of 15 floats has 15 elements • an array of 5 characters has 5 elements • and so on…
Index (Position Number or Subscript) • Refers to one particular element in the array • Also known as position number or, more formally, as a subscript • The first element in an array is represented by an index, position number, or subscript of 0 (zero). For example, • arrStudentGrades[ 0 ] • This first position is known as the zeroth element • The second position is referred to by • arrStudentGrades[ 1 ]
Figuring out the array positions • In C, arrays’ elements start out at index 0 and go up to (the number of elements – 1) • For example, our array of 10 student grades filled in with grades might look like:
Array positions (cont’d) • We can access them by specifying the array name followed by square brackets with the index number between them. For example, printf (“The third student’s grade is “%d”\n”, arrStudentGrades[ 2 ] ); • Would print only the third integer spot in the array (remember 0 is the first, 1 is the second, and 2 is the third element’s index (position number, subscript) • The output would look like the following: The third student’s grade is 99
The Off-by-One Error • The index scheme starting at 0 may be initially confusing • E.g. the seventh element is not the same as element seven • Seventh element has index or subscript of 6 and element 7 has subscript of 7 • This is the cause of many off-by-one errors so study the concept carefully
Declaring an array • You must specify the type of elements in the name and the number of them so the computer knows how much memory to set aside for the array • Can declare almost like other variables (except for the brackets and number of elements): int iMyFirstArray[ 15 ] ; or int iMyFirstArray[ 15 ], iMySecondArray[ 20 ]; • Can even use a #define constant to set the size of the array #define GRID_ROWS_MAX 8 int arrGridRows[ GRID_ROWS_MAX ] ;
The Limit of Arrays • You cannot have a variable for the size • ie. int x[y] is not legal if y is a variable. • Arrays require the programmer to know in advance how much space (s)he will need • Other structures (vectors, linked lists, stacks, and queues) which are not covered by this course deal with this limitation.
Initializing an Array • You can initialize when you declare it, as you do with other variables • Syntax is slightly different, as you are now initializing more than one element at a time • One way at declaration, using initializers: int iMyFirstArray[ 5 ] = { 0, 0, 0, 0, 0 } ; • Note the braces around the initial zeroes which themselves are separated by commas
Initializing an Array (cont’d) • If you specify fewer initializing values than you have elements, all the rest are initialized to a value of 0! For examples, int iMyFirstArray[ 5 ] = { 0 }; would set all elements to 0 int iMyFirstArray[ 5 ] = { 4 }; would set the zeroth element to 4 and the rest to 0!
Initializing an array without specifying size • You can also initialize and set the number of elements with the same step: int iMyFirstArray[ ] = { 1, 2, 0, 4, -2 } ; • Note there is NO size specified; C automatically makes it 5 elements since that’s how many initial values you gave it
Initializing array with a for loop • After declaring an array, you can initialize it in the body of your program by using a for loop: int i = 0, iMyFirstArray[ 5 ] ; /* size is 5*/ for ( i = 0 ; i <= 4 ; i++ ) { iMyFirstArray[ i ] = 0 ; } /* end for i */ • Note the upper bound is 4, not 5! That is you loop through 0 to 4 to initialize an array with 5 elements
Accessing elements with for loop • Can use a for loop to print the contents of an array #define SIZE 5 int i = 0, iMyFirstArray[ SIZE ] ; /* go through all element index */ for ( i = 0 ; i < SIZE ; i++ ) { printf( “%d”, iMyFirstArray[ i ]) ; } /* end for i */