1 CS 105 Lecture 13Vectors version of Wed, Apr 27, 2011, 6:20 pm.

Slides:



Advertisements
Similar presentations
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Advertisements

1 CSC 222: Computer Programming II Spring 2004  vectors  library, templated  advantages over arrays:  const-reference parameters  growable vectors.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
1 CS 105 Lecture 12 Pass- By-Reference, 2-D Arrays Version: Wed, Apr 20, 2011, 6:25 pm.
Programming with Collections Collections in Java Using Arrays Week 9.
Passing Arrays to Functions. COMP104 Lecture 16 / Slide 2 Array Element Pass by Value * Individual array elements can be passed by value or by reference.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
 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.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
Upcoming Events Project Check Due Next Week in Lab –Phase I –main(), menu() Functions and Reading in File –Stub Functions Last Lectures Next Week –Project.
C arrays (Reek, Ch. 8) 1CS 3090: Safety Critical Programming in C.
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
Announcements Final Exam:. Review Passing Arrays as Parameters Pass Array Name into Function int intArray[100]; func(intArray); Accept into Function as.
 CS105 C++ Lecture 2 Arrays. Parameter passing  2 types  Pass-by-value  Pass-by-reference 2.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
CSE1222: Lecture 3The Ohio State University1. Assignment Operations  The C++ assignment operator is: =  Examples: x = 3 * 5; y = x – 7; y = y + 4; Do.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Arrays & Vectors Week 5. The simplest form of the multidimensional array is the two-dimensional array. A two- dimensional array is, in essence, a list.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
CSC 107 – Programming For Science. Today’s Goal  Better understand arrays and how they work  Using array variable & its entries  When calling function,
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
Arrays.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Module 1: Array ITEI222 - Advance Programming Language.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
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.
L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
CS 31 Discussion, Week 2 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Windows Programming Lecture 03. Pointers and 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.
Motivation and Overview
Vectors.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Review for Final Exam.
Pointers & Functions.
Review for Final Exam.
Pointers & Functions.
(Dreaded) Quiz 2 Next Monday.
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Presentation transcript:

1 CS 105 Lecture 13Vectors version of Wed, Apr 27, 2011, 6:20 pm

2 Final Exam next week Wednesday Feb 4, 7:30 pm – 9:30 pm Our usual room Exam will be comprehensive. In addition to questions seen previously, be sure to study Lab 9: Ch 7 ex. 4 Lab 10: Ch 9 ex. 2, 3, 4 Lab 11: Ch 10 ex 11, 12, 13 (Skip Ch10 ex 16, 17) ☞ One page of notes (both sides) allowed ☜

3 An array is a list of values (all of the same type) Each value is named by an index (a.k.a. subscript) Subscripts numbered 0, 1, …. Are passed by reference, not value. Arrays in C++ int a[5], b[3] = {5, 5, 5}; type variable[size]; type variable[size] = {value, value, … } Name specific elements with variable[index expression]; cin >> a[k+2]; b[i] = b[j] + 2;

4 The initial size of an array can vary at runtime. But once you allocate an array, you can't change its size. Arrays are Fixed-Size int len; cin >> len; int a[len]; // (Runtime error if len < 0) len = len+1; // (Doesn't change size of a)

5 A Vector Is a Resizable ArrayNeed to #include The size member function returns current number of elements in vector Vectors in C++ vector a; // size 0 vector b(5); // size 5 vector c(5,8); // size 5; 5 copies of 8 cout << c.size(); // size 5 Declarations: vector variable; vector variable(size); vector variable(size,value);

6 Another Vector Declaration #include using namespace std; int main() { vector x; vector y(5); vector z(5,"hello"); return(0); }

7 Can inspect/update elements using square bracket notation with indexCan assign vector1 = vector2 (makes fresh copy of vector2) Using Vectors vector b(5), c(5,8); c[0] = 10; c[1] = c[0]*2; // c[1] = 20 a = c; // make copy of c a[1] = 30; cout << c[1]; // Still 20

8 Example: Accessing Vector Components #include using namespace std; int main() { vector x; vector y(4); vector z(4,"hello"); y[0] = 6; cin >> z[2]; cout << z[2].length() << endl; // string length 5 return(0); }

The resize Member Function In addition to size, vectors have other member functions. One of them is resize. vectorVar.resize(newSize) changes the size of the vector. We usually make vectors larger. But we can shorten them (toss rightmost elements as necessary). Resizing a long vector can take a long time. Example: vector y(4); … y.resize(y.size()*2);

The push_back Member Function vectorVar.push_back(value) adds the value to the right end of the vector. (New largest subscript.) Easy way to build a vector by repeatedly adding elements. Example: vector y; double newVal; cin >> newVal; while (newVal > 0) { y.push_back(newVal); cin >> newVal; }

Passing Vectors to Functions When passing vectors to functions Pass-by-Value is the default Not like pass-by-reference for arrays Passing a vector by value causes a copy of the vector to be made. Can take lots of time for large vector Can pass vectors by reference for better efficiency.

12 Passing Vectors to Functions #include using namespace std; void pbv(vector x); void pbr(vector &x); int main() { vector v(5); v[0] = 10; pbv(v); cout << v[0] << endl; pbr(v); cout << v[0] << endl; return(0); } void pbv(vector x) { x[0] = 1000; cout << x[0] << endl; } void pbr(vector &x) { x[0] = 1000; cout << x[0] << endl; }