Multidimensional Arrays

Slides:



Advertisements
Similar presentations
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
Advertisements

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.
Multiple-Subscripted Array
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.
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.
Arrays One-Dimensional initialize & display Arrays as Arguments Two-dimensional initialize & display Part I.
Chapter 8 Arrays and Strings
Two –Dimensional Arrays Mrs. C. Furman Java Programming November 19, 2008.
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.
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.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)
Arrays.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Arrays float Scores[9]; ? index: element // one dimensional array 2.
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.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
I/O Streams File I/O 2-D array review
EGR 2261 Unit 10 Two-dimensional Arrays
Lecture 8: 2D Arrays and Nested Loops
Two-Dimensional Arrays
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Arrays 1. One dimensional arrays - Review 2. Using arrays
Computer Programming BCT 1113
Two Dimensional Array Mr. Jacobs.
Two-Dimension Arrays Computer Programming 2.
ECE Application Programming
Numeric Arrays Numeric Arrays Chapter 4.
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Multi-dimensional Array
ECE Application Programming
C Passing arrays to a Function
Engineering Problem Solving with C++, Etter/Ingber
Array Data Structure Chapter 6
Multidimensional Arrays Vectors of Vectors
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Chapter 8 Slides from GaddisText
1020: Introduction to Programming Mohamed Shehata November 22, 2017
Engr 0012 (04-1) LecNotes
Multidimensional Arrays
Chapter 7 Part 2 Edited by JJ Shepherd
Lecture 4 2d Arrays CSE /26/2018.
Multidimensional array
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Multi-Dimensional Arrays
Dr Tripty Singh Arrays.
INC 161 , CPE 100 Computer Programming
Array Data Structure Chapter 6
Fundamental Programming
Multi-Dimensional Arrays
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
EECE.2160 ECE Application Programming
C++ Array 1.
EECE.2160 ECE Application Programming
ICS103: Programming in C Searching, Sorting, 2D Arrays
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Variable Storage Memory Locations (Logical) Variable Classes Stack
Presentation transcript:

Multidimensional Arrays

Array Conceptual: What if we want more than one index??? List of objects indexed by a number What if we want more than one index??? Row / Col

Two-Dimensional Arrays Logical matrix Declare: type identifier[rows][cols]; Rows then columns int scores[5][3]; 1 2 76 82 83 84 94 88 93 3 91 98 4

Initialization Initialize as list of lists: Zero out entire array: int nums[3][4] =  {{19,22,31,42},                   {50,61,32,83},                    {93,47,15,66}}; Zero out entire array: int nums[3][4] =  {{0}};

2D Access Access: scores[row][col] scores[4][0] = 10; cout << scores[2][1]; //outputs 93 1 2 76 82 83 84 94 88 93 3 91 98 4 10

Storage 2D arrays stored internally in row major order scores[1][??] First dimension is start address of row scores[1][??] 1 2 76 82 83 84 94 88 93 1 2 3 4 5 6 7 8 76 82 83 84 94 88 93

Traversals Traverse with nested loops Row index/col index Loop order matters

Loop Samples i, j aren't great, but get used to them Use constants for loop conditions

Single Dimension Traversals Traversing one row or column requires one loop, one hardcoded index

Sum all columns Sum all columns: Column is main loop, rows second:

Sum Rows And Columns Use arrays of row totals / col totals to build all sums 1 4 2 5 9 7 1 2 1

Sum Rows And Columns Use arrays of row totals / col totals to build all sums 1 4 2 5 9 7 1 2 4 1 4

Sum Rows And Columns Use arrays of row totals / col totals to build all sums 1 4 2 5 9 7 1 2 6 1 4 2

Sum Rows And Columns Use arrays of row totals / col totals to build all sums 1 4 2 5 9 7 1 2 6 5 1 9 2

Sum Rows And Columns Use arrays of row totals / col totals to build all sums

Passing Arrays Must specify each dimension after first when passed as parameter

Storage 2D arrays stored internally in row major order scores[1][??] First dimension is start address of row scores[1][??] 1 2 76 82 83 84 94 88 93 1 2 3 4 5 6 7 8 76 82 83 84 94 88 93

Passing Arrays Compiles, but BAD

Passing Arrays Specify columns using global constant:

Passing Arrays Same using defined global constants: Don't need to pass number rows Still need to specify array second dimension

Faking 2D Can fake 2D with a 1D array scores[1][2] scores[3*1 + 2] [row][col]  [colwidth*row][col] 1 2 76 82 83 84 94 88 93 scores[1][2] scores[3*1 + 2] 3*0 + 0 3*0 + 1 3*0 + 2 3*1 + 0 3*1 + 1 3*1 + 2 3*2 + 0 3*2 + 1 3*2 + 2 1 2 3 4 5 6 7 8 76 82 83 84 94 88 93

Multidimensional Arrays Can have an arbitrary number of dimensions: