Download presentation
Presentation is loading. Please wait.
Published by砚壑宛 吴 Modified over 6 years ago
1
Arrays 1 2 3 4 5 Arrays are data structures that allow us to store data of the same type in contiguous memory locations. That was a pretty dense statement! To better understand the concept of an array, think back to the last time you picked up mail in the Science Center basement or your house's mailroom. An array is simply a block of contiguous space in memory (the mail center) that has been partitioned into identically-sized chunks (mailboxes). Each chunk can store a certain amount of data (mail) that can be accessed by an index number (mailbox number). Note that array indices in C always start at 0!
2
65 87 30 Creating an Array 1 2 <data type> name[<size>];
Example: int temperature[3]; temperature[0] = 65; temperature[1] = 87; temperature[2] = 30; OR int temperature[] = { 65, 87, 30 }; 1 2 Declare an array by specifying the data type it will store, its name, as well as its size. Here, we declare an array of 3 ints called temperature and load it with values. Alternatively, you can declare and initialize the array in a single step (in which case stating the size is optional). Because arrays store data contiguously in memory, array size is fixed after array declaration. You are effectively asking the OS to reserve the appropriate number of contiguous chunks of memory for the array's elements. There's no guarantee that more memory adjacent to your array will be available for use later, so arrays cannot easily grow.
3
Accessing Array Elements
1 2 for (int i = 0; i < 3; i++) { printf("%d\n", temperature[i]); } Because each element is associated with an array index, we have random access to all of the elements in an array. In other words, we can access any element in a single step by indexing into the array. This is a big deal because algorithms like binary search depend on random access. This code prints out all elements of the temperature array.
4
#include <stdio.h> #include <cs50.h> #define CLASS_SIZE 30
int main(void) { // declare array int scores_array[CLASS_SIZE]; // populate array for (int i = 0; i < CLASS_SIZE; i++) printf("Enter score for student %d: ", i); scores_array[i] = GetInt(); } } Here's an example of the usage of an array to keep track of student scores. This code prompts the user to enter a score for each student. Scores are stored in scores_array.
5
Where's the bug? string class[3] = { "Sam", "Jess", "Kim" };
for (int i = 0; i <= 3; i++) { printf("%s\n", class[i]); } What's the index number of the last element in an array of size n?
6
Multidimensional Arrays
0,0 0,1 0,2 x x 1,0 1,1 1,2 char board[3][3]; board[1][1] = 'o'; board[0][0] = 'x'; board[2][0] = 'o'; board[0][2] = 'x'; o Arrays can be multidimensional. You can think of multidimensional arrays as "arrays of arrays." Here, we declare and initialize a 2-dimensional array tic-tac-toe board. Note that dimensions must be declared explicitly for multidimensional arrays! 2,0 2,1 2,2 o
7
Accessing Multidimensional Array Elements
// print out all elements for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) printf("%c", board[i][j]); printf("\n"); } This code uses nested for loops to print out all elements of the tic-tac-toe board array.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.