Lecturers slides http www comp nus edu sgcs

  • Slides: 18
Download presentation
Lecturer’s slides http: //www. comp. nus. edu. sg/~cs 1010/ WEEK 6 Class Activities

Lecturer’s slides http: //www. comp. nus. edu. sg/~cs 1010/ WEEK 6 Class Activities

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6: Arrays Preparation § Unit

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6: Arrays Preparation § Unit #11: Random Numbers § Unit #12: Using UNIX I/O Redirection One-dimensional Arrays § Going through examples in Unit #10 Multi-dimensional Arrays § Exercise #1: Sum to Random Position § Exercise #2: Matrix Multiplication § Exercise #3: Valid Path Week 6 - 2

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 3 Random Numbers

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 3 Random Numbers § We will go through Unit #11 Random Numbers.

© NUS CS 1010 (AY 2014/5 Semester 1) Using UNIX I/O Redirection § We

© NUS CS 1010 (AY 2014/5 Semester 1) Using UNIX I/O Redirection § We will go through Unit #12 UNIX I/O Redirection Week 6 - 4

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 5 One-dimensional Arrays

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 5 One-dimensional Arrays § We will go through the examples in Unit #10.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 6 Multi-dimensional Arrays

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 6 Multi-dimensional Arrays § We will go through the examples in Unit #10. § Work out the following exercises.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 7 Exercise #1:

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 7 Exercise #1: Sum to Random Position (1/4) § Write a program Week 6_Sum. To. Random. Pos. c that reads in values (of type float) for a 2 D array with at most 5 rows and 8 columns, generates a random position in the array and sums the elements from index [0][0] to that position, in row-major order. § Your program should contain the function sum. Partial() to take in the array and a random position and return the sum of the elements up to that position. § § What are the parameters of sum. Partial()? The incomplete program Week 6_Sum. To. Random. Pos. c is given. Study the function scan. Array() closely.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 8 Exercise #1:

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 8 Exercise #1: Sum to Random Position (2/4) § The sum is printed in 2 decimal places. § To ease data input, create a file to store the input data, and use UNIX input redirection to redirect input from this file when you execute the program. § Sample run: $ Enter rows and columns: 3 4 $ Enter 12 values: 5. 1 4. 2 -6. 3 12. 4 7. 5 8. 6 -3. 7 11. 8 9. 9 -20. 0 17. 1 10. 2 Sum to position [1][2] = 27. 80

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 9 Exercise #1:

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 9 Exercise #1: Sum to Random Position (3/4) #include <stdio. h> #include <stdlib. h> #include <time. h> #define MAX_ROWS 5 #define MAX_COLS 5 Week 6_Sum. To. Random. Pos. c void scan. Array(float [][MAX_COLS], int); int main(void) { float array[MAX_ROWS][MAX_COLS]; int rows, cols, up. To. Row, up. To. Col; printf("Enter rows and columns: "); scanf("%d %d", &rows, &cols); scan. Array(array, rows, cols); srand(time(NULL)); up. To. Row = rand() % rows; up. To. Col = rand() & cols; // call sum. Partial() function below printf("Sum to position [%d] = %. 2 fn"); // incomplete return 0; }

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 10 Exercise #1:

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 10 Exercise #1: Sum to Random Position (4/4) Week 6_Sum. To. Random. Pos. c void scan. Array(float arr[][MAX_COLS], int rows, int cols) { int r, c; printf("Enter %d values: n", rows * cols); for (r=0; r < rows; r++) for (c=0; c < cols; c++) scanf("%f", &arr[r][c]); } // Sum elements from position [0][0] to a random // position [up. To. Row][up. To. Col]. // Fill in sum. Partial() function below.

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 11 Exercise #2:

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 11 Exercise #2: Matrix Multiplication (1/3) § To multiply two matrices A and B, the number of columns in A must be the same as the number of rows in B. § The resulting matrix has same number of rows as A and number of columns as B § For example, multiplying a 2 4 matrix with a 4 3 matrix gives a 2 3 matrix. m n matrix n p matrix = m p matrix

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 12 Exercise #2:

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 12 Exercise #2: Matrix Multiplication (2/3) § To compute C = A B, where A, B, C are matrices ci, j = (ai, 1 b 1, j ) + (ai, 2 b 2, j ) +. . . + (ai, n bn, j ) ci, j is sum of terms produced by multiplying the elements of A’s row i with B’s column j. § Examples: § Complete the prod. Matrix() function in Unit 10_Matrix. Ops. c

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 13 Exercise #2:

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 13 Exercise #2: Matrix Multiplication (3/3) § Multiplying a 2 4 matrix with a 4 3 matrix: col 0 col 1 row 0 2 1 3 2 row 1 3 0 2 1 3 2 1 2 2 3 1 3 0 2 1 3 6 + 2 + 3 + 4 = 15 4 + 2 + 9 + 2 = 17 9 + 0 + 2 = 13 row 0, col 0 col 1 = ? 17 ? 11 ? 15 ? 13 row 1, col 0 ? 6

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 14 Maze (1/2)

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 14 Maze (1/2) § Let’s consider a maze that is represented by a twodimensional 6 6 integer array. § The value of each array element is either 0 (representing a wall) or 1 (representing a cell). § The starting and exit points in the maze are specified by the cells maze[0][0] and maze[5][5] respectively. § A path is represented by a single-dimensional character array with four possible element values representing the move directions: ‘N’ (for north), ‘S’ (for south), ‘E’ (for east), and ‘W’ (for west). Each path is defined with respect to the starting cell maze[0][0].

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 15 Maze (2/2)

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 15 Maze (2/2) § Example of a 6 6 maze Start 0 1 2 3 4 5 Exit Cell Wall

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 16 Exercise #3:

© NUS CS 1010 (AY 2014/5 Semester 1) Week 6 - 16 Exercise #3: Valid Path § § A path in a maze is defined to be valid if the path is within the maze and does not knock against any wall. 0 1 2 3 4 5 Examples: 0 § § § Valid path: ‘E’, ‘S’, ‘N’, ‘E’, ‘S’ Invalid path: ‘S’, ‘W’ Invalid path: ‘S’, ‘E’ 1 2 3 4 § Write a function 5 inis. Valid (int maze[][6], char path[]) that takes in a 6 6 maze and a path with at most 10 characters. It returns 1 if path is valid in maze, or returns 0 otherwise. § An incomplete program Week 6_Is. Valid. c is given. It handles string input which is not covered yet.

© NUS CS 1010 (AY 2014/5 Semester 1) Things-To-Do n Revise n n PE

© NUS CS 1010 (AY 2014/5 Semester 1) Things-To-Do n Revise n n PE 1 n n n Chapter 6: Numeric Arrays This Saturday! Refer to CS 1010 website “PE” page for details Continue to do practice exercises on Code. Crunch Week 6 - 17

© NUS CS 1010 (AY 2014/5 Semester 1) End of File Week 6 -

© NUS CS 1010 (AY 2014/5 Semester 1) End of File Week 6 - 18