Section #1: Write some code

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

Problem #1: The 3n+1 problem

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

Starting value: 3

Problem #2: Write a class

Write a class called Time. The job of this class is to store a time of day (hours, minutes, seconds). You can choose exactly how you want to implement this class (i.e. you pick what data variables/member functions you want). But your final code should be able to do the following things:

  - Construct a new Time object by inputting hours, minutes, seconds
       in 24H format (so hours can be between 0 and 23. No am/pm).
  - Construct a new Time object by inputting hours, minutes, seconds
       in 12H format (so hours can be between 1 and 12. Yes am/pm).
  - Construct a new Time object by inputting only a number of seconds
  (Ex: 3673 seconds is the same as 1 hr, 1 min, and 13 seconds
         so 1:01am with 13 seconds)
  - Print the currently stored time to the console in 24H format
  - Print the currently stored time to the console in 12H format

Your class should be able to handle bad input in some way or another (i.e. 32:19:-4 is not a valid time). You don't have to split your code into a header and a cpp if you don't want to (all in one file is fine). Write a short main method to test your code.

Problem #3: Some input/output

I have a bunch of names saved in the following file:


The names are saved in the format


but I really want them to be in the format


Write a program that will create a file called Prob3Output.txt that contains the same names, but written in the LASTNAME, FIRSTNAME format. (I expect you to input the data from Prob3Input.txt into your program. Don't manually enter the names.)

Here's what your Prob3Output.txt should look like if your program runs correctly.


Problem #4: A problem about lists

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

If list#1 contains 1, 1, 3, -6, 0
   list#2 contains 9, 2, 4
Then your function should return a list 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() {
    list<int> v1;
    list<int> v2;
    list<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(auto iter=vTotal.begin(); iter != vTotal.end(); iter++)
        cout << *iter << endl;

    return 0;

Problem #5: Check for Palindromes

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.

Problem #6: Fibonacci numbers

Write a program that calculates the first n Fibonacci numbers (here n is a const int defined at the beginning of the program) and stores the results in a vector.

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.

Long list of Vector Problems

Solve these problems however you'd like. There are no requirements for techniques to use/not use. Think carefully about the inputs and outputs of your functions. What kind of variable should you return? When you input, should you pass by value or by reference?

Write the following functions for inputs of type vector<int>:
    -print_vector, which prints the given vector to the console

    -count, which takes as input a vector and an int, and returns the
        number of times the given element occurs in the vector
        Ex: If my vector is 1, 1, 4, 5, 1 and my integer is 4 then this function
            should return 1. If instead my integer is 1 then this function
            should return 3.

    -average, which returns the average (mean) of a vector

    -min, which returns the minimum value in the vector

    -max, which returns the maximum value in the vector
    -sum, which returns the sum of all elements in the vector
    -reverse, which modifies a vector so that its entries are in reverse

    -mode, which returns the most common value occurring in the list (if there
        is a tie, you can return whichever number you'd like)
        Ex: If my vector is 1, 1, 4, 5, 1 then this function should return 1.
            If instead my vector is 1, 1, 4, 4, 1, 4 then this function can return either
            1 or 4.
        Hint: Use the count function that you already wrote to make this easier.

Challenge Problem: Write one more function
    -median, which returns the median value of the vector
            (This one is pretty hard. You will likely need to sort
             the vector to find the median.)

Another vector problem

Write a function called same_elements, which inputs two vector<int>s, and outputs a bool. The function should return true if the vectors contain exactly the same elements (not necessarily in the same order), and return false otherwise. If one vector has some repeated elements, then the other vector should have the same repeated elements.

same_elements([1, 2], [2, 1]) returns true
same_elements([1, 2, 3], [2, 1]) returns false
same_elements([1, 2, 1], [2, 1]) returns false
same_elements([1, 2, 1], [2, 2, 1]) returns false
same_elements([2, 1, 2, 1], [1, 2, 2, 1]) returns true

One more vector problem

Write a function called factor which inputs an int (call it n here), and outputs a vector<int> that contains all the positive integers that evenly divide n.

factor(4) returns [1,2,4]
factor(5) returns [1,5]
factor(20) returns [1,2,4,5,10,20]
factor(-6) returns [1,2,3,6]

Challenge Problem: Do this if you think it will be fun

Write your own string-to-integer converter function. C++ comes with a std::stoi function that serves this purpose, but we can also just solve this on our own. We have all the required tools.

Here are a few hints:
    (1) string has a little bit of vector-like functionality. In particular, suppose str is a string.
        str[j] is the jth character in the string. str.size() is the total number of characters in the string.
    (2) Suppose c is of type char. Then the function std::isdigit tells us if c is a number or not.
    (3) We can convert from chars to ints pretty quickly: Suppose c is a char (say c = '5').
        Then c is actually represented by a value on the //">ascii table. (so if c='5' then c is 
        represented by 53). Then  (int)(c-'0') will be the integer-version of c.
        (If c = '5', then (int)(c - '0') = (int)(53 - 48) = (int)(5) = 5).
    (4) Solve this problem using a loop! Convert one char at a time.

Section #2: Read some code

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

Heads up: I put some weird tricks in these problems. All the codes should compile and run, but there are plenty of things that I would consider errors.

Problem #1

using namespace std;

void mysteryFunction(string a, int& 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;

Problem #2

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: ";

        cout << "Please input the price of the item: ";
        cin >> itemPrice;

        cout << "Would you like to quit? Answer y if yes, and n if no: ";
        cout << endl << "Your answer was " << quit << endl;

    cout << "I didn't actually save the item prices or names. HAHAHAHAHA!!!!" << endl;

    return 0;

Problem #3

using namespace std;

int main() {

    vector<char> myVec;

    for(int i=0; i<6; i++) {
        if(i = 2) {
            char temp = 'A';
        if(i = myVec.size()) {
            char temp = 'Z';

    cout << "Size of myVec is " << myVec.size() << endl;
    cout << "myVec contains " << endl;

    for(int i=0; i<myVec.size(); i++) {
        cout << << endl;

    return 0;

Problem #4

using namespace std;

// Here's another one of the standard
// "Figure out this annoying loop." problems
int main() {
    for(int i=1; i<6; i+=2) {
        for(int j=8; j>2; j = (j-1)/2) {

            cout << i << "*" << j << " = " << i*j << endl;

    return 0;

Problem #5

using namespace std;

class exampleClass {
    int i = 0;
    int j = 34;
    exampleClass() {
        int i = 12;
        int j = 13;
    void printiandj() {
        cout << i << " " << j << "\n";

int main() {
    exampleClass exampleObject;

    return 0;