Engineering Problem Solving with C++, Etter/Ingber

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Engineering Problem Solving with C++ An Object Based Approach Chapter 7 Two-Dimensional Arrays and Matrices.
©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 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
Chapter 8 Arrays and Strings
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.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
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.
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.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
 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.
Arrays.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Two-Dimensional Arrays and Matrices ELEC 206 Computer Applications for Electrical Engineers.
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.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Arrays float Scores[9]; ? index: element // one dimensional array 1.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
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.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
EGR 2261 Unit 10 Two-dimensional Arrays
Two-Dimensional Arrays
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Computer Programming BCT 1113
Two Dimensional Array Mr. Jacobs.
Two-Dimensional Arrays Lesson xx
ECE Application Programming
Engineering Problem Solving with C++, Etter
Multi-Dimension Arrays
Chapter 7: Arrays.
ECE Application Programming
Two Dimensional Arrays
Array Data Structure Chapter 6
One-Dimensional Array Introduction Lesson xx
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
4.9 Multiple-Subscripted Arrays
Standard Version of Starting Out with C++, 4th Edition
Multidimensional array
Multidimensional Arrays
Engineering Problem Solving with C++, Etter
7 Arrays.
Multi-Dimensional Arrays
CHAPTER 2 Arrays and Vectors.
INC 161 , CPE 100 Computer Programming
Array Data Structure Chapter 6
Arrays Arrays A few types Structures of related data items
CHAPTER 2 Arrays and Vectors.
Arrays.
C++ Array 1.
Presentation transcript:

Engineering Problem Solving with C++, Etter/Ingber Chapter 7 Two-Dimensional Arrays and Matrices Engineering Problem Solving with C++, second edition, J. Ingber

Two Dimensional Arrays Declaration and Initialization Computation and Output Function Arguments Engineering Problem Solving with C++, second edition, J. Ingber

Declaration and initialization Engineering Problem Solving with C++, second edition, J. Ingber

Declaration and Initialization The declaration of a two-dimensional array requires a row size and a column size. A consecutive block of (row size)(*column size) memory locations are allocated. The name of the array holds the address of the first byte of memory. Engineering Problem Solving with C++, second edition, J. Ingber

Engineering Problem Solving with C++, second edition, J. Ingber Example: //Declaration int data[2][3]; Memory Snapshot data ? ? ? ? ? ? Engineering Problem Solving with C++, second edition, J. Ingber

Engineering Problem Solving with C++, second edition, J. Ingber Example: //Declaration int data[2][3]; row/column form: col 0 col 1 col 2 ? row 0 row 1 Engineering Problem Solving with C++, second edition, J. Ingber

Engineering Problem Solving with C++, second edition, J. Ingber Example: //Declaration and Initialization double t[2][2] = { {0.1,0.2}{1.1, 1.2} }; Memory Snapshot t 0.1 0.2 1.1 1.2 row 0 0.1 0.2 1.1 1.2 row 1 col 0 col 1 Engineering Problem Solving with C++, second edition, J. Ingber

Two-Dimensional Arrays A two dimensional array stores data as a collection of rows and columns. Each element of a two-dimensional array has a row position and a column position. To access an element in a two-dimensional array, you must specify the name of the array followed by: a row offset a column offset Engineering Problem Solving with C++, second edition, J. Ingber

Example: Input Using cin Nested for loops are often used when inputting and assigning values to a two-dimensional array. Example: double table[RSIZE][CSIZE]; for (int i=0; i<RSIZE; ++i) //every row for (int j=0; j<CSIZE; ++j )//every col cin >> table[i][j]; Engineering Problem Solving with C++, second edition, J. Ingber

Engineering Problem Solving with C++, second edition, J. Ingber Example: Assignment Example: int v[RSIZE][CSIZE]; for (int i=0; i<RSIZE; ++i) //every row for (int j=0; j<CSIZE; ++j )//every col v[i][j] = i+j; V 1 2 3 Engineering Problem Solving with C++, second edition, J. Ingber

Computation and output computing an average formatted output function arguments Computation and output Engineering Problem Solving with C++, second edition, J. Ingber

Computation and Output The for statement is useful when performing computations on arrays. The index variable of the for statement can be used as an array offset. Computations can be easily performed on an entire two-dimensional array, or on a sub set of the array. Engineering Problem Solving with C++, second edition, J. Ingber

Engineering Problem Solving with C++, second edition, J. Ingber Computations Compute the average of an array with n rows and m columns. for(int i=0; i<n; ++i) { for(int j=0; j<m; ++j) sum+=array[i][j]; } average = sum/(n*m); Engineering Problem Solving with C++, second edition, J. Ingber

Engineering Problem Solving with C++, second edition, J. Ingber Computations Compute the average of the nth row of a two-dimensional array with r rows and c columns. for(int col=0; col < c; ++col) { sum += array[n][col]; } rowAverage = sum/c; Engineering Problem Solving with C++, second edition, J. Ingber

Engineering Problem Solving with C++, second edition, J. Ingber Modify! Modify the C++ statements on the previous slide to compute the average of the nth column. Engineering Problem Solving with C++, second edition, J. Ingber

Engineering Problem Solving with C++, second edition, J. Ingber Output Two dimensional arrays are often printed in a row by row format, using nested for statements. When printing the row values of an array, be sure to print: whitespace between the values in a row. a newline character at the end of each row. Engineering Problem Solving with C++, second edition, J. Ingber

Engineering Problem Solving with C++, second edition, J. Ingber Printing 2-D Array for(int r=0; r<NROWS; ++r) { for(int c=0; c<NCOLS; ++c) cout << twoDArr[r][c] << " "; } cout << endl; Engineering Problem Solving with C++, second edition, J. Ingber

Engineering Problem Solving with C++, second edition, J. Ingber Functions Arguments 2-D arrays are always passed by reference. All dimensions of the array, except the leftmost one (row), must be specified. The leftmost one may be empty [] Function prototype example: int rowAverage(int Arr[][COLSIZE], int whichRow); Array declaration in main: int table [ROWSIZE][COLSIZE]; Function invocation example: avg = rowAverage(table, 3); Engineering Problem Solving with C++, second edition, J. Ingber