Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Chapter 10.
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.
©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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
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.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
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 to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
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
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 7 One-Dimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
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.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
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.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
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.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Arrays.
Engineering Computing I Chapter 5 Pointers and Arrays.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
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.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
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.
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
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.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
MULTI-DIMENSIONAL ARRAY
Engineering Problem Solving with C++, Etter/Ingber
Multiple Dimension Arrays
EKT150 : Computer Programming
Presentation transcript:

Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Multidimensional Arrays Multidimensional arrays are derived from the basic or built-in data types of the C language. Two-dimensional arrays are understood as rows and columns with applications including two-dimensional tables, parallel vectors, and two-dimensional matrices. The data stored in multidimensional arrays must be homogeneous. This type of data structure and its applications are very common in science and engineering.

Multidimensional Arrays Topics Concept of multidimensional arrays Comparing one- and multidimensional arrays Initialization multidimensional arrays Printing multidimensional arrays

8.1 Introduction to Two- Dimensional Arrays Declaration Statement Storage Allocation Array Initialization

Multidimensional Arrays What is a multidimensional array? B = 51, 52, 53 54, 55, 56 Algebraic notation Col 1Col 2Col 3 Row 1 Row 2 Int b[2][3] = {51, 52, 53, 54, 55, 56}; Array type Array name Array dimension = 2 Two rows Three columns First rowsecond row C notation

Multidimensional Arrays How to declare a multidimensional array? int b[2][3]; declares the name of the array to be b the type of the array elements to be int the dimension to be 2 (two pairs of brackets []) the number of elements or size to be 2*3 = 6

Declaration Statement

Storage Allocation

