Hello everyone! Welcome back to another episode of me trying to learn code! This week, my mentor gave me my very first independent assignment. In summary, she provided me with a problem, supplied me with the necessary knowledge to solve it, and let me problem solve. The assignment was to create a calculator that found the gravitational attraction between two objects. Did I know what gravitational attraction was? No, of course not. But that’s not important. The important bit is the formula. The basic idea was to have the program ask you what the two objects were, have the person input the numbers, and have the program spit out an answer. So, I need to input this formula into the code:
Just like last time, let’s go over the code.
The beginning of the code is just like that last program. Just a bit of code telling the program what libraries are included in my code. The small bunch of code underneath that are the variables. So we have object one, object two, mass one, mass two, and distance. These are all variables from the equation that the user will be inputting.
This section of the code just asks the user what the objects are, and has the user input their answers. I’ve explained “cout” and “cin” in a previous post, so you can go check that out if you want to know more, or if you just can’t remember.
Next, we have a relatively new code that my mentor taught me, the “if” statement. It basically works just like it sounds like, if the code, or whatever variable fulfills the requirements in the brackets right next to the code, it will run the code below. In this example, if object one is the exact same as object two, then it will run an error message saying that the two objects cannot be the same. So, lets say that you input Earth as object one. If you input Earth again for object two, it will stop the code, and send out an error message. This code was implemented because if the distance between the objects are 0, the formula will not work, and there’s really no reason to use the program.
Once the code has verified that the two objects are not the same, it will continue on with the code and start to ask you what the variables are for the formula. It will ask for the mass of object one, the mass of object two, and the distance between the objects.
Now that the program has the value of all the necessary variables, it can finally make the calculation. This was probably the most difficult part of the code because I didn’t understand the formula all too well. But after mulling over it for awhile and with the help of Mike (Thank you Mike), I was able to recreate the formula using code. After the program runs the formula with all the variables, it will spit out its final calculation in the form of a sentence that I created.
Now let’s see it in action! (The file exceeded the size limit so here’s a link!)
*Quick disclaimer: I explained this in my last post, but using doubles (numerical variables with decimals) has a pretty bad problem with rounding, so the numbers may not be super accurate. But what matters is that I’m close (Which hopefully I am). I also realize that a certain science teacher may see this so… let’s just hope that it’s at least close.
I’ve probably mentioned this in all of my previous posts, but communication is super important in all of my meetings, especially with a super logic based topic like coding. So let’s analyze one of my previous conversations with my mentor.
Just for context, my mentor was explaining the difference between “X++” compared to “++X” and was teaching me the importance of order.
Mentor: So, do you remember what ++ means?
MJ: Yeah, it just means add one to the uhh… value or variable, right?
Mentor: Yep, but did I teach you what happened when you put the variable before or after ++?
MJ: Uh, I don’t think so?
At this point, the blue hat is being used as my mentor is trying to organize the conversation by checking by knowledge. She’s finding the main topic or idea for the current conversation.
Mentor: Ok, ok. So, basically what the order does is that it indicates when the program should add the number to the variable. So if we have ++X, one is added to the variable right away since the ++ comes before the X. By the time the line is finished, the value of X now has one added to it. But if we look at the other one, X++ the one is added after that line is complete since the ++ comes after the X. Does that make sense?
MJ: … sorry, I don’t really understand…
Mentor: Ok, in that case, let me just pull up a quick video.
This section of our conversation heavily utilized the white hat. My mentor was presenting me with information for me to take in. I on the other hand was using the black hat as I tried to understand the given information. Let’s continue the conversation after we watched the video.
MJ: So, basically it’s kind of like the difference of counting from 0 or 1?
Mentor: Uhh… yeah kind of! It’s basically just whether you count the variable number in your count. So if the variable is 3, the difference is whether you count 3 first, or 4 first.
This time, the black hat and a bit of the green hat was used. I critically analyzed the information from the video and used the green hat to help me come up with an example. Then, my mentor put on the black hat to try to understand my example, and to see if it was a good representation of the information.
MJ: Ok, that makes a lot more sense to me. But what’s the purpose of the order? I don’t really understand why they implemented this function if it’s such a small difference.
Mentor: It’s mostly just depends on what your counting, or what your preference is. Let’s say you’re counting time, in that case, you can’t count from 0, you have to count from 1. But if we have, lets say… number of people. You need to have the option of counting 0 in the case where there are 0 people.
Finally, in this last bit of the conversation, the black hat and the yellow hat were used. I thought about the purpose of this function and realized that there was probably a better way of implementing this method. But my mentor explained the purpose of the functions using the yellow hat and found the use for this function.
Thanks for coming to check out my blog post again! This one may feel a bit long, so hopefully I didn’t bore you to death. Now that the final In Depth night is approaching, my mentor and I are beginning to work on out final project, so I guess you can start looking forward to that. Thanks again for reading! Just remember, plus ultra, reach further beyond.