Standard Candy Vector Operations in C++

 
Chapter 10
 
Paul Bodily
 
Few reminders with vectors
 
Always have to remember
#include <vector>
using namespace std;
Also,
 
Example
 
 
Problem spec: Trick or Treat!
 
1.
Create a vector of strings 
standardCandyBowl
 representing standard types of candy: 3
Musketeers, Milky Ways, and Snickers
2.
Write a function 
printVector() 
to print out the list of strings using a while loop
 
Problem spec: Trick or Treat!
 
1.
Create a vector of strings 
standardCandyBowl
 representing standard types of candy: 3
Musketeers, Milky Ways, and Snickers
2.
Write a function 
printVector() 
to print out the list of strings using a while loop
3.
Modify the program to use a for loop instead of a while loop
 
Problem spec: Trick or Treat!
 
1.
Create a vector of strings 
standardCandyBowl
 representing standard types of candy: 3
Musketeers, Milky Ways, and Snickers
2.
Write a function 
printVector() 
to print out the list of strings using a while loop
3.
Modify the program to use a for loop instead of a while loop
4.
Write a function to create a vector 
myCandyBowl 
that starts by copying the vector
standardCandyBowl
 and then prompts the user to add another candies
 
Problem spec: Trick or Treat!
 
1.
Create a vector of strings 
standardCandyBowl
 representing standard types of candy: 3
Musketeers, Milky Ways, and Snickers
2.
Write a function 
printVector() 
to print out the list of strings using a while loop
3.
Modify the program to use a for loop instead of a while loop
4.
Write a function to create a vector 
myCandyBowl 
that starts by copying the vector
standardCandyBowl
 and then prompts the user to add another candies
5.
Write a function 
selectRandom()
 to sample a random string from a vector
 
Problem spec: Trick or Treat!
 
1.
Create a vector of strings 
standardCandyBowl
 representing standard types of candy: 3
Musketeers, Milky Ways, and Snickers
2.
Write a function 
printVector() 
to print out the list of strings using a while loop
3.
Modify the program to use a for loop instead of a while loop
4.
Write a function to create a vector 
myCandyBowl 
that starts by copying the vector
standardCandyBowl
 and then prompts the user to add another candies
5.
Write a function 
selectRandom()
 to sample a random string from a vector
6.
Write a function 
selectNRandom() 
to sample 
n
 random candies (with replacement) from
myCandyBowl
 
Problem spec: Trick or Treat!
 
1.
Create a vector of strings 
standardCandyBowl
 representing standard types of candy: 3
Musketeers, Milky Ways, and Snickers
2.
Write a function 
printVector() 
to print out the list of strings using a while loop
3.
Modify the program to use a for loop instead of a while loop
4.
Write a function to create a vector 
myCandyBowl 
that starts by copying the vector
standardCandyBowl
 and then prompts the user to add another candies
5.
Write a function 
selectRandom()
 to sample a random string from a vector
6.
Write a function 
selectNRandom() 
to sample 
n
 random candies (with replacement) from
myCandyBowl
7.
Write a function 
countMatches()
 that, given a particular string, counts how many times it appears
in a vector
 
Example
Standard Candies:
0. Milky Way
1. 3 Musketeers
2. Snickers
 
EXAMPLE
 
Example
Standard Candies:
0. Milky Way
1. 3 Musketeers
2. Snickers
 
My Candy Bowl:
0. Milky Way
1. 3 Musketeers
2. Snickers
3. Reese's PB Cups
 
Example
Standard Candies:
0. Milky Way
1. 3 Musketeers
2. Snickers
 
My Candy Bowl:
0. Milky Way
1. 3 Musketeers
2. Snickers
3. Reese's PB Cups
 
***** Austin *****
0. Reese's PB Cups
 
Example
Standard Candies:
0. Milky Way
1. 3 Musketeers
2. Snickers
 
My Candy Bowl:
0. Milky Way
1. 3 Musketeers
2. Snickers
3. Reese's PB Cups
 
***** Austin *****
0. Reese's PB Cups
1. Snickers
2. 3 Musketeers
3. Reese's PB Cups
4. 3 Musketeers
5. Reese's PB Cups
 
Example
Standard Candies:
0. Milky Way
1. 3 Musketeers
2. Snickers
 
My Candy Bowl:
0. Milky Way
1. 3 Musketeers
2. Snickers
3. Reese's PB Cups
 
***** Austin *****
0. Reese's PB Cups
1. Snickers
2. 3 Musketeers
3. Reese's PB Cups
4. 3 Musketeers
5. Reese's PB Cups
 
Austin had 3 Reese's PB Cups
 
Example
Standard Candies:
0. Milky Way
1. 3 Musketeers
2. Snickers
 
My Candy Bowl:
0. Milky Way
1. 3 Musketeers
2. Snickers
3. Reese's PB Cups
 
***** Austin *****
0. Reese's PB Cups
1. Snickers
2. 3 Musketeers
3. Reese's PB Cups
4. 3 Musketeers
5. Reese's PB Cups
 
Austin had 3 Reese's PB Cups
Random Dad tax = 0.335223
Austin owes dad 1.00567 Reese's PB Cups
 
Example
Standard Candies:
0. Milky Way
1. 3 Musketeers
2. Snickers
 
My Candy Bowl:
0. Milky Way
1. 3 Musketeers
2. Snickers
3. Reese's PB Cups
 
***** Austin *****
0. Reese's PB Cups
1. Snickers
2. 3 Musketeers
3. Reese's PB Cups
4. 3 Musketeers
5. Reese's PB Cups
 
Austin had 3 Reese's PB Cups
Random Dad tax = 0.335223
Austin owes dad 1.00567 Reese's PB Cups
 
***** Siena *****
...
 
Siena had 1 Reese's PB Cups
Random Dad tax = 0.513401
Siena owes dad 0.513401 Reese's PB Cups
 
***** Autumn *****
...
 
Autumn had 1 Reese's PB Cups
Random Dad tax = 0.0163006
Autumn owes dad 0.0163006 Reese's PB Cups
 
***** Aurora *****
...
 
Aurora had 1 Reese's PB Cups
Random Dad tax = 0.108809
Aurora owes dad 0.108809 Reese's PB Cups
 
Example
Standard Candies:
0. Milky Way
1. 3 Musketeers
2. Snickers
 
My Candy Bowl:
0. Milky Way
1. 3 Musketeers
2. Snickers
3. Reese's PB Cups
 
***** Austin *****
0. Reese's PB Cups
1. Snickers
2. 3 Musketeers
3. Reese's PB Cups
4. 3 Musketeers
5. Reese's PB Cups
 
Austin had 3 Reese's PB Cups
Random Dad tax = 0.335223
Austin owes dad 1.00567 Reese's PB Cups
 
***** Siena *****
...
 
Siena had 1 Reese's PB Cups
Random Dad tax = 0.513401
Siena owes dad 0.513401 Reese's PB Cups
 
***** Autumn *****
...
 
Autumn had 1 Reese's PB Cups
Random Dad tax = 0.0163006
Autumn owes dad 0.0163006 Reese's PB Cups
 
***** Aurora *****
...
 
Aurora had 1 Reese's PB Cups
Random Dad tax = 0.108809
Aurora owes dad 0.108809 Reese's PB Cups
 
Dad takes home 1.64418 Reese's PB Cups
 
Histograms/Bar charts
 
A 
frequency
 chart
Example: How many trees are
100-150 cm: 4
150-200 cm: 30
200-250 cm: 26
250-300 cm: 50
300-350 cm: 11
A 
bar chart
 is like a histogram except x values
can be categorical rather than numerical
Example: How many candy bars are
Almond Joy: 3
Crunch: 3.6
Krackel: 2.1
etc.
 
