R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.

Slides:



Advertisements
Similar presentations
O-1 University of Washington Computer Programming I Lecture 14: Arrays © 2000 UW CSE.
Advertisements

1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
O-1 University of Washington Computer Programming I Lecture 14: Arrays © 2000 UW CSE.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
M-1 University of Washington Computer Programming I Lecture 13 Pointer Parameters © 2000 UW CSE.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Chapter 9: Arrays and Strings
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Chapter 8 Arrays and Strings
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Lesson 7 Arrays CS 1 Lesson 7 -- John Cole1. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 13 – Having Fun with Arrays in Java Webpage:
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
Chapter 8 Arrays and Strings
Arrays- Part 2 Spring 2013Programming and Data Structure1.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Lecture 16: Working with Complex Data Arrays. Double-Subscripted Arrays Commonly used to represent tables of values consisting of information arranged.
Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 7 Arrays.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
ARRAYS Multidimensional realities Image courtesy of
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Arrays Hold Multiple Values 7.1.
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
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.
Arrays Chapter 7.
EGR 2261 Unit 10 Two-dimensional Arrays
Programming application CC213
ECE Application Programming
Image Processing & Perception
C Passing arrays to a Function
Presentation transcript:

R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE

R-2 Overview Review 1-D arrays Concepts this lecture: 2-D arrays 2-D arrays as parameters Layout of 2-D arrays in memory Reading Textbook sec. 8.7

R-3 Overview Review 1-D arrays Concepts this lecture: 2-D arrays 2-D arrays as parameters Layout of 2-D arrays in memory

R-4 Arrays as Data Structures Review: An array is an ordered collection of values of identical type Name the collection; number the elements Arrays are the natural choice for organizing a large number of values, all of identical type

R-5 Beyond Simple Arrays Sometimes the collection of values has some additional regular pattern or structure One common such structure is the matrix or table In C, we can express this as a two- dimensional array Higher-dimensional arrays (3-D, 4-D, …) are possible, but we won’t use them in this course

R-6 2-Dimensional Arrays An ordered collection of values of identical type Name the collection; number the elements Like 1-D arrays, but a different numbering scheme Example: scores for 7 students on 4 homeworks C expressions: score[0][0] is 22 score[6][3] is 12 2*score[3][0] is student 0 student 1 student 2 student 3 student 4 student 5 student hw score

R-7 Declaring a 2-D Array int score [MAX_STUDENTS] [MAX_HWS] ; #define MAX_STUDENTS 80 #define MAX_HWS6...

