In Depth Post 3: The Code Strikes Back

Hey ya’ll! Welcome back to my In Depth blog posts. It’s been about two weeks since the last post, and with the help of my mentor, I think I made quite a bit of progress so far. Last week, I discussed and explained some code like cin, and cout. This week, my mentor and I focused more on expanding on some of the different uses for these codes. We managed to develop two very basic programs, one that simply greets you, and another that is able to calculate what year you were born in based on your age.

So, let’s start off with the code.

The code here is very basic, and is hopefully easy to follow. As mentioned in my last post, the first few lines are simply commands to tell the computer which code libraries we will be using. Now, on line seven, there is the code: using namespace std;. This is a new trick my mentor taught me that can be used for more efficient code writing. I believe I mentioned this last time, but because of the code library that I am using, each code must start with std; to tell the computer which library it is from. However, if you’re only using one library, you can get around having to type it before every code. By saying: using namespace std;, you’re telling the computer that all the code that you are using is from that library, unless specified.

The next topic that I learned about were strings and integers. These are basically what you call the variables. Integers are number variables, while strings are words. So, if you look around in the code, there should be a total of four variables, name, answer, age, and birthyear.

These variables must now be defined. Meaning that we need to tell the computer what each of these represent and what to do with them. To start off, we have name. This is a very simple code that simply asks for your name, and the user must simply respond with an input.

Next, we have age. This is an integer, so we must give it some sort of value to begin with. In this case, we made it equal to 0. Then, the computer will ask the user “How old are you?”. To which the user will respond with their age.

After that, we have answer and birthyear. We created a very simple formula for the birthyear, birthyear = 2021 – age. However, this formula will not be accurate if the user has not celebrated their birthday this year. So, in order to compensate for that, we create a very short code to account for that. By asking, “Have you celebrated your birthday this year?” we can take their answer of yes or no, and can create a if, else code to help accurately calculate the user’s birthyear. So, if the user answered yes, then the birthyear stayed the same because it is already accurate. However, if the user answered no, then the birthyear that was just calculated is subtracted by one.

Then finally, all the variables are gathered and the computer will output all the information that it gathered, and calculated.

All of this work accumulates into this super simple program, that you can watch below.

Now, learning all of these different codes and their uses was extremely tricky. You may understand where I’m coming from just based on the explanations in this blog post. There is a lot of memorization, and understanding these concepts are actually quite difficult. So, there was a lot of back and forth, clarifications, and analogies/metaphors. For example, as she was explaining the portion of the code that accounted for whether you’ve celebrated your birthday or not, I was pretty confused as she was going at a rather fast pace. So, I tried to slow down the conversation by using trying to reiterate what she had just said in a different way. This also helped me to better understand the concept at hand.

Now for next week, I believe that I will be learning some new code and begin developing a new program that will be a bit more complicated that this one. So until next time, plus ultra!

-MJ

Categories: Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *