Introduction to Programming (in C++) Advanced examples Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.

Slides:



Advertisements
Similar presentations
Introduction to Programming (in C++) Subprograms: procedures and functions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science,
Advertisements

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 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Objectives Find terms of a geometric sequence, including geometric means. Find the sums of geometric series.
Introduction to Programming (in C++) Vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Introduction to Programming (in C++) Introduction Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Data types and visibility Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. 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.
Introduction to Programming (in C++) Conclusions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Reasoning with invariants Jordi Cortadella Department of Computer Science.
Data types and their representation Jordi Cortadella Department of Computer Science.
Chars and strings Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Data structures Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
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.
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.
Lists. Container Classes Many applications in Computer Science require the storage of information for collections of entities e.g. a student registration.
Data structure design Jordi Cortadella Department of Computer Science.
Greatest Common Divisor Jordi Cortadella Department of Computer Science.
Structures Jordi Cortadella Department of Computer Science.
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.
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Vectors Jordi Cortadella Department of Computer Science.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
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.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Basic concepts of C++ Presented by Prof. Satyajit De
Jordi Cortadella, Ricard Gavaldà, Fernando Orejas
Reasoning with invariants
Jordi Cortadella Department of Computer Science
For loops, nested loops and scopes
Combinatorial problems
Jordi Cortadella Department of Computer Science
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Jordi Cortadella Department of Computer Science
Let’s all Repeat Together
Search algorithms for vectors
Containers: Queue and List
Jordi Cortadella and Jordi Petit Department of Computer Science
Jordi Cortadella Department of Computer Science
CS150 Introduction to Computer Science 1
CS148 Introduction to Programming II
CS150 Introduction to Computer Science 1
Chapter 18 Recursion.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Introduction to Programming (in C++) Advanced examples
Presentation transcript:

Introduction to Programming (in C++) Advanced examples Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Sports tournament Design a program that reads the participants in a knockout tournament and the list of results for each round. The program must write the name of the winner. Assumptions: – The number of participants is a power of two. – The list represents the participation order, i.e. in the first round, the first participant plays with the second, the third with the fourth, etc. In the second round, the winner of the first match plays against the winner of the second match, the winner of the third match plays against the winner of the fourth match, etc. At the end, the winner of the first semi-final will play against the winner of the second semi- final. The specification of the program could be as follows: // Pre: the input contains the number of players, // the players and the results of the tournament. // Post: the winner has been written at the output. Introduction to Programming© Dept. CS, UPC2

Sports tournament Nadal – Djokovic 3-0 Nadal – Berdych 3-1 Nadal – Murray 2-0 Berdych – Soderling 3-1 Federer – Djokovic 2-3 Federer – Ferrer 3-1 Djokovic – Roddick 3-2 Introduction to Programming© Dept. CS, UPC3 Input (example): 8 Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick

Sports tournament A convenient data structure that would enable an efficient solution would be a vector with 2n-1 locations (n is the number of participants): – The first n locations would store the participants. – The following n/2 locations would store the winners of the first round. – The following n/4 locations would store the winners of the second round, etc. – The last location would store the name of the winner. Introduction to Programming© Dept. CS, UPC4

Sports tournament Input: 8 Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Berdych Federer Djokovic Nadal Djokovic Nadal Introduction to Programming© Dept. CS, UPC5 First round Second round Third round Winner

Sports tournament The algorithm could run as follows: – First, it reads the number of participants and their names. They will be stored in the locations 0…n-1 of the vector. – Next, it fills up the rest of the locations. Two pointers might be used. The first pointer (j) points at the locations of the players of a match. The second pointer (k) points at the location where the winner will be stored. // Inv: players[n..k-1] contains the // winners of the matches stored // in players[0..j-1] Introduction to Programming© Dept. CS, UPC6

Sports tournament Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Introduction to Programming© Dept. CS, UPC7 Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Berdych Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Berdych Federer Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Berdych Federer Djokovic Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Berdych Federer Djokovic Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Berdych Federer Djokovic Nadal Djokovic Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Berdych Federer Djokovic Nadal Djokovic Nadal

