Computer programming 1 Multidimensional Arrays, and STL containers: vectors and maps.

Slides:



Advertisements
Similar presentations
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Advertisements

Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
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.
Vectors, lists and queues
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
C++ Basics Prof. Shermane Austin. Learning Programming Language Basics Data Types – simple Expressions Relational and Logical Operators Conditional Statements.
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter 9: Arrays and Strings
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
C# Programming: From Problem Analysis to Program Design1 Advanced Collections C# Programming: From Problem Analysis to Program Design 3 rd Edition 8.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Arrays.
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Data Structures Using C++ 2E
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.
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
Chapter 8 Arrays and Strings
Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.
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.
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.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan and A. Ranade.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
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 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
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.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
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.
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
CS Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Arrays.
Module 1: Array ITEI222 - Advance Programming Language.
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
Multidimensional Arrays Computer and Programming.
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.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Computer Programming BCT 1113
Two Dimensional Array Mr. Jacobs.
Multi-dimensional Array
Collections Intro What is the STL? Templates, collections, & iterators
Multidimensional Arrays Vectors of Vectors
7 Arrays.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Collections Intro What is the STL? Templates, collections, & iterators
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Presentation transcript:

Computer programming 1 Multidimensional Arrays, and STL containers: vectors and maps

Computer programming 2 Objectives Learn how to declare – multidimensional arrays – Vectors – Maps Illustrate how and when to use – multidimensional arrays – Vectors – Maps

Computer programming 3 Motivations Write a program that, given two cities, will tell us the distance between the two cities How to represent an image? How to represent a matrix? How to represent a two or a three dimensional space? Use multidimensional arrays

Computer programming 4 Multidimensional arrays Declaration typeName name[SIZE_1][SIZE_2]…[SIZE_n] Examples double distances[5][5]; double surface[100][100]; int threeDimension[10][10][10]; Initialization double distances[5][5]={{0,1,2,3,4,5},{2,3}}; double myArray[][2]={{2,3},{4,5}};//a 2x2 array

Computer programming 5 Multidimensional arrays Initialize and reference/access a multidimensional array using for loops – nested for loops Outer loop counting through the first dimension The next Inner loop counting through the second dimension And so on. double surface[10][10]; //initialize to 0 for (int i=0; i<10; i++){ cout <<endl; for (int j=0; j<10; j++){ surface[i][j]=1/(i+j+1.); cout << ++surface[i][j]<< “”; }

Computer programming 6 Visualizing 2D arrays A table with columns (vertical) and rows (horizontal) const int distances[6][6]

Computer programming 7 STL containers and algorithms STL containers – A collection of data structures that can hold any kind of data Vectors Maps sets Queues Lists – They are classes. So, several methods are already defined on them sort find STL algorithms – A collection of algorithms that can be used to manipulate STL containers. – E.g. copy, sort, find, min, max, etc.

Computer programming 8 STL containers: vectors Arrays that we dealt with so far have a size that must be known before the compiling time Arrays do not check whether the index is out of bounds When we implement a function we must pass – The name of the array – The bounds on each dimension To manipulate arrays, you need to implement the functions to do so Arrays are not classes; they are not self-contained objects Vectors allows us to solve all these problems

Computer programming 9 Vectors A vector is a class template similar to a dynamic array. Examples of a vector declaration vector vectorName; vector vectorName(size); Inserting an element at the end of a vector vectorName.push_back(…) Access an element you can either use [] or at vectorName[…];//does not check for “out of bound” vectorName.at(…);//checks for “out of bound” The method size returns the number of elements in the vector; you can use it in a for loop vectorName.size()

Computer programming 10 Vectors: example #include using namespace std; int main() { int n; cin >> n; //read the size from the keyboard vector vec(n); //declare a vector of n elements for(int i=0;i<vec.size();i++) //size is n vec[i]=1./(i+1); vec.push_back(1.0); for(int i=0;i<vec.size();i++) //size is n+1 cout << vec[i]<<“ “; cout << endl; return 0; }

Computer programming 11 Maps Arrays and vectors use integer indices. Can we use other types, such as strings, as indices? – Yes; use maps! Maps: a set of pairs (key, value) – Keys cannot be duplicated – Values can be duplicated – Recall the concept of map/function from math! Declaring a map Map mapName

Computer programming 12 Maps Initialize and access a map using [] or insert To go through a map use a for loop and iterators – Iterators allow us to go through STL containers – To declare an iterator STLContainer::iterator itr – To access the first element in the container use itr.begin() – To increment an iterator use the operator ++ – To detect the end of the user itr.end() (the location after the last element) – itr->first is the key – itr->second is the value

Computer programming 13 Problem Write a program that counts the number of occurrences of words that the users enters. Compute the probability of each word.

Computer programming 14 Maps: Example #include using namespace std; int main() { map dictionary; string str; cin >> str; while (str != "q") { dictionary[str]++; cin >> str; } dictionary.insert(pair ("bel",++dictionary["bel"])); map ::iterator itr; //iterator for (itr=dictionary.begin();itr!=dictionary.end();++itr) cout first " second<<endl; return 0; }