Chapter 6 - Worked Exercises
Exercise 1 - Two Sum
Create a program that traverses a list, and returns two numbers in the list that sum to 14. You may only use each number once.
You can start with the following:
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {1, 12, 5, 30, 2, 8, 6};
// your code below
}The program should output the following:
The numbers '12' and '2' sum to '14'If there are no two numbers that can be added to make 14:
There are no two numbers that sum to 14Exercise 2 - Duplicates
Create a program that allows the user to enter as many inputs as desired, and the program will let the user know if the value has been seen before.
The program should continue endlessly until the user inputs -1.
Enter the next number: 5
Enter the next number: 3
Enter the next number: 10
Enter the next number: 5
I've seen that number before!
Enter the next number: -1
Exiting...Exercise 3 - Leap Years
According to Wikipedia, years that are perfectly divisible by 4 are leap years, except for years divisible by 100, which are not leap years unless they are also divisible by 400.
Year 1213 is not a leap year because it is not divisible by 4
Year 2004 is a leap year because it is divisible by 4, and not divisible by 100
Year 2100 is not a leap year because while it is divisible by 4, it is also divisible by 100 and not 400.
Year 2400 is a leap year because it is divisible by 4 - and while it is divisible by 100, it is also divisible by 400.
Create a program that asks the user for a year, and the program should check if the year provided is a leap year or not.
Enter a year: 2014
2014 is not a leap year. Enter a year: 2016
2016 is a leap year. Exercise 4 - Bubble Wrap
A sheet of bubble wrap can be represented as a 2D vector, with each bubble represented by the letter O, and a popped bubble represented by the letter .
Creating the bubble wrap
Create a program that asks the user for a number – we'll call this N, creates a 2D vector of N by N elements, and displays it to the user:
How large will your bubble wrap be? 5
Here you go!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O OHow large will your bubble wrap be? 3
Here you go!
O O O
O O O
O O OBy default none of the bubbles in the bubble wrap are popped, so each bubble should be an O.
Considerations
What should the type of the 2D vector be? Would it make sense to use an
enumhere?The program should account for invalid inputs – what happens if the user enters
0or-100?For simplicity, the program should also restrict the size of the bubble wrap – to say a size of 5.
Popping the bubble wrap
Imagine each bubble as having an index – the bubble on row 0 and column 0 has the index 0, and the bubble on row 2 and column 3 has index 11.
The program should continue asking the user which bubble they wish to pop, until there are no bubbles left to pop:
How large will your bubble wrap be? 2
Here you go!
O O O
O O O
O O O
Which bubble would you like to pop? 2
Pop!
O O .
O O O
O O O
Which bubble would you like to pop? 3
O O .
. O .
O O O
< ... some time later >
. . .
. O .
. . .
Which bubble would you like to pop? 4
Pop!
. . .
. . .
. . .
All bubbles have been popped! Considerations
What happens if a user tries to pop a bubble that's already been popped? Should the program tell the user that it's already been popped?
Last updated
Was this helpful?