Multidimensional Arrays How to initialize a multidimensional array? Initialized directly in the declaration statement int b[2][3] = {51, 52, 53, 54, 55, 56}; b[0][0] = 51 b[0][1] = 52 b[0][2] = 53 Use braces to separate rows in 2-D arrays. int c[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; int c[ ][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; Implicitly declares the number of rows to be 4.

Array Initialization

8.2 Input of Two-Dimensional Arrays Data may be input into two-dimensional arrays using nested for loops interactively or with data files. Standard Input Input from a Data File

Standard Input

How to read the data from a file? fscanf (infile, "%d %d", &year, &month) ; for (day=1; day<=31; day++) { fscanf(infile, "%d",&rainfall[year][month][day]); } Note that the values of the first two subscripts for rainfall do not change. Only the last subscript changes. The function fscanf automatically skips to the next line when it is looking for the next non-white-space character. Loop over each day in a month. Using the previously read values for year and month, read each day’s rainfall value.

Input from a Data File

8.3 Output of Two-Dimensional Arrays The output of two-dimensional arrays should be in the form of rows and columns for readability. Nested for loops are used to print the rows and columns in row and column order. Standard Output Output to a Data File

How to print the data to the screen? printf ("Rainfall for Year = %d, Month = %d\n\n",year, month); for (day=1; day<=31; day++) { printf("%d ",rainfall[year][month][day]); if (day==7 || day==14 || day==21 || day==28) printf("\n"); } Illustrate some of the concepts of printing arrays.

Standard Output Output to a monitor or printer: int a[2][3] = {5, 6, 9, 4, 2, 10}; int i, j;.. for (i = 0; i < 2; i++) { for(j = 0; j < 3; j++) { printf(“%d “, a[i][j]); } printf(“\n”); } Note that at the end of each row, a line feed character is output so that the next row is on the next line.

Output to a Data File Output to a data file: int a[2][3]; int i, j;.. for (i = 0; i < 2; i++) { for(j = 0; j < 3; j++) { fprintf(outptr, “%d “, a[i][j]); }

8.4 Manipulation of Arrays Array Assignment Array Arithmetic Matrix Operation

Array Assignment

Array Arithmetic

Matrix Operation

8.5 Passing Arrays to Functions Two-dimensional arrays may by passed by array name. Because arrays are stored by rows, in order to accurately locate an element, a function must know the length of a row: that is the number of columns. This must be included in both the function prototype and the header of the function definition. Passing Fixed Sized Arrays Passing Array Elements

Functions and 1-D Arrays Topics Passing individual array elements to functions Passing entire arrays to functions Passing entire arrays to functions with a restriction Illustrate how values and address of array elements are passed

Function Call How to pass a single array element to a function? We treat a single element like a simple variable. If we want to change the value of the array element in the function, we use the “address of” operator, &, before the array element in the function call. If we want to pass element without having it changed in the function, we simple put the array element in the parameter list. function1(&a[5], a[8]);

Function Prototype When receiving an address, we must use a pointer variable (indicated in the declaration by *). When receiving a value, we use a simple variable. void function1(int *d, int e); function1(&a[5], a[8]); void function1(int *d, int e) Value passed to simple variable Address passed to pointer variable *d = e;

Function Call and Prototype How to pass the ability to access an entire 1-D array to a function? Pass the address of the first element of the array With the address of the first element, C can internally calculate the address of any element in the array. The address of the first element of an array is indicated by the array name with no brackets following it. &c[0] and c are equivalent.

Function Call and Prototype function2(c, 5) Pass the address of the first element of the array c[ ] to function2, which gives function2 the ability to modify the array c[ ]. The prototype for the function must indicate that it is receiving an address. Use * in the declaration void function2 (double *b, int num_elem); Use brackets [ ] void function2 (double b[ ], int num_elem);

Function Call and Prototype function2(c, 5) void function2 (double b[ ], int num_elem); Number of array elements passed as a simple variable Address of the first element passed to pointer variable indicated with brackets

Function Definition Within a function that has received an array’s address, how to work with the array? Must be cognizant of the number of elements that the array contains. void function2 (double b[ ], int num_elem) void function2 (double b[5]) This is correct – A separate parameter is used to transfer the number of elements. This is incorrect – It does not indicate that b[ ] has only five elements.

Passing Fixed Sized Arrays

Passing Array Elements

8.6 Higher-Dimensional Arrays Declaration and Storage Allocation Input of Three-Dimensional Arrays Output of Three-Dimensional Arrays Manipulation of Three-Dimensional Arrays

Higher-Dimensional Arrays Three-dimensional arrays are very common in scientific and engineering problems dealing with three-dimensional geometries. Some examples of such problems include: the stress analysis in three-dimensional solids and structures the computational analysis of velocity and pressure used to compute the drag and lift character of airplane wings wave motion and vibration analysis in three-dimensional solids and structures. Problems that deal with three space coordinates and time require four dimensions.

3-D Arrays 3-D array of size [2][3][4] k = 0 k = 1 k = 2 k = 3 Rightmost subscript J = 0 J = 1 J = 2 Middle subscript I =1 I =0 Leftmost subscript

Memory Location a[0][0][0] a[1][0][0] a[0][0][1] a[1][0][1] a[0][0][2] a[1][0][2] a[0][0][3] a[1][0][3] a[0][1][0] a[1][1][0] a[0][1][1] a[1][1][1] a[0][1][2] a[1][1][2] a[0][1][3] a[1][1][3] a[0][2][0] a[1][2][0] a[0][2][1] a[1][2][1] a[0][2][2] a[1][2][2] a[0][2][3] a[1][2][3]

Memory Location a[x][y][z] of an array declared with a size a[I][J][K] Sequence location = x*(J*K) + y*(K) + z + 1 For a 3-D a[2][3][4] array, the sequence location for element a[0][1][2] is 0*(3*4) + 1*(4) = 7 How to use a loop to print a multidimensional array? for (i=0; i<2; i++) { for (j=0; j<3; j++) { printf("b[%1d][%1d] = %5d", i, j, b[i][j]); } printf("\n"); }

Declaration and Storage Allocation Declaration of three-dimensional arrays requires three indices: plane, row, and column. On each plane, the row and column indices are the same and the plane index varies from one plane to the next. The plane index varies from 0 to one less than the number of planes, the row index varies from 0 to one less than the number of rows, and the column index varies from 0 to one less than the number of columns.

Input of Three-Dimensional Arrays

Output of Three-Dimensional Arrays

Manipulation of Three- Dimensional Arrays

Sample Programs Drag Force Saddle Point Computation of Pressure Geometric Transformations Inventory of Cars in the XYZ Dealership