My learning process so far has been this: meet with Massimo and look at the topic of the next chapter in the book. Then, we’ll code together (mostly, he’ll tell me what to code) writing made-up examples within that particular chapter topic. For the chapter on classes, we made a few basic classes. The chapter on expressions, we both play around with expressions in Objective-C. Then, I’d use the rest of the week to actually read the chapter on my own, making sure I understand everything before moving forward, and performing the exercises in at the end of the chapter on my own.<\/p>\n
I’ve just completed chapter 5: “Programming Loops,” and stumbled upon my first example that really made me struggle.<\/p>\n
The factorial of an integer n<\/code>, written n!<\/code>, is the product of the consecutive integers 1<\/code> through n<\/code>. For example, 5 factorial is calculated as follows:
\n 5! = 5 x 4 x 3 x 2 x 1 = 120<\/code>
\nWrite a program to generate and print a table of the first 10 factorials.<\/p><\/blockquote>\nMy brain’s first response, as reading this questions, was to go completely blank. I knew the answer would involve loops (not only was that the topic of this chapter, but that part is already obvious to me). I just had to figure out how to put the loop together.<\/p>\n
I knew that I would need to utilize both subtraction and multiplication, as I needed to take a number i<\/code>, subtract 1, multiply that by the original number, subtract 1 again, multiply that, and so on until i<\/code> became 1.<\/p>\nThe initial challenge, when tackling these logic problems, is knowing how many variables to declare and what I’ll do with each. I’d need an integer for my loop (I’m a fan of using i<\/code>, although the book usually uses n) as well as at least one more integer variable to store the sum of the multiplication as my program going through the loop.<\/p>\nThe question asked for a table with the first 10 factorials, which I interpreted as a table with two columns, the first listing the numbers from 1 to 10 and the second listing the corresponding factorials.<\/p>\n
Since I’d need to calculate factorials for each of the integers between 1 and 10, I knew I’d need a loop that would calculate each integer between 1 and 10, one by one. To do this, I’d need a loop. Then I’d need another loop inside that loop to calculate the factorial for each integer.<\/p>\n
I started easy:<\/p>\n
for (int i = 1; i <= 10; i++)<\/code><\/p>\nNow I needed to figure out what went inside this loop.<\/p>\n
I knew that i<\/code> would be the number that I would need to work with, so I initially used the integer of i<\/code> within the loop, creating a process where I subtracted 1<\/code> and then *=<\/code> that number to i<\/code>. But a bit of critical thinking, and an unsuccessful build and run later, I realized that messing with i<\/code> messed with the loop. Since the loop is performing it’s task and handling the i++<\/code> feature, messing with i<\/code> can result in exiting the loop early or other unexpected results.<\/p>\nI created a new integer, that I called temp<\/code> (horrible naming convention, but I had no idea what to call it yet because I was just testing out ideas) that I could assign the value of i<\/code>.<\/p>\nI’d need to assign the value of i<\/code> to my integer temp<\/code>, subtract one, multiply that number by the original temp<\/code> value, save that new value, and continue. I just started writing equations, testing ideas out. I wrote a for loop inside the above for loop, added an NSLog<\/code> command to print out numbers as my program looped through the code, but for some reason the loop only processed my number once, returned one value, then exited.<\/p>\nMuch of my confusion stemmed from the loop within a loop, so I decided to ditch the 10 to 1 countdown loop and instead isolate the factorial number loop. Once I got that to work, I’d insert it into my previous loop.<\/p>\n
I decided I wanted to test my new loop by calculating the factorial number of 5, since that’s smack dab in the middle of 1 and 10. I assigned 5 to an int<\/code> variable outside the loop, then used that variable in a loop.<\/p>\nint factorialNumber = 1;<\/code>
\nint number = 5;<\/code><\/p>\nfor (int i = number; i > 0; i--)<\/code><\/p>\nThis was my first time working with decreasing loops (i--<\/code> rather than i++<\/code>) but since I’d be subtracting 1 from the number until it equaled 1 (multiplying the number by 0 would obviously mess things up).<\/p>\nAt the end of this loop, I knew that I’d need the factorial number of whatever was assigned to number. My original plan was to subtract 1 from my int<\/code> number in the first line of the loop, but I realized I could have the loop do that for me. All I’d need in my loop was factorialNumber *= i<\/code> (by this point, I had renamed temp<\/code> to factorialNumber<\/code>).<\/p>\nAfter a bit of condensing, here’s what I had (and changing my counter integer to j, since I wanted to use i in my initial loop):<\/p>\n
int factorialNumber = 1;<\/code><\/p>\nfor (int j = 5; j > 0; j--) {<\/code>
\n factorialNumber *= j;<\/code>
\n}<\/code><\/p>\nNSLog(@\"Factorial Number is: %i\",factorialNumber);<\/code><\/p>\nThis returns the factorial number for 5, and I can change the number 5 in the loop to anything I want. Next, I needed to figure out how to put this loop into my other loop.<\/p>\n
I realized that I would be calculating the factorial for each number, from 1 – 10, which is what the value of i<\/code> is going to be in the first loop. By subsituting “5” in the second loop with i<\/code>, I’d be able to process each number and get the results I want.<\/p>\nAfter playing around a bit, getting things just right, here was my final code:<\/p>\n
for (int i = 1; i <= 10; i++) {<\/code>
\n int factorialNumber = 1;<\/code>
\n for (int j = i; j > 0; j--) {<\/code>
\n factorialNumber *= j;<\/code>
\n }<\/code>
\n NSLog(@\"i: %2i, temp: %i\", i, factorialNumber);<\/code>
\n}<\/code><\/p>\nThe process of arriving at this final code took a lot of thinking and a few walks around the block. But I ended up with the code I needed, doing exactly what I wanted it to do. There were a few times that I wanted to just give up and ask for help, but I stuck it out and ended up getting the answer.<\/p>\n
I can’t wait for problems like these to become second nature so I can start tackling more complex exercises.<\/p>\n","protected":false},"excerpt":{"rendered":"
In addition to my traditional posts, I want to use this website as a personal journal recording my experiences learning to code. This is the first of what will be many posts relating to my learning. The main purpose of these posts are for my own benefit, processing my own thoughts by writing them down […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_newsletter_tier_id":0,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[688],"tags":[672,655,627],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p123kB-tB","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.iamdann.com\/wp-json\/wp\/v2\/posts\/1835"}],"collection":[{"href":"https:\/\/www.iamdann.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.iamdann.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.iamdann.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.iamdann.com\/wp-json\/wp\/v2\/comments?post=1835"}],"version-history":[{"count":4,"href":"https:\/\/www.iamdann.com\/wp-json\/wp\/v2\/posts\/1835\/revisions"}],"predecessor-version":[{"id":1855,"href":"https:\/\/www.iamdann.com\/wp-json\/wp\/v2\/posts\/1835\/revisions\/1855"}],"wp:attachment":[{"href":"https:\/\/www.iamdann.com\/wp-json\/wp\/v2\/media?parent=1835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.iamdann.com\/wp-json\/wp\/v2\/categories?post=1835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.iamdann.com\/wp-json\/wp\/v2\/tags?post=1835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}