Homework 3

Due Date: October 4th, 2021 at 12:00pm

For this assignment, you will submit multiple C++ compilable files containing a program written in C++. For every function, you must have the proper function documentation as well! We suggest (strongly) that you make a directory for every assignment! Name your file a meaningful name and give it a .cpp extension since you will be compiling it. Also, make sure that you compile and run your program using the GNU (g++) compiler before submitting to make sure that it will work.


Congratulations! You are now admitted to the Codémon Academy (after significant efforts) and you just can’t wait to become a Codémon master. However, every new student must go through a test first to determine their inherent potential of becoming one. The test requires you to bravely enter the unspeakable Codémon Forest, located just behind the school yard of the academy. Many different species of Codémons live inside the forest and your task is to capture and collect as many Codémons as you can.

Each Codémon monster is categorized into different family types; each family possesses a unique set of power and attributes. For this particular test, you will be concerned with the family of Flying, Poison, Rock, Bug, Water, and Dragon. In order for you to successfully capture each Codémon, the trick is to outsmart them by performing some mathematical magic. The faster you solve the problems, the more Codémons you’ll catch at the end. Remember, this test is not an easy one—it has the reputation of even costing a student’s life if not being careful. Though, if passed with flying colors, then the future, for sure, will be destined to bathe in the glory of being an almighty Codémon master.


  1. The program should begin with you entering the forest to start the test. Once inside the forest, you’ll need to begin the test by calling the explore()1 function. This function should return the family type of the Codémons that you encountered and should obtain the result by initiating the sequence to generate a random letter character (A-Z, case sensitive). If the random character is a match for any of the first letters of the said family types (Bug, Flying, Rock, Poison, Water), then that’ll be the family type of the Codémon encountered. For example, if the random character obtained is B, then the encountered type would be Bug.

    • As you can see, while this function will not run infinitely, but sometimes it could take a long time to encounter a Codémon
    • This function should also keep track of the amount of time it takes for the current Codémon to be encountered. The student should be notified of this information when the function returns. A time unit is measured by the number of total random character iteration.
    • Since the family type of Dragon is a rare one, it can only be encountered if you are SUPER lucky. That is, return the Dragon family type only if you have encountered the same family type TWO times in a roll. Replacing the duplicate roll with the Dragon type. This must be logically determined from within the explore() function without the need of any function arguments to be provided. For instance, let’s say you first encountered a Rock Codémon, during your second exploration, if the random character matched that of Rock again, return the Dragon type instead.
  2. Now that you have encountered a Codémon, the next task would be to capture it. There is going to be a different capture() function that you’ll use for each family type, but the function name stays the same2. In fact, for each version of the capture() function, the core logic depends on the mathematical problem referred to as the “Triangular Number Sequence”. A triangular number is a unique number that can be represented in the form of a triangular grid of elements such that every row contains an element more than the previous one. For example, 15 is a valid triangular number but 20 is not (21 is though).

    For the capture() functions that you are going to overload, most of them require you to compute a random, valid triangular number within a certain range.

    For this particular homework assignment, you may safely assume that any given range will contain at least one valid triangular number.

    Here, you’ll need to figure out two things: 1) how to validate a given integer value is a triangular number, and 2) be able to generate a random number within a [min, max] range. What about the rest of the steps? Well, it’s a test! So, you’ll need to figure them out yourself3. Next, checkout the following detail requirement for capturing each specific family type:

    Flying: capture()

    In order to capture a Flying Codémon, it needs to know the length of the student’s left and right arm measured separately in inches (0<length<=1000). This piece of info should be obtained from the student before entering the unspeakable forest and to be passed into this function as parameters. The capture attempt would be successful if the sum of the two lengths (no decimals) equal to a valid triangular number.

  3. Poison: capture()

    In order to capture a Poison Codémon, you’ll have to first generate a random valid triangular number within the range of [1, 100]. The capture is a successful one if that number is smaller than another magical number 434. NOTE: The superscript 4 DOES NOT mean 43 to the power of 4. The supersciprt 4 references number 4 in the notes at the bottom of the page. I know, it's confusing.

    Rock: capture()

    In order to capture a Rock Codémon, a battle is necessary. The Rock Codémon will first generate a random valid triangular number, followed by you also doing the same. The range is [56, 4586]. You’d then take the number you generated and multiply it with the amount of time it takes to encounter this Codémon. You will win the battle if your resulting value is greater than the Rock Codémon’s.

    Bug: capture()

    The requirement you need to satisfy to capture a Bug Codémon is more complex than the other ones since it involves the use of pesticide. The function will first need to know whether or not you have already captured at least a Poison and Water Codémon—otherwise you won’t have the ingredients you need to produce the pesticide which will temporarily paralyzed the Bug Codémon you are trying to capture. This information needs to be provided to the function when it’s called.

    Water: capture()

    The key to capturing a Water Codémon is somewhat similar to that of a rock type. The Codémon will generate a random valid triangular number first within the range of [10000000, 20219999]. Your battling number, though, is not randomly generated this time. The function needs to know some personal information on the student’s birth year, month, and day (should be obtained before the student enters the forest…) The end result is going to be an integer combination of the different attributes. For example, if the student is born in 1990, January 26th, then the representative integer number would be 19900126. You win if your “birth number” is smaller than the Codémon’s.

    Dragon: capture()

    Note that a special capture function for the Dragon type is not needed since your “super luckiness” shall result in an automatic capture in this case.

  4. Like all good things, this test eventually comes to an end when the student has collected enough Codémon monsters, and that magical number is 20. A student is deemed SUPER if the total exploration time spent is less than or equal to 50 time units, ACCEPTABLE if it is less than or equal to 200 time units, and DIMINISHING (of the Codémon master potential) if it is anything else. Based on the student’s standing, the testSummary() function should be called with different parameters; however, it should only be defined ONCE in your implementation.

    In addition, the academy is not that mean. It will reward the students who completed the test with Dragon Codémons if they are lucky. And by lucky we mean only the students who performed at the SUPER level.

    The function signature resembles something like this:

    Void testSummary( student_ranking,

    Consider the flexibility of the ranking_based_info parameter here, it should have the potential to represent ANY information needed. So be careful when choosing the most appropriate data type for it.

    • SUPER: if a student is super, a string of messages (input from the student) should be passed into the function, expressing the emotions of joy and happiness, as well as the number of Dragon Codémon captured (yes, the student gets to keep all the Dragon Codémons as a reward!) .If the super student did not capture any, then omit this parameter and the academy will still reward a total of ONE to the student. The function should produce the following output if the student’s name is Andy Royale and he captured two Dragon Codémons

      Andy Royale’s Test Ranking: SUPER
                    Andy’s Emotional Message: whatever_Andy_wants_to_say!
                    Andy will be rewarded 2 Dragon Codémons! 

      When prompting Andy for his emotional message input, you might go for:

      cin >> emotionalMsg; 

      Don’t, because it won’t retrieve the entire line, use the following method instead5:

      cin.ignore(); // to clean out the input stream buffer  
      getline(cin, emotionalMsg);  
    • ACCEPTABLE: if a student shows an acceptable level of potential, then you should pass into the function the first letter of the Codémon family type that is collected the most during the test. It’s completely up to you how to handle a tie-breaker if it occurs. The function should produce the following output if the student’s name is Johnie Katlego and the ROCK Codémon type is captured the most:

      Johnie Katlego’s Test Ranking: ACCEPTABLE
      Johnie’s Favorite Letter: R!
      Johnie will be rewarded 0 Dragon Codémons! 
    • DIMINISHING: the diminishing Codémon master potential in a student is primarily expressed via the total exploration time (hint: this value should already be stored in an INT variable). So, pass this into the function. The function should produce the following output if the student’s name is Lou Wen:

      Lou Wen’s Test Ranking: DIMINISHING
      Lou’s Total Exploration Time: whatever_that_time_is!
      Lou will be rewarded 0 Dragon Codémons!

Sample Output

Note: this sample output is generated for terminating the test when 3 Codémons are successfully caught.

Hello Challenger!
Let's obtain some essential info from you first before we begin the test:
What's your first name? Andy
And your last name? Royale
Now tell me how long your left arm is (in inches, >0 and <= 1000): 92
Now tell me how long your right arm is (in inches, >0 and <= 1000): 96
And your birth year? 1991
And your birth month? 8
And your birth day? 11
Thanks! Now off to the Codémon Forest you go!

1. Andy encountered a Water codémon in 2 time units.
Your number (19910811) is not less than H2O's random triangular number (18693555)!
The capture is unsuccessful...

2. Andy encountered a Bug codémon in 4 time units.
You did not have the pesticide!
The capture is unsuccessful...

3. Andy encountered a Rock codémon in 4 time units.
Your number (11100) beat Rocky's random triangular number (3570)!
The capture is successful!

4. Andy encountered a Bug codémon in 14 time units.
You did not have the pesticide!
The capture is unsuccessful...

5. Andy encountered a Poison codémon in 2 time units.
The random triangular number (66) is not less than the magic number (43)!
The capture is unsuccessful...

6. Andy encountered a Flying codémon in 6 time units.
Your total arm lengths (188) is not a triangular number!
The capture is unsuccessful...

7. Andy encountered a Rock codémon in 5 time units.
Your number (22325) beat Rocky's random triangular number (3916)!
The capture is successful!

8. Andy encountered a Water codémon in 2 time units.
Your number (19910811) is not less than H2O's random triangular number (15716421)!
The capture is unsuccessful...

9. Andy encountered a Rock codémon in 6 time units.
Your number (2610) beat Rocky's random triangular number (528)!
The capture is successful!

Congratulations! You passed the test with FLYING colors! 
What's in your mind right now? 
I really want to scream, but I am too happy to say anything right now! 
Here's your test result:
Andy Royale's Test Ranking: SUPER
Andy's Emotional Message: I really want to scream, but I am too happy to say anything right now! 
Andy will be rewarded 1 Dragon Codémons!
  1. It is up to you to determine the most appropriate parameters for the explore() function.
  2. Technically, each capture() function can return something different. But in this case, they should all be returning the same thing (you don’t have to listen to me). And again, you need to be smart about choosing the most appropriate parameters for each version of this function since it’s being overloaded.
  3. Feel free to create additional functions relevant to handling the triangular numbers. In fact, I’d recommend you to for the sake of function reusability.
  4. Did you know that our world emits about 43 billion tons of CO2 a year?
  5. If the getline function is not working, make sure that you have referenced the library #include <string>.
  6. Seed the random number generator function with the value of 1100.
  7. Even though everyone is using the same seed for the random number generation, the random valid triangular numbers generated among your peers might be different. As long as your chosen approach is reasonable and insaneness-free in obtaining the valid triangular numbers (the random number function has to be incorporated though), then you should be fine.