WEEK 6 Class Activities Lecturer’s slides.

Slides:



Advertisements
Similar presentations
WEEK 5 Class Activities Lecturer’s slides.
Advertisements

1 CS 162 Introduction to Computer Science Chapter 7 Matrix Manipulation Herbert G. Mayer, PSU Status 9/21/2014.
CS1010 Programming Methodology
UNIT 12 UNIX I/O Redirection.
Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.
1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.
Multidimensional arrays Many problems require information be organized as a two- dimensional or multidimensional list Examples –Matrices –Graphical animation.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
Chapter 8 Arrays and Strings
Building Java Programs Chapter 7.5
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
WEEK 9 Class Activities Lecturer’s slides.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 13 – Having Fun with Arrays in Java Webpage:
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
A First Book of ANSI C Fourth Edition
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
Crypto Project Sample Encrypted Data: –Turing: CS Public\CryptoProjectData Crypto_Short_Keys_P_U.out Crypto_Long_Keys_O_R.out Bonus +10 points on.
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
WEEK 4 Class Activities Lecturer’s slides.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
UNIT 14 Functions with Pointer Parameters.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
CS1101: Programming Methodology
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
Arrays as Function Arguments Array can be used as a function argument. E.g., #include int sum(int b[], int n) { int i, res; res = 0; for(i = 0; i < n;
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
UNIT 10 Multidimensional Arrays.
CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays.
UNIT 11 Random Numbers.
WEEK 8 Class Activities Lecturer’s slides.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Computer Programming for Engineers
CS1020 Data Structures and Algorithms I Lecture Note #2 Arrays.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Chapter 8: Arrays Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Move the first element to the.
DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing.
8. ARRAYS. Aggregate variables Scalar variables –Single value Aggregate variables –Collection of values –Arrays: elements have the same type.
UNIT 9 Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
CS1010: Programming Methodology
Table of Contents Matrices - Definition and Notation A matrix is a rectangular array of numbers. Consider the following matrix: Matrix B has 3 rows and.
WEEK 10 Class Activities Lecturer’s slides.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
Week 12 Class Activities.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.
CS1101: Programming Methodology Aaron Tan.
EGR 2261 Unit 10 Two-dimensional Arrays
CS1010 Programming Methodology
Programming Fundamental
CS 1430: Programming in C++.
CS1100 Computational Engineering
Multidimensional Arrays
Multidimensional Arrays
Multidimensional array
Presentation transcript:

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

Random Numbers CS1010 (AY2015/6 Semester 1)Week6 - 3© NUS  We will go through Unit #11 Random Numbers.

Using UNIX I/O Redirection CS1010 (AY2015/6 Semester 1)Week6 - 4© NUS  We will go through Unit #12 UNIX I/O Redirection

One-dimensional Arrays CS1010 (AY2015/6 Semester 1)Week6 - 5© NUS  We will go through the examples in Unit #10.

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.

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.

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: Sum to position [1][2] = 27.80

Exercise #1: Sum to Random Position (3/4) CS1010 (AY2015/6 Semester 1)Week6 - 9© NUS #include #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 below printf("Sum to position [%d][%d] = %.2f\n"); // incomplete return 0; } Week6_SumToRandomPos.c

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

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 2  4 matrix with a 4  3 matrix gives a 2  3 matrix. n  p matrix m  n matrix  = m  p matrix

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 c i,j = (a i,1  b 1,j ) + (a i,2  b 2,j ) (a i,n  b n,j ) c i,j is sum of terms produced by multiplying the elements of A’s row i with B’s column j.  Examples:  Complete the prodMatrix() function in Unit10_MatrixOps.c

Exercise #2: Matrix Multiplication (3/3) CS1010 (AY2015/6 Semester 1)Week6 - 13© NUS  Multiplying a 2  4 matrix with a 4  3 matrix: ?  = ? ? ? ? ? row 0, col 0 row 0 col row 1 = =13 row 0, col 1 col =17 row 1, col 0

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].

Maze (2/2) CS1010 (AY2015/6 Semester 1)Week6 - 15© NUS  Example of a 6  6 maze Start Exit Cell Wall

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 function in isValid (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

Things-To-Do CS1010 (AY2015/6 Semester 1)Week Revise Chapter 6: Numeric Arrays PE1 This Saturday! Refer to CS1010 website “PE” page for details Continue to do practice exercises on CodeCrunch © NUS

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