Patterns: Working with Arrays

Similar documents
Operator Overloading

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

Operator Overloading

Pointers and References

Defensive Programming

Computer Science II Lecture 2 Strings, Vectors and Recursion

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Encapsulation. Contents. Steven Zeil. July 17, Encapsulation Encapsulation in C Classes 4. 3 Hiding Attributes 8

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

Lesson 13 - Vectors Dynamic Data Storage

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

Basic Templates Intro

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 4

6. Pointers, Structs, and Arrays. 1. Juli 2011

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

A brief introduction to C++

CS93SI Handout 04 Spring 2006 Apr Review Answers

Copying Data. Contents. Steven J. Zeil. November 13, Destructors 2

The Structure of a C++ Program

Templates and Vectors

Due Date: See Blackboard

C++ Primer for CS175

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 3

The Stack, Free Store, and Global Namespace

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

G52CPP C++ Programming Lecture 18

by Pearson Education, Inc. All Rights Reserved. 2

EL2310 Scientific Programming

QUIZ. What is wrong with this code that uses default arguments?

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

CS 103 Lab - Party Like A Char Star

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. Array Terminology. Array Terminology 8/23/2014. Arrays Hold Multiple Values

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

Sets and MultiSets. Contents. Steven J. Zeil. July 19, Overview of Sets and Maps 4

QUIZ. What are 3 differences between C and C++ const variables?

Memory and Pointers written by Cathy Saxton

G52CPP C++ Programming Lecture 17

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

Errors. Lecture 6. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

Lab#5 Due Wednesday, February 25, at the start of class. Purpose: To develop familiarity with C++ pointer variables

Chapter 1 Getting Started

Fast Introduction to Object Oriented Programming and C++

CS 103 Unit 12 Slides

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

pointers + memory double x; string a; int x; main overhead int y; main overhead

CSE 303: Concepts and Tools for Software Development

Intermediate Programming, Spring 2017*

Chapter 16. Templates. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Due Date: See Blackboard

CA31-1K DIS. Pointers. TA: You Lu

Heap Arrays and Linked Lists. Steven R. Bagley

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

15. Pointers, Algorithms, Iterators and Containers II

EE 355 Lab 4 - Party Like A Char Star

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Object-Oriented Programming for Scientific Computing

CS 103 Lab 6 - Party Like A Char Star

EE 355 Unit 10. C++ STL - Vectors and Deques. Mark Redekopp

Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13

COEN244: Class & function templates

CS 103 Unit 11. Linked Lists. Mark Redekopp

C:\Temp\Templates. Download This PDF From The Web Site

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

Common Modifications of Class Members

Vectors. A Computer Science Tapestry 8.1

Common Modifications of Class Members

C++ For Science and Engineering Lecture 15

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

CS 103 Unit 13 Slides

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

Chapter 5 Errors. Hyunyoung Lee. Based on slides by Bjarne Stroustrup.

Recursion. Contents. Steven Zeil. November 25, Recursion 2. 2 Example: Compressing a Picture 4. 3 Example: Calculator 5

Instantiation of Template class

CS 103 Unit 11. Linked Lists. Mark Redekopp

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Name SECTION: 12:45 2:20. True or False (12 Points)

Chapter 7: Arrays Copyrig opy ht rig 2012 Pea e rson a Educa Educ ti a on, Inc I. nc

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

Common Modifications of Class Members

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Data Structures Lecture 3 Order Notation and Recursion


1. Which of the following best describes the situation after Line 1 has been executed?

1 Memory management in C++

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Topics. bool and string types input/output library functions comments memory allocation templates classes

Short Notes of CS201

Vector and Free Store (Vectors and Arrays)

Linked lists Tutorial 5b

Transcription:

Steven Zeil October 14, 2013

Outline 1 Static & Dynamic Allocation Static Allocation Dynamic Allocation 2 Partially Filled Arrays Adding Elements Searching for Elements Removing Elements 3 Arrays and Templates 4 Vectors 5 Multi-Dimension Arrays Arrays of Arrays

Static & Dynamic Allocation Outline I 1 Static & Dynamic Allocation Static Allocation Dynamic Allocation 2 Partially Filled Arrays Adding Elements Searching for Elements Removing Elements 3 Arrays and Templates 4 Vectors 5 Multi-Dimension Arrays Arrays of Arrays

