Based on the midterm, I'd say knowing your "C++ trivia" seems very important for tests in this class.

Here are some links to old pages on this site where I posted problems. These are all still worth solving if you haven't already.

- The challenge at the bottom here: debugging practice!
- Everything here: several good practice problems with vectors, loops, and some logical thinking...
- Challenge problem 1 here: using random numbers to compute odds in craps.
- Both problems at the bottom here: some debugging and some coding.

Below are some general practice problems. Solve them using any method you'd like.

**Part A:** Write a program that inputs a positive integer from the user (I will call the number n here), and then applies the 3n+1 process to that integer. Here's a full description of the 3n+1 process:

If n is an odd number, then multiply n by 3 and add 1 (replace n with 3n+1). If instead n is an even number, then divide n by 2. Either way, cout the new value of n. If that value is 1, then end the process. If not, then start again using the new value of n.

Example input/output: Starting value: 4 4 2 1 Starting value: 3 3 10 5 16 8 4 2 1

**Part B:** Out of all the starting integers n with 2 <= n <= 1,000, which one requires the most steps before ending at 1? For example, n=4 takes 2 steps and n=3 takes 7 steps.

**Part C:** What if I'd allowed n up to 1,000,000 in part B (instead of 1,000)? Note: your solution from part B probably won't immediately work with that one number changed. Try to debug what goes wrong!

Write a function that inputs two vectors of integers, and returns a new vector formed by concatenating the two input vectors.

Example: If vector#1 contains 1, 1, 3, -6, 0 vector#2 contains 9, 2, 4 Then your function should return a vector containing 1, 1, 3, -6, 0, 9, 2, 4 (in that order)

Below is a main method I wrote that you can use to test your function. I called my function concat. If you give yours a different name then make sure to make the corresponding change in the main method.

int main() { vector<int> v1; vector<int> v2; vector<int> vTotal; // initializes v1 to store 1, 4, 2, 0 // initializes v2 to store 0, 1, 1, 2 for ( int i = 0 ; i < 4 ; ++i ) { v1.push_back((i+2)*3 % 5); v2.push_back((2*i+1)/3); } vTotal = concat(v1, v2); // Should output 1, 4, 2, 0, 0, 1, 1, 2 for(vector<int>::iterator iter=vTotal.begin(); iter != vTotal.end(); iter++) cout << *iter << endl; return 0; }

Write a function called isPalindrome. This function should input a string (use constant reference), and output a bool. It should output true if the string is a palindrome, and false otherwise.

Ex: "stressed desserts" is a palindrome. "racecar" is also a palindrome. "Aaaa" is not a palindrome. "Dammit Im mad" is not a palindrome.

Write a function that calculates the first n Fibonacci numbers (here n is a int typed by the user at the beginning of the program) and returns a vector containing the results.

Recall the defn of Fibonacci numbers:

F(0) = 1 F(1) = 1. Then for j >= 2 we define F(j) = F(j-1) + F(j-2). So the first 10 Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.

Write a function that takes a vector<int> as input and outputs the median of the vector's elements. If you don't remember the definition of median, go check Wikipedia. Make sure your program gives the right result even if the vector has an even number of elements! For example, the median of { 1 , 2 , 3 , 4 } should be 2.5.

Define a class called Counter. An object of this type is used to count things, so it records a count that is a nonnegative whole number. Include a mutator function that sets the counter to a count given as an input. Include member functions to increase the count by one and to decrease the count by one. Be sure that no member function allows the value of the counter to become negative. Also, include a member function that returns the current count value and one that outputs the count to the console. Write a main() function that will run a simple test of your CounterType class.

These problems are all of the same type. Read the code, and figure out what it will do when it runs. After you decide on your answer, you can copy it into Visual Studio and run it to check your work.

Heads up: I tried to make these problems tricky. Read carefully!

#include<string> #include<iostream> using namespace std; void mysteryFunction(string a, int& i) { a.erase(0,++i); cout << "I edited the string and got: " << a << endl; } int main() { string str = "I like pie."; int x = 2; mysteryFunction(str, x); cout << "The character at position " << x << " is: " << str[x] << endl; cout << "The final string is: " << str << endl; return 0; }

#include<string> #include<iostream> using namespace std; int main() { cout << "It's time to set up a shopping list." << endl; string quit = "n"; string itemName; double itemPrice; while(quit == "n") { cout << "Please input the name of the item: "; getline(cin,itemName); cout << "Please input the price of the item: "; cin >> itemPrice; cout << "Would you like to quit? Answer y if yes, and n if no: "; getline(cin,quit); cout << endl << "Your answer was " << quit << endl; } cout << "I didn't actually save the item prices or names. HAHAHAHAHA!!!!" << endl; return 0; }

#include <iostream> #include <vector> using namespace std; int crazyFunction(vector<int> a); void printArray(vector<int> a); int main() { vector<int> a = {3, 2, 1, 2, 3}; int r; r = crazyFunction(a); printArray(a); cout << a[r] << endl; return 0; } int crazyFunction(vector<int> a) { for(int i=0; i<a.size()-1; i++) { a[i] = a[i]/a[i+1]; } return a[a.size()/2]; } void printArray(vector<int> a) { for(int i=0; i<a.size(); i++) cout << a[i] << endl; }

#include<iostream> #include<vector> using namespace std; int main() { vector<char> myVec; for(int i=0; i<6; i++) { if(i = 2) { char temp = 'A'; myVec.push_back(temp); } if(i = myVec.size()) { char temp = 'Z'; myVec.push_back(temp); } } cout << "Size of myVec is " << myVec.size() << endl; cout << "myVec contains " << endl; for(int i=0; i<myVec.size(); i++) { cout << myVec.at(i) << endl; } return 0; }

#include <iostream> using namespace std; class ShoeType { public: char style; double price; }; int main() { ShoeType shoe1,shoe2; shoe1.style = 'A'; shoe1.price = 9.99; cout << shoe1.style << " $" << shoe1.price << endl; shoe2 = shoe1; shoe2.price = shoe2.price/9; cout << shoe2.style << " $" << shoe2.price << endl; }