130 likes | 313 Views
Arrays. INTRODUCTION TO ARRAYS. Just as with loops and conditions, arrays are a common programming construct and an important concept Arrays can be found in most high-level programming languages, such as C, and offer a simple way of grouping like variables for easy access.
E N D
INTRODUCTION TO ARRAYS • Just as with loops and conditions, arrays are a common programming construct and an important concept • Arrays can be found in most high-level programming languages, such as C, and offer a simple way of grouping like variables for easy access. • Arrays in C share a few common attributes: • • Variables in an array share the same name • • Variables in an array share the same data type • • Individual variables in an array are called elements • • Elements in an array are accessed with an index number
Creating One-Dimensional Arrays Arrays in C are created in similar fashion to other variables. • intIArray[10]; • float fAverages[30]; //Float data type array with 30 elements • double dResults[3]; //Double data type array with 3 elements • short sSalaries[9]; //Short data type array with 9 elements • char cName[19]; //Char array - 18 character elements and one null character
Initializing One-Dimensional Arrays • In C, memory spaces are not cleared from their previous value when variables or arrays are created. Because of this, it is generally good programming practice to not only initialize your variables but to also initialize your arrays. • There are two ways to initialize your arrays: within the array declaration and outside of the array declaration. • In the first way, you simply assign one or more comma-separated values within braces to the array in the array’s declaration. • intiArray[5] = {0, 1, 2, 3, 4};
Initializing One-Dimensional Arrays • intiArray[5] = {0}; int x; intiArray[5]; for ( x = 0; x < 5; x++ ) iArray[x] = 0;
Searching One-Dimensional Arrays for ( x = 0; x < 5; x++ ) { if ( iArray[x] == iValue ) { iFound= x; break; } } //end for loop
TWO-DIMENSIONAL ARRAYS Two-dimensional arrays are created similar to one-dimensional arrays, but with one exception: • two-dimensional arrays must be declared with two separate element numbers (number of columns and number of rows) • intiTwoD[3][3]; • The array declaration above creates a total of 9 elements (remember that array indexes start with number 0). • Two-dimensional arrays are accessed with two element numbers, one for the column and one for the row.
Initializing Two-Dimensional Arrays You can initialize a two-dimensional array in a couple of ways. First, you can initialize a twodimensional array in its declaration • intiTwoD[3][3] = { {0, 1, 2}, {0, 1, 2}, {0, 1, 2} }; Each grouping of braces initializes a single row of elements. For example, iTwoD[0][0] gets 0, iTwoD[0][1] gets 1, and iTwoD[0][2] gets 2.
Or you can use nested loops intiTwoD[3][3]; int x, y; //intialize the 2-d array for ( x = 0; x <= 2; x++ ) { for( y = 0; y <= 2; y++ ) iTwoD[x][y] = ( x + y ); } //end outer loop //print the 2-d array for ( x = 0; x <= 2; x++ ) { for( y = 0; y <= 2; y++ ) printf("iTwoD[%d][%d] = %d\n", x, y, iTwoD[x][y]); } //end outer loop
Searching Two-Dimensional Arrays • The concept behind searching a two-dimensional array is similar to that of searching a single dimension array. • You must receive a searchable value, such as user input, and then search the array’s contents until a value is found or the entire array has been searched without a match. • When searching two-dimensional arrays, however, you must use the nested looping
Searching a 2 Dimensional Array //search the 2-D array for ( x = 0; x <= 2; x++ ) { for( y = 0; y <= 2; y++ ) { if ( iTwoD[x][y] == iValue ) { iFound= 1; iFoundAt[0] = x; iFoundAt[1] = y; break; } //end if } //end inner loop } //end outer loop if ( iFound == 1 ) printf("\nFound value in iTwoD[%d][%d]\n", iFoundAt[0], iFoundAt[1]); else printf("\nValue not found\n");
Tic-Tac-Toe There are a total of four functions, including the main() function, that are used to build the tic-tac-toe game. FUNCTIONS USED IN THE TIC-TAC-TOE GAME • main() Initializes array and prompt players for X and O placement until the game is over • displayBoard() Clears the screen and displays the board with X and O placements • verifySelection() Verifies square is empty before placing an X or O inside the square • checkForWin() Checks for a win by X or O or a tie (cat) game
SUMMARY • An array is a grouping of contiguous memory segments. • Variables in an array share the same name. • Variables in an array share the same data type. • Individual variables in an array are called elements. • Elements in an array are accessed with an index number. • Assigning the single numeric value of 0 in an array declaration will, by default, assign all array elements the value of 0. • Elements in a character array hold characters plus a special null termination character, which is represented by the character constant '/0'. • When creating character arrays, be sure to allocate enough room to store the largest character sequence assignable. Also, remember to allow enough room in the character array for storing the null character ('\0'). • Use looping structures, such as the for loop, to iterate through each element in an array. • When C encounters the break keyword in a loop, it moves program control to the next statement outside of the loop. • C implements two-dimensional arrays as single-dimension arrays with pointers to other single-dimension arrays. • The easiest way to understand or think about two-dimensional arrays is to visualize a table with rows and columns. • Nested loops are necessary to search through a two-dimensional array.