Vectors Jordi Cortadella Department of Computer Science.

Slides:



Advertisements
Similar presentations
Week 8 Arrays Part 2 String & Pointer
Advertisements

Introduction to Programming (in C++) Subprograms: procedures and functions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Introduction to Programming (in C++) Numerical methods I Jordi Cortadella Dept. of Computer Science, UPC.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Computer Science 1620 Loops.
CS150 Introduction to Computer Science 1
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
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.
CSE202: Lecture 14The Ohio State University1 Arrays.
Lesson 7 Arrays CS 1 Lesson 7 -- John Cole1. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored.
Introduction to Programming (in C++) Vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Reasoning with invariants Jordi Cortadella Department of Computer Science.
Data types and their representation Jordi Cortadella Department of Computer Science.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Chars and strings Jordi Cortadella Department of Computer Science.
Matrices Jordi Cortadella Department of Computer Science.
Prime numbers Jordi Cortadella Department of Computer Science.
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
Recursion Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Approximate computations Jordi Cortadella Department of Computer Science.
Sequences Jordi Cortadella Department of Computer Science.
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Data structure design Jordi Cortadella Department of Computer Science.
Polynomials Jordi Cortadella Department of Computer Science.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Sudoku Jordi Cortadella Department of Computer Science.
First steps Jordi Cortadella Department of Computer Science.
Reusing computations Jordi Cortadella Department of Computer Science.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Sorting Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Numerical algorithms Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Pointers & References. Pointers Pointer arithmetic Pointers and arrays Pointer-related typedef’s Pointers and const References.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Parameter passing Jordi Cortadella Department of Computer Science.
Functions Jordi Cortadella Department of Computer Science.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Combinatorial problems Jordi Cortadella Department of Computer Science.
Building Java Programs Chapter 13 Lecture 13-1: binary search and complexity reading:
Chapter 9 Introduction to Arrays Fundamentals of Java.
Reasoning with invariants
Jordi Cortadella Department of Computer Science
for Repetition Structures
For loops, nested loops and scopes
Combinatorial problems
New Structure Recall “average.cpp” program
Object-Oriented Programming Using C++
C++ Arrays.
Jordi Cortadella Department of Computer Science
Jordi Cortadella Department of Computer Science
CS150 Introduction to Computer Science 1
Search algorithms for vectors
Jordi Cortadella and Jordi Petit Department of Computer Science
Jordi Cortadella Department of Computer Science
Presentation transcript:

Vectors Jordi Cortadella Department of Computer Science

Vectors A vector is a data structure that groups values of the same type under the same name. Declaration: vector name(n); A vector contains n elements of the same type (n can be any expression). name[i] refers to the i-th element of the vector (i can also be any expression) Note: use #include in the program Introduction to Programming© Dept. CS, UPC2 01n-2n-1 name:

Declaration and operations Declaration of an empty vector: vector a; Declaration of a vector with an initial size (100 elements): vector a(100); Declaration of a vector of 30 integers in which all elements have an initial value (7): vector a(30, 7); Adding an element at the tail of the vector: a.push_back(x); Removing an element from the tail of the vector: a.pop_back(); Reading the size of a vector: a.size() Introduction to Programming© Dept. CS, UPC3

push_back and pop_back operations Introduction to Programming© Dept. CS, UPC4 vector a; // a.size()=0 a.push_back(3); a.push_back(5); a.push_back(8); // a = [3, 5, 8]; a.size()=3 a.pop_back(); // a = [3, 5]; a.size()=2

Example: counting the last element Write a program that reads a sequence of numbers and prints how many times the last element is repeated in the sequence. Input: Output: 7 appears 4 times Rationale: there is no way to count the number of occurrences of a number without knowing the number. Strategy: store the sequence somewhere (in a vector). Introduction to Programming© Dept. CS, UPC5

Counting the last element // Input: a sequence of integer numbers. // Output: A message indicating the number of occurrences of // the last number has been printed. int main() { // Store the sequence in a vector vector S; int n; while (cin >> n) S.push_back(n); // Adds the element at the tail if (S.size() == 0) { // Special case (empty sequence) cout << "The sequence is empty" << endl; return 0; } int last = S[S.size() - 1]; int count = 0; for (int i = 0; i < S.size(); ++i) { if (S[i] == last) ++count; } cout << last << " appears " << count << " times " << endl; } Introduction to Programming© Dept. CS, UPC6

Vectors: be careful A reference to a vector may not always exist. Example, if i=25 and x has 10 elements, then x[i] does not exist. A runtime error might be produced. Aliases: x[i] and x[j] refer to the same element if i and j have the same value: i = 4; j = 3; A[i] = 5; A[j + 1] = 6; cout << A[i] << endl; // Prints 6 Introduction to Programming© Dept. CS, UPC7