Example
My Candy Bowl:
0. Milky Way
1. 3 Musketeers
2. Snickers
3. Reese's PB Cups
 
 
***** Austin *****
0. Reese's PB Cups (3)
1. Snickers (2)
2. 3 Musketeers (1)
3. Reese's PB Cups (3)
4. 3 Musketeers (1)
5. Reese's PB Cups (3)
6. Milky Way (0)
7. Snickers (2)
8. 3 Musketeers (1)
9. Reese's PB Cups (3)
10. Milky Way (0)
11. Snickers (2)
12. 3 Musketeers (1)
13. Reese's PB Cups (3)
14. 3 Musketeers (1)
15. Reese's PB Cups (3)
16. Milky Way (0)
17. Snickers (2)
18. 3 Musketeers (1)
19. Reese's PB Cups (3)
20. 3 Musketeers (1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Example
My Candy Bowl:
0. Milky Way
1. 3 Musketeers
2. Snickers
3. Reese's PB Cups
 
 
***** Austin *****
0. Reese's PB Cups (3)
1. Snickers (2)
2. 3 Musketeers (1)
3. Reese's PB Cups (3)
4. 3 Musketeers (1)
5. Reese's PB Cups (3)
6. Milky Way (0)
7. Snickers (2)
8. 3 Musketeers (1)
9. Reese's PB Cups (3)
10. Milky Way (0)
11. Snickers (2)
12. 3 Musketeers (1)
13. Reese's PB Cups (3)
14. 3 Musketeers (1)
15. Reese's PB Cups (3)
16. Milky Way (0)
17. Snickers (2)
18. 3 Musketeers (1)
19. Reese's PB Cups (3)
20. 3 Musketeers (1)
 
 
 
 
 
 
 
 
 
 
 
 
 
***** Austin *****
0:3
1:7
2:4
3:7
 
***** Austin *****
0. XXX
1. XXXXXXX
2. XXXX
3. XXXXXXX
Example
My Candy Bowl:
0. Milky Way
1. 3 Musketeers
2. Snickers
3. Reese's PB Cups
 
 
***** Austin *****
0. Reese's PB Cups (3)
1. Snickers (2)
2. 3 Musketeers (1)
3. Reese's PB Cups (3)
4. 3 Musketeers (1)
5. Reese's PB Cups (3)
6. Milky Way (0)
7. Snickers (2)
8. 3 Musketeers (1)
9. Reese's PB Cups (3)
10. Milky Way (0)
11. Snickers (2)
12. 3 Musketeers (1)
13. Reese's PB Cups (3)
14. 3 Musketeers (1)
15. Reese's PB Cups (3)
16. Milky Way (0)
17. Snickers (2)
18. 3 Musketeers (1)
19. Reese's PB Cups (3)
20. 3 Musketeers (1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
***** Austin *****
0. XXX
1. XXXXXXX
2. XXXX
3. XXXXXXX
 
What makes a histogram different than a
normal kind of vector?
the index is a key for a mapping
 
Example
My Candy Bowl:
0. Milky Way
1. 3 Musketeers
2. Snickers
3. Reese's PB Cups
 
 
***** Austin *****
0. Reese's PB Cups (3)
1. Snickers (2)
2. 3 Musketeers (1)
3. Reese's PB Cups (3)
4. 3 Musketeers (1)
5. Reese's PB Cups (3)
6. Milky Way (0)
7. Snickers (2)
8. 3 Musketeers (1)
9. Reese's PB Cups (3)
10. Milky Way (0)
11. Snickers (2)
12. 3 Musketeers (1)
13. Reese's PB Cups (3)
14. 3 Musketeers (1)
15. Reese's PB Cups (3)
16. Milky Way (0)
17. Snickers (2)
18. 3 Musketeers (1)
19. Reese's PB Cups (3)
20. 3 Musketeers (1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
***** Austin *****
0. Milky Way XXX
1. 3 Musketeers XXXXXXX
2. Snickers XXXX
3. Reese's PB Cups XXXXXXX
 
How would we add a label to our bar
chart?
 
Example
My Candy Bowl:
0. Milky Way
1. 3 Musketeers
2. Snickers
3. Reese's PB Cups
 
 
***** Austin *****
0. Reese's PB Cups (3)
1. Snickers (2)
2. 3 Musketeers (1)
3. Reese's PB Cups (3)
4. 3 Musketeers (1)
5. Reese's PB Cups (3)
6. Milky Way (0)
7. Snickers (2)
8. 3 Musketeers (1)
9. Reese's PB Cups (3)
10. Milky Way (0)
11. Snickers (2)
12. 3 Musketeers (1)
13. Reese's PB Cups (3)
14. 3 Musketeers (1)
15. Reese's PB Cups (3)
16. Milky Way (0)
17. Snickers (2)
18. 3 Musketeers (1)
19. Reese's PB Cups (3)
20. 3 Musketeers (1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
***** Austin *****
0. Milky Way       XXX
1. 3 Musketeers    XXXXXXX
2. Snickers        XXXX
3. Reese's PB Cups XXXXXXX
 
How could we adjust the spacing so that
the bars lined up?
 
Towers of Hanoi
 
 
Genetic Pizzas
 
Slide Note
Embed
Share

Learn how to work with vectors in C++ to manage lists of standard candies like 3 Musketeers, Milky Ways, and Snickers. Practice creating, printing, modifying, and interacting with candy lists using while and for loops. Develop functions to copy and expand candy collections, as well as to sample random candies from the list.

  • C++
  • Vectors
  • Candy Operations
  • Loops
  • Functions

Uploaded on Sep 21, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Chapter 10 Paul Bodily

  2. Few reminders with vectors Always have to remember #include <vector> using namespace std; Also,

  3. Example

  4. Problem spec: Trick or Treat! 1. Create a vector of strings standardCandyBowl representing standard types of candy: 3 Musketeers, Milky Ways, and Snickers Write a function printVector() to print out the list of strings using a while loop 2.

  5. Problem spec: Trick or Treat! 1. Create a vector of strings standardCandyBowl representing standard types of candy: 3 Musketeers, Milky Ways, and Snickers Write a function printVector() to print out the list of strings using a while loop Modify the program to use a for loop instead of a while loop 2. 3.

  6. Problem spec: Trick or Treat! 1. Create a vector of strings standardCandyBowl representing standard types of candy: 3 Musketeers, Milky Ways, and Snickers Write a function printVector() to print out the list of strings using a while loop Modify the program to use a for loop instead of a while loop Write a function to create a vector myCandyBowl that starts by copying the vector standardCandyBowl and then prompts the user to add another candies 2. 3. 4.

  7. Problem spec: Trick or Treat! 1. Create a vector of strings standardCandyBowl representing standard types of candy: 3 Musketeers, Milky Ways, and Snickers Write a function printVector() to print out the list of strings using a while loop Modify the program to use a for loop instead of a while loop Write a function to create a vector myCandyBowl that starts by copying the vector standardCandyBowl and then prompts the user to add another candies Write a function selectRandom() to sample a random string from a vector 2. 3. 4. 5.

  8. Problem spec: Trick or Treat! 1. Create a vector of strings standardCandyBowl representing standard types of candy: 3 Musketeers, Milky Ways, and Snickers Write a function printVector() to print out the list of strings using a while loop Modify the program to use a for loop instead of a while loop Write a function to create a vector myCandyBowl that starts by copying the vector standardCandyBowl and then prompts the user to add another candies Write a function selectRandom() to sample a random string from a vector Write a function selectNRandom() to sample n random candies (with replacement) from myCandyBowl 2. 3. 4. 5. 6.

  9. Problem spec: Trick or Treat! 1. Create a vector of strings standardCandyBowl representing standard types of candy: 3 Musketeers, Milky Ways, and Snickers Write a function printVector() to print out the list of strings using a while loop Modify the program to use a for loop instead of a while loop Write a function to create a vector myCandyBowl that starts by copying the vector standardCandyBowl and then prompts the user to add another candies Write a function selectRandom() to sample a random string from a vector Write a function selectNRandom() to sample n random candies (with replacement) from myCandyBowl Write a function countMatches() that, given a particular string, counts how many times it appears in a vector 2. 3. 4. 5. 6. 7.

  10. Example Standard Candies: 0. Milky Way 1. 3 Musketeers 2. Snickers EXAMPLE

  11. Example Standard Candies: 0. Milky Way 1. 3 Musketeers 2. Snickers My Candy Bowl: 0. Milky Way 1. 3 Musketeers 2. Snickers 3. Reese's PB Cups

  12. Example Standard Candies: 0. Milky Way 1. 3 Musketeers 2. Snickers My Candy Bowl: 0. Milky Way 1. 3 Musketeers 2. Snickers 3. Reese's PB Cups ***** Austin ***** 0. Reese's PB Cups

  13. Example Standard Candies: 0. Milky Way 1. 3 Musketeers 2. Snickers My Candy Bowl: 0. Milky Way 1. 3 Musketeers 2. Snickers 3. Reese's PB Cups ***** Austin ***** 0. Reese's PB Cups 1. Snickers 2. 3 Musketeers 3. Reese's PB Cups 4. 3 Musketeers 5. Reese's PB Cups

  14. Example Standard Candies: 0. Milky Way 1. 3 Musketeers 2. Snickers My Candy Bowl: 0. Milky Way 1. 3 Musketeers 2. Snickers 3. Reese's PB Cups ***** Austin ***** 0. Reese's PB Cups 1. Snickers 2. 3 Musketeers 3. Reese's PB Cups 4. 3 Musketeers 5. Reese's PB Cups Austin had 3 Reese's PB Cups

  15. Example Standard Candies: 0. Milky Way 1. 3 Musketeers 2. Snickers My Candy Bowl: 0. Milky Way 1. 3 Musketeers 2. Snickers 3. Reese's PB Cups ***** Austin ***** 0. Reese's PB Cups 1. Snickers 2. 3 Musketeers 3. Reese's PB Cups 4. 3 Musketeers 5. Reese's PB Cups Austin had 3 Reese's PB Cups Random Dad tax = 0.335223 Austin owes dad 1.00567 Reese's PB Cups

  16. Example ... Standard Candies: 0. Milky Way 1. 3 Musketeers 2. Snickers ***** Siena ***** Siena had 1 Reese's PB Cups Random Dad tax = 0.513401 Siena owes dad 0.513401 Reese's PB Cups My Candy Bowl: 0. Milky Way 1. 3 Musketeers 2. Snickers 3. Reese's PB Cups ***** Autumn ***** ... Autumn had 1 Reese's PB Cups Random Dad tax = 0.0163006 Autumn owes dad 0.0163006 Reese's PB Cups ***** Austin ***** 0. Reese's PB Cups 1. Snickers 2. 3 Musketeers 3. Reese's PB Cups 4. 3 Musketeers 5. Reese's PB Cups ***** Aurora ***** ... Aurora had 1 Reese's PB Cups Random Dad tax = 0.108809 Aurora owes dad 0.108809 Reese's PB Cups Austin had 3 Reese's PB Cups Random Dad tax = 0.335223 Austin owes dad 1.00567 Reese's PB Cups

  17. Example ... Standard Candies: 0. Milky Way 1. 3 Musketeers 2. Snickers ***** Siena ***** Siena had 1 Reese's PB Cups Random Dad tax = 0.513401 Siena owes dad 0.513401 Reese's PB Cups My Candy Bowl: 0. Milky Way 1. 3 Musketeers 2. Snickers 3. Reese's PB Cups ***** Autumn ***** ... Autumn had 1 Reese's PB Cups Random Dad tax = 0.0163006 Autumn owes dad 0.0163006 Reese's PB Cups ***** Austin ***** 0. Reese's PB Cups 1. Snickers 2. 3 Musketeers 3. Reese's PB Cups 4. 3 Musketeers 5. Reese's PB Cups ***** Aurora ***** ... Aurora had 1 Reese's PB Cups Random Dad tax = 0.108809 Aurora owes dad 0.108809 Reese's PB Cups Austin had 3 Reese's PB Cups Random Dad tax = 0.335223 Austin owes dad 1.00567 Reese's PB Cups Dad takes home 1.64418 Reese's PB Cups

  18. Histograms/Bar charts A frequency chart Example: How many trees are 100-150 cm: 4 150-200 cm: 30 200-250 cm: 26 250-300 cm: 50 300-350 cm: 11 A bar chart is like a histogram except x values can be categorical rather than numerical Example: How many candy bars are Almond Joy: 3 Crunch: 3.6 Krackel: 2.1 etc.

  19. Example My Candy Bowl: 0. Milky Way 1. 3 Musketeers 2. Snickers 3. Reese's PB Cups ***** Austin ***** 0. Reese's PB Cups (3) 1. Snickers (2) 2. 3 Musketeers (1) 3. Reese's PB Cups (3) 4. 3 Musketeers (1) 5. Reese's PB Cups (3) 6. Milky Way (0) 7. Snickers (2) 8. 3 Musketeers (1) 9. Reese's PB Cups (3) 10. Milky Way (0) 11. Snickers (2) 12. 3 Musketeers (1) 13. Reese's PB Cups (3) 14. 3 Musketeers (1)

  20. Example My Candy Bowl: 0. Milky Way 1. 3 Musketeers 2. Snickers 3. Reese's PB Cups ***** Austin ***** 0. Reese's PB Cups (3) 1. Snickers (2) 2. 3 Musketeers (1) 3. Reese's PB Cups (3) 4. 3 Musketeers (1) 5. Reese's PB Cups (3) 6. Milky Way (0) 7. Snickers (2) 8. 3 Musketeers (1) 9. Reese's PB Cups (3) 10. Milky Way (0) 11. Snickers (2) 12. 3 Musketeers (1) 13. Reese's PB Cups (3) ***** Austin ***** 0:3 1:7 2:4 3:7 ***** Austin ***** 0. XXX 1. XXXXXXX

  21. Example My Candy Bowl: 0. Milky Way 1. 3 Musketeers 2. Snickers 3. Reese's PB Cups ***** Austin ***** 0. Reese's PB Cups (3) 1. Snickers (2) 2. 3 Musketeers (1) 3. Reese's PB Cups (3) 4. 3 Musketeers (1) 5. Reese's PB Cups (3) 6. Milky Way (0) 7. Snickers (2) 8. 3 Musketeers (1) 9. Reese's PB Cups (3) 10. Milky Way (0) 11. Snickers (2) 12. 3 Musketeers (1) 13. Reese's PB Cups (3) 14. 3 Musketeers (1) ***** Austin ***** 0. XXX 1. XXXXXXX 2. XXXX

  22. Example My Candy Bowl: 0. Milky Way 1. 3 Musketeers 2. Snickers 3. Reese's PB Cups ***** Austin ***** 0. Reese's PB Cups (3) 1. Snickers (2) 2. 3 Musketeers (1) 3. Reese's PB Cups (3) 4. 3 Musketeers (1) 5. Reese's PB Cups (3) 6. Milky Way (0) 7. Snickers (2) 8. 3 Musketeers (1) 9. Reese's PB Cups (3) 10. Milky Way (0) 11. Snickers (2) 12. 3 Musketeers (1) 13. Reese's PB Cups (3) 14. 3 Musketeers (1) ***** Austin ***** 0. Milky Way XXX 1. 3 Musketeers XXXXXXX 2. Snickers XXXX

  23. Example My Candy Bowl: 0. Milky Way 1. 3 Musketeers 2. Snickers 3. Reese's PB Cups ***** Austin ***** 0. Reese's PB Cups (3) 1. Snickers (2) 2. 3 Musketeers (1) 3. Reese's PB Cups (3) 4. 3 Musketeers (1) 5. Reese's PB Cups (3) 6. Milky Way (0) 7. Snickers (2) 8. 3 Musketeers (1) 9. Reese's PB Cups (3) 10. Milky Way (0) 11. Snickers (2) 12. 3 Musketeers (1) 13. Reese's PB Cups (3) 14. 3 Musketeers (1) ***** Austin ***** 0. Milky Way XXX 1. 3 Musketeers XXXXXXX 2. Snickers XXXX

  24. Towers of Hanoi

  25. Genetic Pizzas

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#