Sports tournament int main() { int n; cin >> n; // Number of participants vector players(2n - 1); // Read the participants for (int i = 0; i > players[i]; int j = 0; // Read the results and calculate the winners for (int k = n; k < 2n - 1; ++k) { int score1, score2; cin >> score1 >> score2; if (score1 > score2) players[k] = players[j]; else players[k] = players[j + 1]; j = j + 2; } cout << players[2n - 2] << endl; } Introduction to Programming© Dept. CS, UPC8

Sports tournament Exercise: Modify the previous algorithm using only a vector with n strings, i.e., vector players(n) Introduction to Programming© Dept. CS, UPC9

Permutations For N=4: Given a number N, generate all permutations of the numbers 1…N in lexicographical order. For N=4: Introduction to Programming© Dept. CS, UPC

Permutations Note: // Structure to represent the prefix of a permutation. // When all the elements are used, the permutation is // complete. // Note: used[i] represents the element i+1 struct Permut { vector v; // stores a partial permutation (prefix) vector used; // elements used in v }; Introduction to Programming© Dept. CS, UPC v: used:

Permutations void BuildPermutation(Permut& P, int i); Pre: Post: // Pre: P.v[0..i-1] contains a prefix of the permutation. // P.used indicates the elements present in P.v[0..i-1] // Post: All the permutations with prefix P.v[0..i-1] have // been printed in lexicographical order. Introduction to Programming© Dept. CS, UPC12 prefixprefixemptyempty i

Permutations void BuildPermutation(Permut& P, int i) { if (i == P.v.size()) { PrintPermutation(P); // permutation completed } else { // Define one more location for the prefix // preserving the lexicographical order of // the unused elements for (int k = 0; k < P.used.size(); ++k) { if (not P.used[k]) { P.v[i] = k + 1; P.used[k] = true; BuildPermutation(P, i + 1); P.used[k] = false; } } } } Introduction to Programming© Dept. CS, UPC13

Permutations int main() { int n; cin >> n; // will generate permutations of {1..n} Permut P; // creates a permutation with empty prefix P.v = vector (n); P.used = vector (n, false); BuildPermutation(P, 0); } void PrintPermutation(const Permut& P) { int last = P.v.size() – 1; for (int i = 0; i < last; ++i) cout << P.v[i] << “ ”; cout << P.v[last] << endl; } Introduction to Programming© Dept. CS, UPC14

Sub-sequences summing n Given a sequence of positive numbers, write all the sub-sequences that sum another given number n. The input will first indicate the number of elements in the sequence and the target sum. Next, all the elements in the sequence will follow, e.g Introduction to Programming© Dept. CS, UPC15 number of elements target sum sequence

Sub-sequences summing n > Introduction to Programming© Dept. CS, UPC16

Sub-sequences summing n How do we represent a subset of the elements of a vector? – A Boolean vector can be associated to indicate which elements belong to the subset. represents the subset {6,1,5} Introduction to Programming© Dept. CS, UPC falsetrue false truefalse Value: Chosen:

Sub-sequences summing n How do we generate all subsets of the elements of a vector? Recursively. – Decide whether the first element must be present or not. – Generate all subsets with the rest of the elements Introduction to Programming© Dept. CS, UPC true?????? false?????? Subsets containing 3 Subsets not containing 3

Sub-sequences summing n How do we generate all the subsets that sum n? – Pick the first element (3) and generate all the subsets that sum n-3 starting from the second element. – Do not pick the first element, and generate all the subsets that sum n starting from the second element. Introduction to Programming© Dept. CS, UPC true?????? false??????

Sub-sequences summing n struct Subset { vector values; vector chosen; }; void main() { // Read number of elements and sum int n, sum; cin >> n >> sum; // Read sequence Subset s; s.values = vector (n); s.chosen = vector (n,false); for (int i = 0; i > s.values[i]; // Generates all subsets from element 0 generate_subsets(s, 0, sum); } Introduction to Programming© Dept. CS, UPC20

Sub-sequences summing n void generate_subsets(Subset& s, int i, int sum); // Pre: s.values is a vector of n positive values and // s.chosen[0..i-1] defines a partial subset. // s.chosen[i..n-1] is false. // Post: prints the subsets that agree with // s.chosen[0..i-1] such that the sum of the // chosen values in s.values[i..n-1] is sum. // Terminal cases: // · sum < 0  nothing to print // · sum = 0  print the subset // · i >= n  nothing to print Introduction to Programming© Dept. CS, UPC21

Sub-sequences summing n void generate_subsets(Subset& s, int i, int sum) { if (sum >= 0) { if (sum == 0) print_subset(s); else if (i < s.values.size()) { // Recursive case: pick i and subtract from sum s.chosen[i] = true; generate_subsets(s, i + 1, sum - s.values[i]); // Do not pick i and maintain the sum s.chosen[i] = false; generate_subsets(s, i + 1, sum); } Introduction to Programming© Dept. CS, UPC22

Sub-sequences summing n void print_subset(const Subset& s) { // Pre: s.values contains a set of values and // s.chosen indicates the values to be printed // Post: the chosen values have been printed in cout for (int i = 0; i < s.values.size(); ++i) { if (s.chosen[i]) cout << s.values[i] << “ “; } cout << endl; } Introduction to Programming© Dept. CS, UPC23