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
4
2
1

Starting value: 3
3
10
5
16
8
4
2
1

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)
  - 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.

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
        order

    -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.

Examples:
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.

Examples:
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 //www.asciitable.com/">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 tried to make these problems tricky. Be careful as you read through.

Problem #1

#include<string>
#include<iostream>
using namespace std;

void mysteryFunction(string a, int& i) {
    ++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;
}

Problem #2

#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;
}

Problem #3

#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;
}

Problem #4

#include<iostream>
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;
}