Vectors: be careful Introduction to Programming© Dept. CS, UPC8 vector x(5); x[0] = 0; x[1] = 0; x[2] = 0; x[3] = 0; x[4] = 0; x[x[0]] = 1; cout << x[x[0]] << endl; // What does it print? Prints 0

Min value of a vector // Pre: A is a non-empty vector. // Returns the min value of the vector. int minimum(const vector & A) { int n = A.size(); int m = A[0]; // visits A[0] // loop to visit A[1..n-1] for (int i = 1; i < n; ++i) { if (A[i] < m) m = A[i]; } return m; } Introduction to Programming© Dept. CS, UPC9

Constant parameters and variables A call-by-value parameter requires a copy of the parameter (inefficient for large vectors). Call-by-reference is more efficient, but the callee may unexpectedly modify the parameter. const parameters can be passed by reference and be protected from any modification. – They cannot be written inside the function or passed to another function as a non-const parameter. Variables can also be declared as constant (only writable during declaration). Introduction to Programming© Dept. CS, UPC10

Constant parameters and variables const double Pi = ; // Constant variable void g(vector & V) {... V[i] = V[i - 1] + 1; // Allowed (V is not const)... } int f(const vector & A) {... A[i] = A[i - 1] + 1; // Illegal (A is const) g(A); // Illegal (parameter of g is not const) Pi = 3.14; // Illegal (Pi is const)... } Introduction to Programming© Dept. CS, UPC11

Average value of a vector Given a non-empty vector, return the average value of the elements in the vector. Introduction to Programming© Dept. CS, UPC12 // Pre: a non-empty vector A. // Returns the average value of the elements in A. double average(const vector & A) { int n = A.size(); int sum = 0; for (int i = 0; i < n; ++i) { sum = sum + A[i]; } // Be careful: enforce a “double” result return double(sum)/n; }

Reversing a vector Design a procedure that reverses the contents of a vector: Invariant: Introduction to Programming© Dept. CS, UPC i last-i 0last reversed not reversed

Reversing a vector // Post: A contains the reversed contents // of the input vector void reverse(vector & A) { int last = A.size() - 1; // Calculate the last location to reverse int middle = A.size()/2 - 1; // Reverse one half with the other half for (int i = 0; i <= middle; ++i) { int z = A[i]; A[i] = A[last - i]; A[last - i] = z; } Introduction to Programming© Dept. CS, UPC14

Reversing a vector (another version) // Post: A contains the reversed contents // of the input vector void reverse(vector & A) { int i = 0; int last = A.size() - 1; // Inv: The elements in A[0…i-1] have been // reversed with the elements in // A[last+1…A.size()-1] while (i < last) { int z = A[i]; A[i] = A[last]; A[last] = z; i = i + 1; last = last – 1; } Introduction to Programming© Dept. CS, UPC15

The largest null segment of a vector A null segment is a compact sub-vector in which the sum of all the elements is zero. Let us consider vectors sorted in increasing order. Introduction to Programming© Dept. CS, UPC Null segment Largest null segment

The largest null segment of a vector Observations: – If a null segment contains non-zero elements, then it must contain positive and negative elements. – Let us consider a segment of a vector. If the sum of the elements is positive, then the largest positive value cannot belong to any null segment included in the segment. – The same is true for negative numbers. Introduction to Programming© Dept. CS, UPC17

The largest null segment of a vector Invariant: Introduction to Programming© Dept. CS, UPC The largest null segment is included in the [left…right] segment sum contains the sum of the elements in the [left…right] segment left right sum = 1 Observation: the search will finish when sum = 0. If the segment becomes empty (no elements) the sum will become 0.

The largest null segment of a vector // Pre: A is sorted in increasing order. // Post: contain the indices of the // largest null segment. In the case of an empty // null segment, left > right. void largest_null_segment (const vector & A, int& left, int& right) left = 0; right = A.size()-1; int sum = sum_vector(A); // Calculates the sum of A while (sum != 0) { if (sum > 0) { sum = sum – A[right]; right = right – 1; } else { sum = sum – A[left]; left = left + 1; } } // sum = 0 and the largest segment is A[left...right] } Introduction to Programming© Dept. CS, UPC19

typedef Typedef declarations create synonyms for existing types: // Declaration of the type typedef vector listTemperatures; // Declaration of a variable listTemperatures MyTemp; // The parameter of a function double maxTemp(const listTemperatures& L) {... } Introduction to Programming© Dept. CS, UPC20

Summary Vectors are data structures that store values of the same type. Unlike sequences, elements can be read and written at any time. Passing vectors as parameters: – Try to avoid passing by value (inefficient). – Pass by constant reference whenever possible. Introduction to Programming© Dept. CS, UPC21