R-8 2-D Arrays: Terminology type name[#rows][#columns] int score[80][6]; score is a two-dimensional array of int of size 80 by 6 score[0][0], score[0][1],...., score[79][5] are the elements of the array

R-9 An Alternate View int score[80][6]; We could also view each row as an element: “score is an array of size 80” With this view, each element (row) is a 1-D array,of type “array of size 6 of int”

R-10 Bookkeeping As with 1-D arrays, often we only use part of the space available in a 2-D array Declared size of the array specifies its maximum capacity. The current size (# of rows and columns currently in use) needs to be kept track of in separate variables

R-11 Reading in Data Problem: Read in data for student assignments Input data format: The number of students, then the number of assignments, followed by the data per student A nested loop is the right program structure for reading in the data details int score [MAX_STUDENTS] [MAX_HWS] ; int nstudents, nhws, i, j ;

R-12 Reading a 2-D Array: Code /* Read the number of students and assignments, then loop to read detailed data */ scanf (“%d %d”, &nstudents, &nhws) ; if (nstudents <= MAX_STUDENTS && nhws <= MAX_HWS) { for ( i = 0 ; i < nstudents ; i = i + 1 ) for ( j = 0 ; j < nhws ; j = j + 1 ) scanf(“%d”, &score [i] [j]) ; } Part of the array is unused; which part?

R-13 Array Input Trace scorej= i=0 i=1 i=2... i=6 i=7 Input: ?? ?? … ????...

R-14 Printing a 2-D Array if (nstudents <= MAX_STUDENTS && nhws <= MAX_HWS) { for ( i = 0 ; i < nstudents ; i = i + 1 ) { for ( j = 0 ; j < nhws ; j = j + 1 ) printf(“%d”, score [i] [j]) ; printf(“\n”) ; }

R-15 2-D Arrays as Parameters Same as 1-D arrays (almost): Individual array elements can be either value or pointer parameters Entire arrays are always passed as pointer parameters - never copied Don’t use & and * with entire array parameters Difference: No empty brackets [ ] in formal parameters Actually, [ ] allowed sometimes; we won’t use in this course

R-16 2-D Array As Parameter A function to read into array a the grade information for the given number of students and assignments void read_2D ( int a [MAX_STUDENTS] [MAX_HWS], int nstudents, int nhws) {...

R-17 2-D Array As Parameter /* Read into array a the grade information for */ /* the given number of students and assignments */ void read_2D (int a [MAX_STUDENTS] [MAX_HWS], int nstudents, int nhws) { int i, j ; for ( i = 0 ; i < nstudents ; i = i + 1 ) for ( j = 0 ; j < nhws ; j = j + 1 ) scanf(“%d”, &a[i][j]) ; }

R-18 Array Function Arguments int main(void) { int score [MAX_STUDENTS] [MAX_HWS] ; int nstudents, nhws ; scanf (“%d %d”, &nstudents, &nhws) ; if (nstudents <= MAX_STUDENTS && nhws <= MAX_HWS) read_2D (score, nstudents, nhws) ;... }no &

R-19 Example - Digital Image A digital image is a rectangular grid of pixels Pixel representation: integer value giving brightness from 0 (off) to 255 (full on) Black & White: one int per pixel Color: 3 ints per pixel - one each for red, green, and blue An image is normally stored as a 2D array

R-20 Problem - Shift Image Write a function that shifts a B&W image right one pixel Strategy: shift columns one at a time To shift a column, shift the pixels 1 row at a time

R-21 A couple of definitions /* Number of rows and columns in image */ #define NROWS 768 #define NCOLS 1024 /* Representation of a white pixel */ #define WHITE 255

R-22 Code /* Shift image right one column */ void shift_right(int image[NROWS][NCOLS]) { int row, col; /* shift all columns */ for (col = … ) { /* shift column col one space to the right */ for (row = 0; row < NROWS; row++) image[row][col+1] = image[row][col]; } /* set leftmost column to white */ for (row = 0; row < NROWS; row++) image[row][0] = WHITE; }

R-23 Column Sequence Question: Does it matter if we shift from left to right vs right to left? Question: What are the correct loop bounds for col? 0 to NCOLS-1? 0 to NCOLS-2? 1 to NCOLS-1? Something else? /* shift all columns */ for (col = … ) { /* shift column col one space to the rigtht */ for (row = 0; row < NROWS; row++) image[row][col+1] = image[row][col]; }

R

R-25 Order Answer: Yes it does. If we shift from left to right, we wind up making copies of column 0 in every column of the image. Correct code looks like this. /* shift all columns */ for (col = NCOLS - 2; col >= 0; col-- ) { /* shift column col one space to the rigtht */ for (row = 0; row < NROWS; row++) image[row][col+1] = image[row][col]; }

R-26 Column Sequence Question: Does it matter if we shift from left to right vs right to left? Question: What are the correct loop bounds for col? 0 to NCOLS-1? 0 to NCOLS-2? 1 to NCOLS-1? Something else? /* shift all columns */ for (col = … ) { /* shift column col one space to the rigtht */ for (row = 0; row < NROWS; row++) image[row][col+1] = image[row][col]; }

R-27 Image Scrolling Finally, we can use our function to scroll an image completely off the screen int main(void) { int image[NROWS][NCOLS]; /* the image */ int k; /* assume image is initialized elsewhere */ … /* scroll image completely off the screen */ for (k = 0; k < NCOLS; k++) shift_right(image); return 0; } Note: no & (it’s an array)

R-28 Representation of Arrays A computer’s memory is a one dimensional array of cells How is a 2-D array stored? Answer: In C, the array rows are stored sequentially: row 0, 1, 2, …

R-29 Representation of Arrays student 0student 1student ?? ?? ??15... score hw student ?? student ?? student ?? student ?? student ?? student ?? student ?? student 7??????

R-30 Summary 2-D arrays model matrices or tables of data Notation and use is an extension of 1-D arrays Nested loops are often the natural processing technique