http://cs1010/ week 6 class activities lecturer’s slides

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

Upload: ralf-young

Post on 17-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

http://www.comp.nus.edu.sg/~cs1010/

WEEK 6

Class Activities

Lect

urer

’s sl

ides

Page 2: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

Week 6: Arrays

Going through examples in Unit #10

CS1010 (AY2015/6 Semester 1) Week6 - 2© NUS

Exercise #1: Sum to Random Position Exercise #2: Matrix Multiplication Exercise #3: Valid Path

One-dimensional Arrays

Multi-dimensional Arrays

Unit #11: Random Numbers Unit #12: Using UNIX I/O Redirection

Preparation

Page 3: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

Random Numbers

CS1010 (AY2015/6 Semester 1) Week6 - 3© NUS

We will go through Unit #11 Random Numbers.

Page 4: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

Using UNIX I/O Redirection

CS1010 (AY2015/6 Semester 1) Week6 - 4© NUS

We will go through Unit #12 UNIX I/O Redirection

Page 5: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

One-dimensional Arrays

CS1010 (AY2015/6 Semester 1) Week6 - 5© NUS

We will go through the examples in Unit #10.

Page 6: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

Multi-dimensional Arrays

CS1010 (AY2015/6 Semester 1) Week6 - 6© NUS

We will go through the examples in Unit #10. Work out the following exercises.

Page 7: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

Exercise #1: Sum to Random Position (1/4)

CS1010 (AY2015/6 Semester 1) Week6 - 7© NUS

Write a program Week6_SumToRandomPos.c that reads in values (of type float) for a 2D 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 sumPartial() 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 sumPartial()?

The incomplete program Week6_SumToRandomPos.c is given. Study the function scanArray() closely.

Page 8: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

Exercise #1: Sum to Random Position (2/4)

CS1010 (AY2015/6 Semester 1) Week6 - 8© NUS

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.47.5 8.6 -3.7 11.89.9 -20.0 17.1 10.2Sum to position [1][2] = 27.80

Page 9: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

Exercise #1: Sum to Random Position (3/4)

CS1010 (AY2015/6 Semester 1) Week6 - 9© NUS

#include <stdio.h>#include <stdlib.h>#include <time.h>#define MAX_ROWS 5#define MAX_COLS 5

void scanArray(float [][MAX_COLS], int, int);

int main(void) {float array[MAX_ROWS][MAX_COLS];int rows, cols, upToRow, upToCol;

printf("Enter rows and columns: ");scanf("%d %d", &rows, &cols);scanArray(array, rows, cols);

srand(time(NULL));upToRow = rand() % rows;upToCol = rand() & cols;// call sumPartial() function belowprintf("Sum to position [%d][%d] = %.2f\n"); // incomplete return 0;

}

Week6_SumToRandomPos.c

Page 10: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

void scanArray(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 [upToRow][upToCol].// Fill in sumPartial() function below.

Exercise #1: Sum to Random Position (4/4)

CS1010 (AY2015/6 Semester 1) Week6 - 10© NUS

Week6_SumToRandomPos.c

Page 11: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

Exercise #2: Matrix Multiplication (1/3)

CS1010 (AY2015/6 Semester 1) Week6 - 11© NUS

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 24 matrix with a 43 matrix gives a 23 matrix.

n pmatrix

m nmatrix = m p

matrix

Page 12: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

Exercise #2: Matrix Multiplication (2/3)

CS1010 (AY2015/6 Semester 1) Week6 - 12© NUS

To compute C = A B, where A, B, C are matrices

ci,j = (ai,1 b1,j ) + (ai,2 b2,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:

121

132

023

120

012

001

101

110

021

Complete the prodMatrix() function in Unit10_MatrixOps.c

61313

111715

312

031

322

123

1203

2312

Page 13: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

Exercise #2: Matrix Multiplication (3/3)

CS1010 (AY2015/6 Semester 1) Week6 - 13© NUS

Multiplying a 2 4 matrix with a 4 3 matrix:

? =

2

3

1

0

3

2

2

1

3

2

2

2

1

3

?

?

?

?

?1

2

3

1

0

3

15

13

17

13

11

6

row 0, col 0

row 0

col 0

6 + 2 + 3 + 4

row 1

= 15

9 + 0 + 2 + 2 = 13

row 0, col 1

col 1

4 + 2 + 9 + 2 = 17

row 1, col 0

Page 14: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

Maze (1/2)

CS1010 (AY2015/6 Semester 1) Week6 - 14© NUS

Let’s consider a maze that is represented by a two-dimensional 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].

Page 15: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

Maze (2/2)

CS1010 (AY2015/6 Semester 1) Week6 - 15© NUS

Example of a 6 6 maze

0 1 2 3 4 5

0

1

2

3

4

5

Start

Exit

Cell

Wall

Page 16: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

Exercise #3: Valid Path

CS1010 (AY2015/6 Semester 1) Week6 - 16© NUS

A path in a maze is defined to be valid if the path is within the maze and does not knock against any wall.

Examples: Valid path: ‘E’, ‘E’, ‘S’, ‘N’, ‘E’, ‘E’, ‘S’ Invalid path: ‘S’, ‘S’, ‘W’ Invalid path: ‘S’, ‘S’, ‘S’, ‘E’

Write a functioninisValid (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 Week6_IsValid.c is given. It handles string input which is not covered yet.

0 1 2 3 4 5

0

1

2

3

4

5

Page 17: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

Things-To-Do

CS1010 (AY2015/6 Semester 1) Week6 - 17

Revise Chapter 6: Numeric Arrays

PE1 This Saturday! Refer to CS1010 website “PE” page for details

Continue to do practice exercises on CodeCrunch

© NUS

Page 18: Http://cs1010/ WEEK 6 Class Activities Lecturer’s slides

End of File

CS1010 (AY2015/6 Semester 1) Week6 - 18© NUS