Static & Dynamic Allocation Static Allocation Static Allocation const i n t MaxItems = 1 00; // Names o f i t e m s s t d : : s t r i n g itemnames [ MaxItems ] ; Simple Best suited to situations where we know the number of items at compile time s t r i n g monthnames [ 1 2 ] ;

Static & Dynamic Allocation Static Allocation How Many Elements Do We Need? If we don t know how many elements we really need, we need to guess Too few, and the program crashes Too many, and we waste space (and maybe time)

Static & Dynamic Allocation Dynamic Allocation Dynamic Allocation Use pointer to array Allocated on heap Needs to be deleted afterwards

Static & Dynamic Allocation Dynamic Allocation Example: bidders.h extern i n t n B i d d e r s ; extern Bidder b i d d e r s ; void r e a d B i d d e r s ( s t d : : i s t r e a m& i n ) ;

Static & Dynamic Allocation Dynamic Allocation Example: bidders.cpp bidders.cpp ❶ Here we read the desired size of the arrays from the input ❷ Here we use that value to actually allocate arrays of the desired size ❸ Once that is done, we can access the arrays in the usual manner using the [ ].

Static & Dynamic Allocation Dynamic Allocation Dynamic Array Declarations are Confusing extern Bidder b i d d e r s ; We can t tell by looking at this declaration whether it is intended to point to a single bidder, or an array of bidders Need to rely on documentation

