Multidimensional Arrays Vectors of Vectors

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Two-Dimensional Arrays Chapter What is a two-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series.
1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Computer programming 1 Multidimensional Arrays, and STL containers: vectors and maps.
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.
 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.
Multiple-Subscripted Array
C++ for Engineers and Scientists Third Edition
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.
Chapter 9 Introduction to Arrays
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.
Java Unit 9: Arrays Declaring and Processing Arrays.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
Arrays.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
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.
COMP 110: Spring Announcements Lab 7 was due today Binary Expression Assignment due Friday.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
CS 201 Tarik Booker California State University, Los Angeles.
Chapter 9 Introduction to Arrays Fundamentals of Java.
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.
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Chapter 6: Using Arrays.
I/O Streams File I/O 2-D array review
Two-Dimensional Arrays
Multidimensional Arrays
Two Dimensional Arrays and Complex Conditions
Two Dimensional Arrays
Two Dimensional Array Mr. Jacobs.
CSCE 210 Data Structures and Algorithms
Dynamic Array Multidimensional Array Matric Operation with Array
MULTI-DIMENSIONAL ARRAY
CS Computer Science IB: Object Oriented Programming
Multi-dimensional Array
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Repeating Instructions And Advance collection
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Nested Loop Review and Two-Dimensional Arrays
Chapter 13 Vector of Vectors (2D Arrays)
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
1020: Introduction to Programming Mohamed Shehata November 22, 2017
CS150 Introduction to Computer Science 1
CSCI N207 Data Analysis Using Spreadsheet
JavaScript Arrays.
Multidimensional Arrays
Chapter 7 Part 2 Edited by JJ Shepherd
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
MSIS 655 Advanced Business Applications Programming
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Lecture 13: Two-Dimensional Arrays
Multidimensional array
Multidimensional Arrays
CS2011 Introduction to Programming I Multidimensional Arrays
Dr Tripty Singh Arrays.
CHAPTER 2 Arrays and Vectors.
Arrays Arrays A few types Structures of related data items
CHAPTER 2 Arrays and Vectors.
Multidimensional Arrays Section 6.4
Arrays and Matrices Prof. Abdul Hameed.
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Arrays.
Announcements.
Presentation transcript:

Multidimensional Arrays Vectors of Vectors There Could Be More than One

Two Dimensional Array Basics matrices, tables and similar information is often naturally represented by a two-dimensional array first index – row second index - column declaration: basicType arrayName[rows][columns]; example int a[3][4]; accessing a[0][1] = 23; cout << a[1][3]; c[2][4]+=55; either index out of range is still an error a[2][4] = 55; // error larger dimensions are possible: int b[10][100][200];

Two Dimensional Arrays in Loops nested loops are useful with multidimensional arrays for (int i=0; i < length; ++i) for (int j=0; j < width; ++j) cout << a[i][j] << ” ”;

Arrays and Functions if multidimensional array is used as parameters, all but first dimension have to be stated the first dimension can be passed as separate parameter const int width=3; void printArray(int c[][width], int length){ for (int i=0; i < length; ++i) for (int j=0; j < width; ++j) cout << c[i][j] << ” ”; }

Vectors of Vectors alternative to multidimensional arrays outer vector’s type is declared to be a vector vector<vector<int>> a; inner vectors can then be added to the outer vector vector<int> row(width); for(int i=0; i < length; ++i) a.push_back(row); vector elements can be accessed as in multidimensional array a[1][2] = 55; vectors retain all advantages over arrays: have extra functions, carry size, can pass by value/return, can change size on demand example: can use size() in a loop iterating over vector for (int i=0; i < a.size(); ++i) for (int j=0; j < a[i].size(); ++j) cout << a[i][j] << ” ”;

Ragged (Jagged) Array ragged (jagged) array: rows can vary in size a.erase(a.begin()+1); a.erase(a.begin()+1); a.erase(a.begin()+1); Ragged (Jagged) Array ragged (jagged) array: rows can vary in size example: program seats in a (non-square) auditorium or in airplane what does this code do? vector<int> row; vector<vector<int>> a; for(int i=0; i < 4; ++i){ row.push_back(i); a.push_back(row); } a.erase(a.begin()+1); a[2].insert(a[2].begin()+1, 55);

Review Questions Why are multidimensional arrays necessary? How does one declare a two-dimensional array? What is the first index? the second index? How does one access an element of multidimensional array? Why are nested loops useful with multidimensional arrays? How is multidimensional array passed as a parameter to a function? What is vector of vectors? How does one declare a vector of vectors? Initialize it? How are individual elements of a vector of vectors accessed? What are the advantages of a vector of vectors over multidimensional array? What is ragged/jagged array? How does one determine the size of a row in a ragged array? How does one change the number of rows in a ragged array? How does one change the number of elements in a row in a ragged array?