EECS 183 DISCUSSION 7 Hannah Westra Upcoming Deadlines

  • Slides: 17
Download presentation
EECS 183 DISCUSSION 7 Hannah Westra

EECS 183 DISCUSSION 7 Hannah Westra

Upcoming Deadlines ◦ Assignment 3 Due Today (10/21) at 6: 00 pm ◦ Project

Upcoming Deadlines ◦ Assignment 3 Due Today (10/21) at 6: 00 pm ◦ Project 3 Due Next Friday (10/28) at 6: 00 pm ◦ Partners ◦ For projects 3 and 4, you are allowed to have a partner ◦ Ohhi ◦ http: //0 hh 1. com/ ◦ Get started early!

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}; int my_array[5] = {1, 2, 3, 4, 5}; int my_array[8] = {1, 2, 3, 4, 5}; //compiler will insert 0’s after 5 th elt ALL VALID

Passing arrays to functions ◦ Arrays are always passed by reference – no &

Passing arrays to functions ◦ Arrays are always passed by reference – no & needed void use. Array(int arr[], int size); //prototype ◦ 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 ◦ const Modifier? ◦ void print. Results(const int arr[], int size)

Iterating through Array string hamsters[3] = {“Pepé”, ”Hammy”, “Fluffy” }; ◦ What does hamsters

Iterating through Array string hamsters[3] = {“Pepé”, ”Hammy”, “Fluffy” }; ◦ What does hamsters refer to? What will print if you cout << hamsters ? ◦ The memory address of the first element of hamsters[0] ◦ Example output: 0 x 00387 A 00 ◦ Use loops to access elements in arrays for (int i = 0; i < 3; i++) { cout << hamsters[i] << endl; }

Practicing Using 1 D Arrays 1) Write a function that takes in an array

Practicing Using 1 D Arrays 1) Write a function that takes in an array of type integer, and prints the sum of the elements 2) Write a function that takes in an array of type integer, and replaces any negative number with its absolute value 3) Write a function that takes in an array of type boolean, and returns whether the array is more true or mostly false 4) Write a function that takes in an array of type string, and prints out every string that starts with the letter ‘p’ or ‘h’ 5) Write a function that takes in an array of type double and round every element to its closer integer

2 D Arrays ◦ You can visualize these like a matrix, or game board!

2 D Arrays ◦ You can visualize these like a matrix, or game board! ◦ ALWAYS: arr[row][col] ◦ You’ll often see ‘col’ in place of column [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] [2][0] [2][1] [2][2]

Initializing all to 0 ◦ 1 -dimensional array: int board[3] = {0}; ◦ 2

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] = { {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? With a 2 -dimensional array, the compiler

When Initializing Which of these is invalid? With a 2 -dimensional array, the compiler absolutely needs to know the column (the second parameter) size at compile time The row is optional int data[10][2]; int data [4][]; COMPILER ERROR! int data [][3] = {{1, 2, 3}, {4, 5, 6}};

How do we declare and initialize our own 2 -d array ◦ Let’s say

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 How would we print this 2 D array?

What if we want to print it backwards?

What if we want to print it backwards?

2 D Array Wrap Up ◦ There a lot of examples from last lecture

2 D Array Wrap Up ◦ There a lot of examples from last lecture – go through them in detail! ◦ Any questions? ◦ Understanding arrays is very important for project 3

Questions?

Questions?

Upcoming Deadlines ◦ Assignment 3 Due Today (10/21) at 6: 00 pm ◦ Project

Upcoming Deadlines ◦ Assignment 3 Due Today (10/21) at 6: 00 pm ◦ Project 3 Due Next Friday (10/28) at 6: 00 pm ◦ Partners ◦ For projects 3 and 4, you are allowed to have a partner ◦ Ohhi ◦ http: //0 hh 1. com/ ◦ Get started early!