Static & Dynamic Allocation Dynamic Allocation Why Are Arrays Different? Because arrays are "really" pointers... array1 = array2; does not copy the array When arrays are passed to functions, we are actually passing a pointer passing arrays is fast and efficient changes to the array can be seen by the caller i n t x ; i n t a r r [ 1 0 0 ] ; void. foo ( i n t i, i n t a ) ; foo ( x,. a ) ; void foo ( i n t i, i n t a ) { i = i + 1 ; // does not a f f e c t x a [ 0 ] = i ; // does a f f e c t a r r }

Partially Filled Arrays Outline I 1 Static & Dynamic Allocation Static Allocation Dynamic Allocation 2 Partially Filled Arrays Adding Elements Searching for Elements Removing Elements 3 Arrays and Templates 4 Vectors 5 Multi-Dimension Arrays Arrays of Arrays

Partially Filled Arrays Partially Filled Arrays To work with a partially filled array, we generally need to have access to the array itself an integer counter indicating how many elements are currently in the array the size or length of the list an integer counter indicating the maximum nmber of elements we can have without overflowing the array the capacity of the list

Partially Filled Arrays Adding Elements Adding Elements Variations include adding to the end adding in the middle adding in order

Partially Filled Arrays Adding Elements Add to the end void addtoend ( s t d : : s t r i n g a r r a y, i n t& s i z e, s t d : : s t r i n g { a r r a y [ s i z e ] = v a l u e ; ++s i z e ; } Assumes that we have a separate integer indicating how many elements are in the array and that the "true" size of the array is at least one larger than the current value of that counter

Partially Filled Arrays Adding Elements Add to the middle addelement (array, size, index, value) Adds value into array[index], shifting all elements already in positions index..size-1 up one, to make room. Increments the size variable If we have this and we do addelement ( a r r a y, 3, 1, " Smith " ) ; we should get this.

Partially Filled Arrays Adding Elements Add to the middle: implementation addelement.cpp You can try out the addtoend and addelement functions here. Try them out with different inputs until you understand how they work.

Partially Filled Arrays Adding Elements Add in order int addinorder (array, size, value) Assume the elements of the array are already in order Find the position where value could be added to keep everything in order, and insert it there. Return the position where it was inserted If we have this and we do addinorder ( a r r a y, 3, " C l a r k e " ) ; we should get this

Partially Filled Arrays Adding Elements Add in order implementation This works: i n t addinorder ( s t d : : s t r i n g a r r a y, i n t& s i z e, s t d : : s t r i n g v a l u e ) { // Find where to i n s e r t i n t pos = 0 ; while ( pos < s i z e && v a l u e > a r r a y [ pos ] ) ++pos ; addelement ( a r r a y, s i z e, pos, v a l u e ) ; return pos ; }

Partially Filled Arrays Adding Elements Add in order (streamlined) This is a bit faster: i n t addinorder ( ( s t d : : s t r i n g a r r a y, i n t& s i z e, s t d : : s t r i n g v a l u e ) { // Make room f o r the i n s e r t i o n i n t tobemoved = s i z e 1 ; while ( tobemoved >= 0 && v a l u e < a r r a y [ tobemoved ] ) { a r r a y [ tobemoved+1] = a r r a y [ tobemoved ] ; tobemoved ; } // I n s e r t the new v a l u e a r r a y [ tobemoved+1] = v a l u e ; ++s i z e ; return tobemoved +1; } Try This: You can try out the two addinorder functions here.

Partially Filled Arrays Searching for Elements Sequential Search Search an array for a given value, returning the index where found or -1 if not found. seqsearch.cpp How many elements does this visit in the worst case? How many elements does this visit on average? if searchitem is in the array? if searchitem is not in the array?

Partially Filled Arrays Searching for Elements Sequential Search 2 Search an array for a given value, returning the index where found or -1 if not found. i n t s e q S e a r c h ( const i n t l i s t [ ], i n t l i s t L e n g t h, i n t s e a r c { i n t l o c ; f o r ( l o c = 0 ; l o c < l i s t L e n g t h ; l o c++) i f ( l i s t [ l o c ] == s e a r c h I t e m ) return l o c ; } return 1;

Partially Filled Arrays Searching for Elements Sequential Search (Ordered Data) Search an array for a given value, returning the index where found or -1 if not found. If data is in order, we can stop early as soon as list[loc] > searchitem

Partially Filled Arrays Searching for Elements seqorderedsearch i n t s e qorderedsearch ( const i n t l i s t [ ], i n t l i s t L e n g t h, i n { i n t l o c = 0 ; } while ( l o c < l i s t L e n g t h && l i s t [ l o c ] < s e a r c h I t e m ) { ++l o c ; } i f ( l o c < l i s t L e n g t h && l i s t [ l o c ] == s e a r c h I t e m ) return l o c ; e l s e return 1; How many elements does this visit in the worst case? How many elements does this visit on average? if searchitem is in the array? if searchitem is not in the array?

Partially Filled Arrays Removing Elements Removing Elements removeelement (std::string* array, int& size; int index) void removeelement ( s t d : : s t r i n g a r r a y, i n t& s i z e, i n t i n d e x ) { i n t tobemoved = i n d e x + 1 ; while ( tobemoved < s i z e ) { a r r a y [ tobemoved 1] = a r r a y [ tobemoved ] ; ++tobemoved ; } s i z e ; } Try This: You can try out the removeelement function here.

Arrays and Templates Outline I 1 Static & Dynamic Allocation Static Allocation Dynamic Allocation 2 Partially Filled Arrays Adding Elements Searching for Elements Removing Elements 3 Arrays and Templates 4 Vectors 5 Multi-Dimension Arrays Arrays of Arrays

Arrays and Templates arrayutils - first draft The functions we have developed in this section can be used in many different programs, so it might be useful to collect them into an "Array Utilities" module. Header: arrayutils0.h Compilation unit: arrayutils0.cpp Test driver: testarrayutils0.cpp Question: The main function will not compile. Why?

Arrays and Templates Overloading One solution is to provide different versions of each function for each kind of array: // Search an o r d e r e d a r r a y f o r a g i v e n value, r e t u r n i n g // found or 1 i f not found. i n t s e qorderedsearch ( const i n t l i s t [ ], i n t l i s t L e n g t h, i n i n t s e qorderedsearch ( const char l i s t [ ], i n t l i s t L e n g t h, i n t s e qorderedsearch ( const double l i s t [ ], i n t l i s t L e n g t h i n t s e qorderedsearch ( const f l o a t l i s t [ ], i n t l i s t L e n g t h, i n t s e qorderedsearch ( const s t d : : s t r i n g l i s t [ ], i n t l i s t L e with nearly identical function bodies for each one. called overloading the function name Tedious Not possible to cover all possible data types

Arrays and Templates Function Templates A function template is a pattern for an infinite number of possible functions. Contains special symbols called template parameters that function like blank spaces in a form Symbols can be replaced to "fill out" the form Called instantiating the template

Arrays and Templates A Simple Template swap.cpp The template header announces that is a template. Without this, it would look like an ordinary function The header lists the template parameters for this pattern. These will be replaced when the template is used (instantiated). Can have more than one (comma-separated) Each preceded by the word typename or class Each template parameter must appear as a type somewhere in the function s parameter list The swap template is declared in the std library header <algorithm>

Arrays and Templates Using A Function Template Import it from the appropriate header (if not declared locally) Call it with the desired parameters Compiler figures out what substitutions to make #i n c l u de <a l g o r i t h m > using. namespace s t d ; s t r i n g a = " abc " ; s t r i n g b = " bcde " ; swap ( a, b ) ; // c o m p i l e r r e p l a c e s "T" by " s t r i n g " i n t i = 0 ; i n t j = 2 ; swap ( i, j ) ; // c o m p i l e r r e p l a c e s "T" by " i n t "

Arrays and Templates Some Other std Function Templates min(x,y) returns the smaller of two values max(x,y) returns the larger of two values fill_n(array, N, value) fills array[0]..array[n-1] with value

Arrays and Templates Building a Library of Array Templates Second try, using templates: Header: arrayutils.h Test driver: testarrayutils.cpp

Arrays and Templates Function Templates Are Not Functions Is it a problem that we have the bodies in a.h file?

Arrays and Templates Function Templates Are Not Functions Is it a problem that we have the bodies in a.h file? No, because function templates are not functions, they are patterns for functions.

Arrays and Templates Patterns versus Instances In the same way that This is not an admission application. But this is an admission application. These are not functions. The code generated by the compiler in response to our calls are the functions.

Vectors Outline I 1 Static & Dynamic Allocation Static Allocation Dynamic Allocation 2 Partially Filled Arrays Adding Elements Searching for Elements Removing Elements 3 Arrays and Templates 4 Vectors 5 Multi-Dimension Arrays Arrays of Arrays

Vectors Keeping Information Together One criticism of functions like void addtoend ( s t d : : s t r i n g a r r a y, i n t& s i z e, s t d : : s t r i n g v a l u e ) ; i n t addinorder ( s t d : : s t r i n g a r r a y, i n t& s i z e, s t d : : s t r i n g v a l u e ) ; is that they separate the array, the size, and the capacity Easy for programmers to lose track of which integer counter applies to which array Complicates functions to pass this information separately.

Vectors Wrapping arrays within structs One solution: use a struct to gather the related elements together: /// A c o l l e c t i o n o f i t e m s s t r u c t ItemSequence { s t a t i c const i n t c a p a c i t y = 500; i n t s i z e ; Item data [ c a p a c i t y ] ; } ;

Vectors A Better Version Using dynamic allocation, we can be more flexible about the capacity: s t r u c t ItemSequence { i n t c a p a c i t y ; i n t s i z e ; Item data ; } ; ItemSequence ( i n t cap ) ; void addtoend ( Item item ) ;

Vectors Implementing the Sequence ItemSequence : : ItemSequence ( i n t cap ) : c a p a c i t y ( cap ), s i z e ( 0 ) { data = new Item [ c a p a c i t y ] ; } void ItemSequence : : addtoend ( Item item ) { i f ( s i z e < c a p a c i t y ) { data [ s i z e ] = item ; ++s i z e ; } e l s e c e r r << " E r r o r : ItemSequence i s f u l l " << e n d l ; }

Vectors Vectors the Un-Array The vector is an array-like structure provided in the std header <vector>. Think of it as an array that can grow at the high end actually another kind of template, a class template

Vectors Declaring Vectors s t d : : v e c t o r <int > v i ; // a v e c t o r o f 0 i n t s s t d : : v e c t o r <s t d : : s t r i n g > vs ( 1 0 ) ; // a v e c t o r o f 10 // empty s t r i n g s s t d : : v e c t o r <f l o a t > v f ( 5, 1. 0 ) ; // a v e c t o r o f 5 // f l o a t s, a l l 1. 0 The type name inside the < > describes the elements contained inside the vector

Vectors Accessing Elements in a Vector Use the [ ] just as with an array: v e c t o r <int > v ( 1 0 ) ; f o r ( i n t i = 0 ; i < 1 0 ; ++i ) { i n t j ; c i n >> j ; v [ i ] = j + 1 ; cout << v [ i ] << e n d l ; }

Vectors Size of a Vector void foo (vector<int>& v) { } for (int i = 0; i < { } int j; cin >> j; v[i] = j + 1; cout << v[i] << endl; v.size() ; ++i) Vectors remember their own size Accessed via size() With vectors, we do not over-allocate extra spaces in the vector and use a separate counter

Vectors Adding to a Vector Adding to the end: This is how we "grow" a vector. v. push_back ( x ) ;

Vectors Adding to the Middle template <c l a s s Vector, c l a s s T> void addelement ( Vector& v, i n t index, T v a l u e ) { // Make room f o r the i n s e r t i o n i n t tobemoved = v. s i z e ( ) 1 ; v. push_back ( v a l u e ) ; // expand v e c t o r by 1 s l o t while ( tobemoved >= i n d e x ) { v [ tobemoved+1] = v [ tobemoved ] ; tobemoved ; } // I n s e r t the new v a l u e v [ i n d e x ] = v a l u e ; }

Vectors Adding to the Middle: v2 Actually, vector s provide a built-in operation for inserting into the middle: s t r i n g a ; // a r r a y o f s t r i n g s v e c t o r <s t r i n g > v ; i n t k, n ;. v. i n s e r t ( v. b e g i n ()+k, " H e l l o " ) ; addelement ( a, n, k, " H e l l o " ) ; The last two statements do the same thing. But insert is a built-in member function of vectors The way of specifying the position is a bit odd. v.begin() is a pointer to the start of the vector Like a is a pointer to the start of the array v.begin() + k is a pointer to v[k] Like a + k is a pointer to the a[k]

Vectors Add in Order to Vector template <c l a s s Vector, c l a s s T> i n t addinorder ( Vector& v, T v a l u e ) { // Make room f o r the i n s e r t i o n i n t tobemoved = v. s i z e ( ) 1 ; v. push_back ( v a l u e ) ; // expand v e c t o r by 1 s l o t while ( tobemoved >= 0 && v a l u e < v [ tobemoved ] ) { v [ tobemoved+1] = v [ tobemoved ] ; tobemoved ; } // I n s e r t the new v a l u e v [ tobemoved+1] = v a l u e ; return tobemoved +1; }

Vectors Advantages of Vectors Can grow as necessary Need not worry about pointers, allocation, delete Vectors can copy: v1 = v2;

Vectors Disadvantages of Vectors A bit slower than arrays push_back is sometimes very slow Can waste a lot of storage but so can arrays if we have to guess at max Harder to work with in a debugger

Multi-Dimension Arrays Outline I 1 Static & Dynamic Allocation Static Allocation Dynamic Allocation 2 Partially Filled Arrays Adding Elements Searching for Elements Removing Elements 3 Arrays and Templates 4 Vectors 5 Multi-Dimension Arrays Arrays of Arrays

Multi-Dimension Arrays Multi-Dimension Arrays Used in situations where we would arrange data into a table rather than a list. If we know how many rows and columns, Allocate array as i n t a r r a y [ numrows ] [ numcols ] ; Access as array[i][j] 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2 3,0 3,1 3,2

Multi-Dimension Arrays Example test2dimfixed.cpp

Multi-Dimension Arrays Arrays of Arrays Arrays of Arrays If we do not know (at compile time) how many rows and columns, Declare array as i n t a r r a y ; Allocate as an array of pointers: a r r a y = new i n t [ numrows ] ; Each row must then be allocated as an array a r r a y [ i ] = new i n t [ numcols ] ; Access as array[i][j] Example: test2dimrowscols.cpp 0 1 2 3 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2 3,0 3,1 3,2

Multi-Dimension Arrays Arrays of Arrays Linearized Arrays We can map 2-D arrays onto a linear structure i n t i n d e x ( i n t i, j, numcols ) { return j + i numcolss ; } 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2 3,0 3,1 3,2

Multi-Dimension Arrays Arrays of Arrays Linearized Array Code 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2 3,0 3,1 3,2 Declare array as i n t a r r a y ; Allocate as a 1-D array a r r a y = new i n t [ numrows numcols ] ; Access as array[index(i,j,numcols)]