Lab 5 Happy Early Spring Break 1 Dimensional

Lab 5 Happy (Early) Spring Break!!!!!!


1 -Dimensional Arrays ● Think of an array as a container that can hold many items of a certain data type ● An array is a collection of items of the same data type stored consecutively in memory under one name ● You can access elements in an array by indexing just like we did with strings
![Array Declaration and Initialization int my_array[5]; int my_array[] = {1, 2, 3, 4, 5}; Array Declaration and Initialization int my_array[5]; int my_array[] = {1, 2, 3, 4, 5};](http://slidetodoc.com/presentation_image_h2/bc2014199bc0fc92520735729001ba3d/image-4.jpg)
Array Declaration and Initialization int my_array[5]; int my_array[] = {1, 2, 3, 4, 5}; int my_array[5] = {1, 2, 3, 4, 5}; //compiler will insert 0’s after 5 th element int my_array[8] = {1, 2, 3, 4, 5} ALL VALID

Types of Arrays ● Arrays can be of any data type, as long as all elements are of the same data type ● For example string my_array[6]; double your_array[8]; bool our_array[4];

Passing arrays to functions ● Arrays are always passed by reference- no & needed //prototype void use. Array(int arr[], int size 0); ● You cannot return an array from a function (a function’s return type cannot be an array) ○ This is okay, because since the array is passed by reference, you can directly modify it in the function

Passing arrays to functions ● When calling the function, argument is the array name //prototype / declaration void use. Array(int arr[], int size); int main() { const int size = 3; const int arr[size] = {1, 2, 3}; // call to use. Array(arr, size); return 0; }

Iterating through an array ● Careful not to go off the end of the array ● If the condition was i <= size (where size is 5) the program would try to access my_array[5] on the last iteration which could be some random value accessed in memory, since it is not part of our defined array

2 Dimensional Arrays ● You can visualize these like a matrix, or a game board! ○ ○ ALWAYS: arr[row][col] You’ll often see ‘col’ in place of column
![Initializing all to 0 ● 1 -dimensional array: ○ Int board[3] = {0}; ● Initializing all to 0 ● 1 -dimensional array: ○ Int board[3] = {0}; ●](http://slidetodoc.com/presentation_image_h2/bc2014199bc0fc92520735729001ba3d/image-10.jpg)
Initializing all to 0 ● 1 -dimensional array: ○ Int board[3] = {0}; ● 2 -dimensional array: ○ Int board[3][5] = {0}; ● Both of these initialize every element in the array to 0 ● THIS IS ONLY THE CASE WITH 0 ○ If any other number is between the brackets, it sets only the first element to that number, and sets all the rest to zero
![Initializing directly const int HEIGHT = 3; const int WIDTH = 4; int board[HEIGHT][WIDTH] Initializing directly const int HEIGHT = 3; const int WIDTH = 4; int board[HEIGHT][WIDTH]](http://slidetodoc.com/presentation_image_h2/bc2014199bc0fc92520735729001ba3d/image-11.jpg)
Initializing directly const int HEIGHT = 3; const int WIDTH = 4; int board[HEIGHT][WIDTH] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; ● You can also initialize all to the same thing, using nested loops
![When initializing. . . Which of these is invalid? int data[10][2]; int data[4][ ]; When initializing. . . Which of these is invalid? int data[10][2]; int data[4][ ];](http://slidetodoc.com/presentation_image_h2/bc2014199bc0fc92520735729001ba3d/image-12.jpg)
When initializing. . . Which of these is invalid? int data[10][2]; int data[4][ ]; int data[ ][3] = {{1, 2, 3}, {4, 5, 6}};
![When initializing. . . Which of these is invalid? int data[10][2]; int data[4][ ]; When initializing. . . Which of these is invalid? int data[10][2]; int data[4][ ];](http://slidetodoc.com/presentation_image_h2/bc2014199bc0fc92520735729001ba3d/image-13.jpg)
When initializing. . . Which of these is invalid? int data[10][2]; int data[4][ ]; -- Compile Error int data[ ][3] = {{1, 2, 3}, {4, 5, 6}};

How do we declare and initialize our own 2 -d array? ● Let’s say we want the values to increase : 1, 2, 3, 4, 5, 6, 7, 8, 9 1 2 3 4 5 6 7 8 9

How do we declare and initialize our own 2 -d array? ● Let’s say we want the values to increase : 1, 2, 3, 4, 5, 6, 7, 8, 9 int my_arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} 1 2 3 4 5 6 7 8 9 };

How do we iterate through a 2 -d array ● What if we want to print every element, like the picture? 1 2 3 4 5 6 7 8 9

How do we iterate through a 2 -d array ● What if we want to print every element, like the picture? ● We print each element in each row, and start a new line for each row so it looks like a matrix. for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; ++j) { cout << my_arr[i][j] << “ } cout << endl; } 1 2 3 “; 4 5 6 7 8 9

How do we iterate through a 2 -d array ● Now what if we want to print the values backwards, keeping the matrix form? int my_arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} 9 8 7 6 5 4 }; 3 2 1

How do we iterate through a 2 -d array ● Now what if we want to print the values backwards, keeping the matrix form? for (int i = 2; i >= 0; --i) { for(int j = 2; j >= 0; --j) { 9 8 7 6 5 4 3 2 1 cout << my_arr[i][j] << “ “; } cout << endl; }

Arrays Discussion Wrap-up ● If you still need more practice with arrays, thoroughly step through the examples from lecture ● Any questions?

Today’s Lab ● There are two parts to the lab: ○ ○ Exam practice: answer the question in lab 5. pdf with paper and pencil Lab assignment: practice using arrays

Exam Practice ● ● Write your answer to the question just like you would on the exam You have 10 minutes to solve the problem After 10 minutes, we will discuss the solution Remember, you do not need to submit this Exam Practice question, but you can use it to review for the next exam!

Exam Practice Solution for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (i == 0 || j == 0 || i == rows - 1 || j == cols - 1) { border. Array[i][j] = 0; } else { border. Array[i][j] = 1; } } }

Arrays Exercise ● Now complete the programming section of the lab assignment ● Feel free to ask any questions that you have ● To receive the 7 pts for the lab, you will need to submit: ○ ○ arrays. cpp to the autograder The grade from the autograder is your grade from the lab
- Slides: 24